2016-12-31 77 views
0

我有這個代碼,它運作良好。平滑滾動如何使用摘錄?

$('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; 
    } 
} 

});

但我也有一些元素,我不想爲它們應用平滑滾動!我該怎麼做 ? 示例 - http://codepen.io/zoom/pen/ggYaXZ 我不aplly希望它< li><a href="#apple">Scroll to Section Apple</a></li>

回答

0

你可以使用一些其他data-屬性:

$('a[href*="#"]:not([href="#"]):not([data-no-smooth-scroll])').click(function() { 

而且你html裏面添加data-no-smooth-scroll="true"到相關鏈接:

<a href="#apple" data-no-smooth-scroll="true">Scroll to Section Apple</a> 

以下是您的Codepen叉子:
http://codepen.io/anon/pen/dNbYKe

+0

THKS這是個好主意 – Dmi

0

您可以製作一個if語句,該語句將刪除每個具有某個類的元素(即class="notThis")的平滑滾動。

在這種情況下,您必須將該類添加到#apple鏈接元素:<a class="notThis" href="#apple">Scroll to Section Apple</a>

這將是這樣的

$('a[href*="#"]:not([href="#"])').click(function() { 
    if ($(this).attr("class") !== "notThis"){ 
    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; 
    } 
    } 
    } 
}); 
+0

感謝思想 – Dmi

+0

請給它一個點,如果你認爲這對你是有益的:) – sebasaenz