javascript - How to go to a specific element on page? -
this question has answer here:
on html page, want able 'go to' / 'scroll to' / 'focus on' element on page.
normally, i'd use anchor tag href="#something"
, i'm using hashchange event along bbq plugin load page.
so there other way, via javascript, have page go given element on page?
here's basic outline of i'm trying do:
function focusonelement(element_id) { $('#div_' + element_id).goto(); // need 'go to' element } <div id="div_element1"> yadda yadda </div> <div id="div_element2"> blah blah </div> <span onclick="focusonelement('element1');">click here go element 1</span> <span onclick="focusonelement('element2');">click here go element 2</span>
the standard technique in plugin form this:
(function($) { $.fn.goto = function() { $('html, body').animate({ scrolltop: $(this).offset().top + 'px' }, 'fast'); return this; // chaining... } })(jquery);
then $('#div_element2').goto();
scroll <div id="div_element2">
. options handling , configurability left exercise reader.
Comments
Post a Comment