gpt4 book ai didi

jQuery - 动画元素定位

转载 作者:太空宇宙 更新时间:2023-11-04 14:15:50 24 4
gpt4 key购买 nike

如何让下面列出的 jQuery 正确设置动画?

var search = $('.searchField');
var searchButton = $('.searchSubmit');

search.on('focus', function() {
search.animate({
'right':'auto',
'left':'0'
}, 600);
searchButton.animate({
'right':'0',
'left':'auto'
}, 600);
});
search.on('focusout', function() {
search.animate({
'right':'0',
'left':'auto'
}, 600);
searchButton.animate({
'right':'auto',
'left':'0'
}, 600);
});

理想情况下,我希望 searchButton 从左到右,在 :focus 上,用 search 切换位置。我正在使用 animate 来尝试让两个元素“滑动”到它们的新位置,而不是让元素从一侧跳到另一侧。

目前,searchButton 不会在 :focus 上向右移动,search 元素会跳转到左侧定位,而不是具有“滑动”效果。

For the live jsFiddle, click here.

最佳答案

我认为使用简单的 CSS 过渡很有意义:

.searchField {
/* ... */
top: 0;
right: 0;
transition: all .6s ease;
&:focus {
/* ... */
right: 50px;
&+.searchSubmit {
left: 100%;
margin-left: -50px;
}
}
}

同时删除 </input> ,输入元素是自闭标签:<input /> .

演示:http://jsfiddle.net/u97jkeq1/8/

关于jQuery - 动画元素定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26895503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com