簡體   English   中英

jQuery滾動到錨點異常

[英]jQuery scroll to anchor with exception

朋友們,

我正在構建單頁網站,當選擇菜單鏈接時,它使用jQuery函數滾動到錨點。 這是我使用的代碼:

(function($) {
    var jump = function(e)
    {
        if (e) {
            e.preventDefault();
            var target = $(this).attr("href");
        } else {
            var target = location.hash;
        }
        $('html,body').animate(
        {
            scrollTop: $(target).offset().top - 150
        }, 1500, 'swing', function()
        {
            location.hash = target - 150;
        });
    }
    $('html, body').hide()
    $(document).ready(function()
    {
        $('a[href^=#]').bind("click", jump);
        if (location.hash) {
            setTimeout(function() {
                $('html, body').scrollTop(0).show()
                jump()
            }, 0);
        } else {
            $('html, body').show()
        }
    });
})(jQuery)

現在,所有帶有'href'的html'a'元素都會調用此函數。 我需要修改上面的函數,所以它適用於所有已定義的鏈接,除了這個帶錨#nav-menu

 <a href="#nav-menu" id="toggle"><span></span></a>

任何建議將非常感激。

Jquery提供了一組內置過濾器,您可以在您使用的情況下使用它們:

  • 內置過濾器not()如下: -

     $("a[href^=#]:not([href=#nav-menu])").click(jump); 
  • 按如下方式構建您自己的業務過濾器: -

     $("a[href^=#]").filter(function() { //you may here do whatever filteration business you want return $(this).attr("href")!='#nav-menu'; }).click(jump); 

這里簡單的例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM