2013-08-02 141 views
0

我运行这个jQuery脚本滚动的divJQuery的动作滚动窗口向下

http://jsfiddle.net/sg3s/rs2QK/

jQuery(function($) { 

     $('a.panel').click(function() { 
      var $target = $($(this).attr('href')), 
       $other = $target.siblings('.active'); 

      if (!$target.hasClass('active')) { 
       $other.each(function(index, self) { 
        var $this = $(this); 
        $this.removeClass('active').animate({ 
         left: $this.width() 
        }, 500); 
       }); 

       $target.addClass('active').show().css({ 
        left: -($target.width()) 
       }).animate({ 
        left: 0 
       }, 500); 
      } 
     }); 

    }); 

问题是,如果例如,上面有具有一定高度,这JQuery的两个div的报头将滚动窗口为相同的标题高度的行动。

有什么办法可以防止这种情况发生?

感谢

回答

0

使用在底部返回false,结合事件锚element.It将阻止在URL和您的问题添加#时。

$(document).ready(function(){ 
    $('a.panel').click(function(e) { 
     e.stopPropagation(); 
     var $target = $($(this).attr('href')), 
      $other = $target.siblings('.active'); 

     if (!$target.hasClass('active')) { 
      $other.each(function(index, self) { 
       var $this = $(this); 
       $this.removeClass('active').animate({ 
        left: $this.width() 
       }, 500); 
      }); 

      $target.addClass('active').show().css({ 
       left: -($target.width()) 
      }).animate({ 
       left: 0 
      }, 500); 
     } 
     return false; 
    }); 
});