javascript - Fixed Header and Auto Scrolling To Div's -


i using following script make scroll section within page when clicked on it's link top navigation menu in header

so in html have assigned id's each section (sectio1, section2, section3 etc.), , have assigned these section in top navigation href's example <a href="#about">about us</a>. when click on top navigation take div have id of #about.

the scrolling bit working, however, because header fixed - when scrolls section top bit of section appears cropped since header overlaying it. click here live preview: www.loaistudio.com can this? can add below javascript make when scroll section gives link navigation class of active?

$(document).ready(function () {             $('a[href*=#]:not([href=#])').click(function() {         if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')              || location.hostname == this.hostname) {              var target = $(this.hash);             target = target.length ? target : $('[name=' + this.hash.slice(1) +']');                if (target.length) {                  $('html,body').animate({                      scrolltop: target.offset().top                 }, 700);                 return false;             }         }     });        }); 

scrolling

simple solution alter scrolltop factor in height of menu bar...

something like:

scrolltop: target.offset().top - 186 

you height dynamically if menu isn't fixed height.


active link

you having scroll event trigger , checking whether scrolltop between heights x , y home or y , z etc..

okay can use following code...

$(window).scroll(function(){     var scrollheight = $(window).scrolltop() + 186;      var welcomeoffset     = $('#welcome').offset().top;     var aboutoffset       = $('#about').offset().top;     var portfoliooffset   = $('#portfolio').offset().top;     var endorsementoffset = $('#endorsements').offset().top;     var contactoffset     = $('#contact').offset().top;      var forecolor = "777";     var backcolor = "fafafa";      $('#mainmenu li a').css({background:"#ffffff", color: "#f58221"})     if(scrollheight >= welcomeoffset && scrollheight < aboutoffset){         $('#mainmenu :nth-child(1) a').css({background:"#"+backcolor, color:"#"+forecolor})     }     if(scrollheight >= aboutoffset && scrollheight < portfoliooffset){         $('#mainmenu :nth-child(2) a').css({background:"#"+backcolor, color:"#"+forecolor})     }     if(scrollheight >= portfoliooffset && scrollheight < endorsementoffset){         $('#mainmenu :nth-child(4) a').css({background:"#"+backcolor, color:"#"+forecolor})     }     if(scrollheight >= endorsementoffset && scrollheight < contactoffset){         $('#mainmenu :nth-child(3) a').css({background:"#"+backcolor, color:"#"+forecolor})     }     if(scrollheight >= contactoffset){         $('#mainmenu :nth-child(5) a').css({background:"#"+backcolor, color:"#"+forecolor})     } }) 

the above code can go in <script>...</script> tags anywhere put other jquery put before (or after) code you've shown... i.e. above (or below)

$(document).ready(function () {.....}) 

if want test before adding site can put firefox scratchpad (have page open , press shift+f4 in firefox) copy/paste above code in , hit ctrl+r scroll , down see working.


to clarify how codes should together

$(window).scroll(function(){     var scrollheight = $(window).scrolltop() + 186;      var welcomeoffset     = $('#welcome').offset().top;     var aboutoffset       = $('#about').offset().top;     var portfoliooffset   = $('#portfolio').offset().top;     var endorsementoffset = $('#endorsements').offset().top;     var contactoffset     = $('#contact').offset().top;      var forecolor = "777";     var backcolor = "fafafa";      $('#mainmenu li a').css({background:"#ffffff", color: "#f58221"})     if(scrollheight >= welcomeoffset && scrollheight < aboutoffset){         $('#mainmenu :nth-child(1) a').css({background:"#"+backcolor, color:"#"+forecolor})     }     if(scrollheight >= aboutoffset && scrollheight < portfoliooffset){         $('#mainmenu :nth-child(2) a').css({background:"#"+backcolor, color:"#"+forecolor})     }     if(scrollheight >= portfoliooffset && scrollheight < endorsementoffset){         $('#mainmenu :nth-child(4) a').css({background:"#"+backcolor, color:"#"+forecolor})     }     if(scrollheight >= endorsementoffset && scrollheight < contactoffset){         $('#mainmenu :nth-child(3) a').css({background:"#"+backcolor, color:"#"+forecolor})     }     if(scrollheight >= contactoffset){         $('#mainmenu :nth-child(5) a').css({background:"#"+backcolor, color:"#"+forecolor})     } })   $(document).ready(function () {             $('a[href*=#]:not([href=#])').click(function() {         if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')              || location.hostname == this.hostname) {              var target = $(this.hash);             target = target.length ? target : $('[name=' + this.hash.slice(1) +']');                if (target.length) {                  $('html,body').animate({                      scrolltop: target.offset().top                 }, 700);                 return false;             }         }     });        }); 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -