【问题标题】:How to scroll to the new position of an element using the scrollTo plugin?如何使用 scrollTo 插件滚动到元素的新位置?
【发布时间】:2014-06-07 12:01:35
【问题描述】:

我编写了一个 jQuery click 事件处理程序,它隐藏除一个以外的所有元素,并使用 scrollTo 将窗口滚动到该元素的顶部,如下所示:

$(".jumbotron").click(function(){
            $('.jumbotron').not(this).toggle("slow");
            $.scrollTo($(this).position().top, 500);    
        });

但是窗口滚动到元素的旧位置,而它已经移动到另一个位置。如何隐藏所有其他元素并滚动到新位置?

【问题讨论】:

    标签: javascript jquery scrollto


    【解决方案1】:

    最好在回调中这样做:

    $(".jumbotron").click(function(){
       var $this = $(this); // cache it here
       $('.jumbotron').not(this).toggle("slow", function(){
          $.scrollTo($this.position().top, 500);
       });
    });
    

    .toggle( [duration ] [, complete ] )

    【讨论】:

      【解决方案2】:

      作为对 Jai 提议的改进:

      $(".jumbotron").click(function(){
         var that = this;
         $('.jumbotron').not(that).toggle("slow", function(){
            $.scrollTo(that, 500);
         });
      });
      

      应该可以,scrollTo 也接受 DOMNodes 和 jQuery 对象。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-20
        • 2017-07-05
        • 1970-01-01
        • 1970-01-01
        • 2014-04-08
        相关资源
        最近更新 更多