javascript - Scroll to div then fix it to top of page -


i have div (#sticky) within right bar of page , fix top of page once scroll it. div (#sticky) want fixed top of page sits 1000px down page.

html:

<div id="right-bar">   <div id="sticky-anchor"></div>   <div id="sticky"></div> </div> 

css:

#right-bar {   display: inline-block;   float: left;   width: 336px;   height: 10000px;   margin-left: 15px; }  #sticky {   display: block;   width: 334px;   height: 600px;   background-color: white;   border: 1px solid #e5e5e5;   margin-top: 15px; }  #sticky .stick {   position: fixed;   top: 0;   z-index: 10000; } 

javascript: (within head tags)

<script>   function sticky_relocate() {   var window_top = $(window).scrolltop();   var div_top = $('#sticky-anchor').offset().top;   if (window_top > div_top) {     $('#sticky').addclass('stick');   } else {     $('#sticky').removeclass('stick');   } }  $(function () {   $(window).scroll(sticky_relocate);   sticky_relocate(); }); </script> 

not quite sure doing wrong, once scroll div, bypass without getting stuck top of page.

any , appreciated!

you have css typo, instead of #sticky .stick use #sticky.stick (same thing without space).


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -