dimanche 28 décembre 2014

How can I execute jquery animate scroll after page load?


I have a bootstrap 3 navbar in rails 4 with a logo on it. When the document is ready it runs:



$(document).ready ->
$("html,body").animate
scrollTop: 270
, "slow"


This properly aligns the top image in the browser.


If I click the logo in the navbar, it will scroll to the top of the page with this code:



$("#home").click (e)->
e.preventDefault()
$("html,body").animate
scrollTop: 270
, "slow"


If, however, I click on a different navbar item (say "about me") and then click on the logo, it loads the home page but it does not scroll to the proper position.


How can I ensure that the page scrolling occurs reliably every time I load the page so that the photo is always aligned initially?


Thanks!


UPDATE: Todd was absolutely right that changing .ready to .on "page:change" was a key to solving this issue. The completed will scroll to the default position or the scroll-down-link position when the links are clicked. Thanks!



$(document).on "page:change", (e)->
if $("#intro").length
e.preventDefault()
$("html,body").animate
scrollTop: 270
, "slow"

$("#home").click (e)->
e.preventDefault()
$("html,body").animate
scrollTop: 270
, "slow"


$("#scroll-down-link").click (e)->
e.preventDefault()
$("html,body").animate
scrollTop: 1300
, "slow"




Aucun commentaire:

Enregistrer un commentaire