Javascript 如何使用scrollTo插件滚动到元素的新位置?

Javascript 如何使用scrollTo插件滚动到元素的新位置?,javascript,jquery,scrollto,Javascript,Jquery,Scrollto,我编写了一个jQuery click事件处理程序,它隐藏除一个元素之外的所有元素,并使用scroll将窗口滚动到该元素的顶部,如下所示: $(".jumbotron").click(function(){ $('.jumbotron').not(this).toggle("slow"); $.scrollTo($(this).position().top, 500); }); 但是当窗口已经移动到另一个位置时,它会滚动到元

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

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

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

最好在回调中这样做:

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

作为对Jai提议的改进:

$(".jumbotron").click(function(){
   var that = this;
   $('.jumbotron').not(that).toggle("slow", function(){
      $.scrollTo(that, 500);
   });
});
这应该是可行的,scrollTo也接受DOMNodes和jQuery对象