javascript - both vertical and horizontal smooth scrolling between anchors -
i want smooth scrolling, using anchors, on same page. anchors spread out on page @ different horizontal or/and vertical levels. got code below, works scrolling vertically, , doesn't work scrolling horizontally. should make the scrolling go vertically , horizontally @ same time?
$(function() { // scroll handler var scrolltoanchor = function( id ) { // grab element scroll based on name var elem = $("a[name='"+ id +"']"); // if didn't work, element our id if ( typeof( elem.offset() ) === "undefined" ) { elem = $("#"+id); } // if destination element exists if ( typeof( elem.offset() ) !== "undefined" ) { // scroll $('html, body').animate({ scrolltop: elem.offset().top }, 1000 ); } }; // bind click event $("a").click(function( event ) { // if it's anchor link if ( $(this).attr("href").match("#") ) { // cancel default event propagation event.preventdefault(); // scroll location var href = $(this).attr('href').replace('#', '') scrolltoanchor( href ); } }); });
you should animate scrollleft property well...
$('html, body').animate({ scrolltop: elem.offset().top, scrollleft: elem.offset().left }, 1000 );
or use jquery scrollto plugin.
Comments
Post a Comment