2013-11-09 83 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