我試圖設置一個if/else語句,如果一個類存在,那麼var num
將等於100,如果該類不存在,那麼var num
將等於55. num
然後是用於瞭解屏幕將被抵消多少像素。任何幫助將不勝感激,Javascript是我的薄弱點之一。If/Else jQuery is break my script
// Add scrollspy to <body>
$('body').scrollspy({target: ".navbar", offset: 55});
// Checks to see if navbar is affixed or not
if (($(".affix-top")[0]){) {
num = 100;
} else {
num = 55;
}
// Add smooth scrolling on all links inside the navbar
$(".navbar-lower a").on('click', function(event) {
// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (300) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top - num
}, 300, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
您是否檢查過控制檯? ... if(($(「。affix-top」)[0]){){' – mhodges
@mhodges當我粘貼到我的控制檯,它返回'未捕獲的SyntaxError:意外的令牌{' – Cody
這就是他們'告訴你。你的代碼(他們完全複製)有一個語法錯誤 – Brennan