2013-11-09 42 views
0

我正在使用此代码在页面内滚动。基本上,检查内部联系,如果他们是间隔,增加了一个平滑滚动:如何修改jscript以防止滚动的特定链接?

<script> 
$(function() { 
    $('a[href*=#]:not([href=#])').click(function() { 
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { 

     var target = $(this.hash); 
     target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 
     if (target.length) { 
     $('html,body').animate({ 
      scrollTop: target.offset().top 
     }, 1000); 
     return false; 
     } 
    } 
    }); 
}); 
</script> 

不过,我也有在同一页幻灯片。这使用Carousel from Bootstrap。此代码与幻灯片冲突,我想禁用此平滑滚动此href

如何添加代码来防止这种冲突?

回答

3

修改您的选择器以明确排除幻灯片中的任何锚点元素。假设幻灯片与所述ID #slideshow一个元件(作为例子):

$('a[href*=#]:not([href=#], #slideshow a)').click(function() { 

虽然我个人觉得更易读使用.not()方法而不是:not()选择器(因为语法颜色高亮显示方法工作,但不在单个字符串中):

$('a[href*=#]').not("[href=#], #slideshow a").click(function() { 
+0

Yesss!谢谢。他们都解决了这个问题。 – www