javascript - jquery scroll to element then lock to that element regardless of changes on window size -


i'm trying use jquery scroll specific element inside of wrapping element. works intended work, long screen width isn't resized. screen width changes, scroll doesn't stick. here current code:

javascript

function pageinit(pageid) {     $('.page-wrapper').animate({ scrollleft: $('#' + pageid).offset().left }, 500);     $('#' + pageid).css('postion', 'fixed');     console.debug('page #' + pageid + ' initiated.'); }  pageinit('about'); 

html

<div class="page-wrapper"> <div class="page main-page" id="root" data-pos="0">     <div class="group-container">         <div class="book"></div>         <div class="promo">             <div class="button">                 <a href="/about">                 <div class="button-logo cover-button-logo"></div>                 <div class="button-text">read summary</div>                 </a>             </div>              <div class="button">                 <a href="#" onclick="return false;">                 <div class="button-logo yt-button-logo"></div>                 <div class="button-text">watch trailer</div>                 </a>             </div>              <div class="button">                 <a href="#" onclick="return false;">                 <div class="button-logo wattpad-button-logo"></div>                 <div class="button-text">coming on wattpad</div>                 </a>             </div>              <div class="button">                 <a href="//localhost/internals/close_tab" target="twitter">                 <div class="button-logo twitter-button-logo"></div>                 <div class="button-text">#ghifari160sbeingtheone</div>                 </a>             </div>              <div class="button">                 <a href="//localhost/internals/close_tab" target="facebook">                 <div class="button-logo facebook-button-logo"></div>                 <div class="button-text">share on facebook</div>                 </a>             </div>         </div>     </div> </div>  <div class="page about-page" id="about" data-pos="100">     <div class="group-container">         <div class="summary">             %summary%         </div>         <div class="promo">             <div class="button">                 <a href="#" onclick="return false;">                 <div class="button-logo yt-button-logo"></div>                 <div class="button-text">watch trailer</div>                 </a>             </div>              <div class="button">                 <a href="#" onclick="return false;">                 <div class="button-logo wattpad-button-logo"></div>                 <div class="button-text">coming on wattpad</div>                 </a>             </div>              <div class="button">                 <a href="//localhost/internals/close_tab" target="twitter">                 <div class="button-logo twitter-button-logo"></div>                 <div class="button-text">#ghifari160sbeingtheone</div>                 </a>             </div>              <div class="button">                 <a href="//localhost/internals/close_tab" target="facebook">                 <div class="button-logo facebook-button-logo"></div>                 <div class="button-text">share on facebook</div>                 </a>             </div>         </div>     </div> </div> 

the wrapping element should scrolled right until element id of "about" fills entire screen. current code works part. need find way same thing when screen width changes, way "about" element fills entire screen. i've tried checking every few seconds , rescroll element if screen width change, fails every once in awhile. there way can make browser scroll element intended scroll to, rescroll element after detecting changes on screen width?

edit: jsfiddle here: https://jsfiddle.net/ghifari160/sy3qgtqe/1/

the question difficult answer since there's no way see results without fiddle or pen. but, considering you're needing trigger event on window resize, should @ window.resize global event handler.

window.resize event handler other, use pageinit() function callback parameter handler. make sure still call function initial time, otherwise function won't run until window has been resized.

edit: might add example clarity.

window.onresize = pageinit('about'); 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -