2017-06-17 31 views
0

我已經添加了一些簡單的動畫滾動到我的網站,但是在jQuery中有一個小小的怪癖。我slideDown()一個.nav-fixed導航欄,每當網站的窗口頂部是從網站的頂部導航靜態.nav。一切都很好,除非當你點擊任何鏈接(我假設任何鏈接與jQuery onClick動畫滾動功能).nav-fixed暫時隱藏,並在網站滾動後再次顯示。我是控制檯日誌記錄Should hide the nav,只要頂部靜態.nav是在窗口視圖中,並注意到,即使當我們在頁面的底部,並單擊網站上的任何onclick jQuery功能鏈接消息被記錄一次如果靜態導航在窗口視圖中。OnClick使scrollTop等於零片刻

有沒有人有任何想法如何解決? 這裏所提到的網站:http://www.mmsmsy.com 下面的代碼:

const addClickToNav = (element) => { 
    $(window.opera ? 'html' : 'html, body').animate({scrollTop: $(element).offset().top - 50}, 1000); 
} 

$('.nav-link-top').on('click',() => $(window.opera ? 'html' : 'html, body').animate({scrollTop: 0}, 1000)); 
$('.link-start').on('click',() => addClickToNav('.about')); 
$('.nav-link-skills').on('click',() => addClickToNav('.skills')); 
$('.nav-link-portfolio').on('click',() => addClickToNav('.portfolio')); 
$('.nav-link-contact').on('click',() => addClickToNav('.contact')); 

$(window).scroll(function() { 
    if ($(this).scrollTop() > $('.nav').offset().top + $('.nav').height() + 50 && $('.nav-fixed').css('display') == 'none') { 
     $('.nav-fixed') 
     .css('display', 'flex') 
     .hide() 
     .slideDown(); 
    } 
    if ($(this).scrollTop() < $('.nav').offset().top + $('.nav').height() + 50) { 
     console.log("Should hide the nav"); 
     $('.nav-fixed').slideUp(); 
    } 
}); 

回答

0

沒有ü嘗試這樣的:

$(window).scroll(function() { 
    if ($(this).scrollTop() > $('.nav').offset().top + $('.nav').height() + 50 && $('.nav-fixed').css('display') == 'none') { 
     $('.nav-fixed') 
     .css('display', 'flex') 
     .hide() 
     .slideDown(); 
    } 
    else if ($(this).scrollTop() < $('.nav').offset().top + $('.nav').height() + 50) 
    { 
     console.log("Should hide the nav"); 
     $('.nav-fixed').slideUp(); 
    } 
}); 
+0

你之前添加'''else'''關鍵字第二'''如果'''聲明?如果是這樣,那麼不,它沒有任何區別。如果我錯過了您所做的任何其他更改,請指出,因爲我沒有看到任何更改。 –