Ich versuche, eine if/else-Anweisung einrichten, dass, wenn eine Klasse existiert dann die var num
wird gleich 100, wenn die Klasse nicht existiert dann var num
wird gleich 55. num
ist dann verwendet, um zu verstehen, wie viele Pixel der Bildschirm versetzt wird. Jede Hilfe wird geschätzt, da Javascript eine meiner Schwachstellen ist.Wenn/Else jQuery bricht mein Skript
// 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
});
Haben Sie Ihre Konsole überprüft? ... 'if (($ (". affix-top ") [0]) {) {' – mhodges
@mhodges wenn ich das in meine Konsole einfüge, gibt es 'Uncaught SyntaxError: Unexpected token {' – Cody
Das ist was sie ' erzähle es dir. Ihr Code (den sie genau kopiert haben) hat einen Syntaxfehler – Brennan