Really Simple jQuery Scroll-To Function
I've used the jQuery ScrollTo Plugin before, but I've started to dislike anchors in some scenarios - like scrolling to the bottom of a dynamically opened form on a website - so I put this together. Plus I've got a sheet of some really common functions I use and this seemed like a good fit, no plugin needed, PACKED and compressed.
The Code:
<script type="text/javascript"> function scrollTo(o,s){ var d = $(o).offset().top; $("html:not(:animated),body:not(:animated)").animate({ scrollTop: d}, s); } </script> Usage:
onclick="scrollTo('#some_div',300);" Yeah, yeah - I used an inline event call, just didn't want to muck it up - this is pretty straight-forward stuff.
Just give it an ID, class, element, etc, and a time (ms) and the rest is handled for you. I've added easing to the instance of this function that is in my common library, it's up to you if you want to go that route. Anyway, enjoy.
Responses to this Article:
Loading Comments...
I am a web developer, designer, and consultant located in the La Crosse / Onalaska Wisconsin region with
over twelve years experience developing and managing projects ranging from large applications and cloud-based
business solutions to social/new media campaigns, to complete system and infrastructure implementation.