jQuery 动画多个选择器

发布于 2025-01-08 06:42:07 字数 608 浏览 3 评论 0原文

我什至不知道如何表达这个问题。下面的代码具有我想要的效果,但它完全多余且丑陋,我不知道如何使它更优雅一点。我尝试了 while 循环但没有成功。也许是递归函数或者有更好的方法来做到这一点?

function next() {

$('#scroll_wrapper li:first-child').animate({
  'top' : -elemHeight-offSet
}, {duration: 1000, queue: false, complete: nextDone});


$('#scroll_wrapper li:nth-child(2)').animate({
  'top' : offSet
}, { duration: 1000, queue: false });

$('#scroll_wrapper li:nth-child(3)').animate({
  'top' : offSet2
}, { duration: 1000, queue: false });

function nextDone() {
  var d = $(this).detach();
  d.appendTo('#scroll_wrapper ul').css({'top' : elemHeight+100});
}


}

I'm not even sure how to phrase this question. The code below has the desired effect I want, but it's completely redundant and ugly, and I'm not sure how to make it a little more elegant. I've tried a while loop without success. Maybe a recursive function or is there a better way to do this?

function next() {

$('#scroll_wrapper li:first-child').animate({
  'top' : -elemHeight-offSet
}, {duration: 1000, queue: false, complete: nextDone});


$('#scroll_wrapper li:nth-child(2)').animate({
  'top' : offSet
}, { duration: 1000, queue: false });

$('#scroll_wrapper li:nth-child(3)').animate({
  'top' : offSet2
}, { duration: 1000, queue: false });

function nextDone() {
  var d = $(this).detach();
  d.appendTo('#scroll_wrapper ul').css({'top' : elemHeight+100});
}


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月蹉跎了容颜 2025-01-15 06:42:07

像这样的东西可能会起作用:

var next = function(){

    var nextDone = function(){
        var d = $(this).detach();
        d.appendTo('#scroll_wrapper ul').css({'top' : elemHeight+100});
    }

    var animateElement = function( element, top, callback ){
        $( element ).animate({
            top: top
        }, { duration: 1000, queue: false, complete: callback });
    };

    animateElement( '#scroll_wrapper li:first-child', -elemHeight-offSet, nextDone );
    animateElement( '#scroll_wrapper li:nth-child(2)', offSet );
    animateElement( '#scroll_wrapper li:nth-child(3)', offSet2 );

};

不过,可以说它更复杂。

Something like this might work:

var next = function(){

    var nextDone = function(){
        var d = $(this).detach();
        d.appendTo('#scroll_wrapper ul').css({'top' : elemHeight+100});
    }

    var animateElement = function( element, top, callback ){
        $( element ).animate({
            top: top
        }, { duration: 1000, queue: false, complete: callback });
    };

    animateElement( '#scroll_wrapper li:first-child', -elemHeight-offSet, nextDone );
    animateElement( '#scroll_wrapper li:nth-child(2)', offSet );
    animateElement( '#scroll_wrapper li:nth-child(3)', offSet2 );

};

Though, arguably it's more complex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文