javascript - Sticky navigation bar - not working -
question
i want navbar stick conditionally on scroll. there bugs can not quite diagnose, new jquery. preventing nav bar sticking conditionally on scroll?
the jquery in jsfiddle below not run correctly, , after trying awhile make work, cannot seem make run. i've looked @ other examples of i'd rather not change jquery entirely until know reason code not work. not link html full of lorem ipsum test text body. found in jsfiddle link.
what error in javascript isn't making navbar apply sticky class?
javascript
var navtop = $(".nav").offset().top; var stickynav = function(){ if ($(window).scrolltop() >= navtop){ $(".nav").addclass(".sticky") } else { $(".nav").removeclass(".sticky") } }; stickynav(); $(window).scroll(function(){ stickynav(); }; css
* { margin: 0; box-sizing: border-box; } .mainheader { width: 100%; height: 20%; background-color: rgb(62, 65, 66); text-align: center; font-family: "helvetica nue"; } .navigation { width 100%; height 10%; background-color: rgb(89, 127, 143); position: relative; } .sticky { position: fixed; }
you have bugs in js code. firstly, .nav should nav since <nav> element has class of navigation. so, we'll grab tag name instead. secondly, .addclass() , .removeclass() don't need period before class name.
here's updated javascript:
var navtop = $("nav").offset().top; var stickynav = function(){ if ($(window).scrolltop() >= navtop){ $("nav").addclass("sticky"); } else { $("nav").removeclass("sticky"); } }; stickynav(); $(window).scroll(function(){ stickynav(); }); here's updated fiddle.
Comments
Post a Comment