Pure onclick scrollTo

user2988257 picture user2988257 · Nov 17, 2013 · Viewed 14.4k times · Source

I am trying to invoke the scrollTo function directly from onclick action. Found many solutions but none of them not worked form me

I am trying to do this:

<button onclick="$(window).scrollTo('#mydiv',100);"type="button"  placeholder="" />CLICK ME</button>

<div id="mydiv">
</div>

Answer

Drew picture Drew · Nov 17, 2013

Try

<button onclick="$('html, body').animate({scrollTop: $('#mydiv').offset().top}, 2000);" type="button"  placeholder="" />CLICK ME</button>

<div id="mydiv"></div>

Make sure you use jQuery 1.9.x