jQuery Smooth Scrolling on Page Load -
i'm using jquery script smooth scrolling (found on css-tricks.com):
/** smooth scrolling functionality **/ jquery(document).ready(function($) { function filterpath(string) { return string .replace(/^\//,'') .replace(/(index|default).[a-za-z]{3,4}$/,'') .replace(/\/$/,''); } var locationpath = filterpath(location.pathname); var scrollelem = scrollableelement('html', 'body'); var urlhash = '#' + window.location.href.split("#")[1]; $('a[href*=#]').each(function() { $(this).click(function(event) { var thispath = filterpath(this.pathname) || locationpath; if ( locationpath == thispath && (location.hostname == this.hostname || !this.hostname) && this.hash.replace(/#/,'') ) { var $target = $(this.hash), target = this.hash; if (target) { var targetoffset = $target.offset().top; event.preventdefault(); $(scrollelem).animate({scrolltop: targetoffset}, 400, function() { location.hash = target; }); } } }); }); // use first element "scrollable" function scrollableelement(els) { (var = 0, arglength = arguments.length; <arglength; i++) { var el = arguments[i], $scrollelement = $(el); if ($scrollelement.scrolltop()> 0) { return el; } else { $scrollelement.scrolltop(1); var isscrollable = $scrollelement.scrolltop()> 0; $scrollelement.scrolltop(0); if (isscrollable) { return el; } } } return []; } }); /** end smooth scrolling functionality **/
it works fantastic, except 1 thing, want work if goes directly url e.g. http://domain.com/page.html#anchor
smooth scrolls anchor top on page load, right goes page anchor unless they've clicked on anchor. hope makes sense.
if not late answer here go.... here modification of psr's code works smooth scrolling of page on load:
http://jsfiddle.net/9sdlw/1425/
$(function(){ $('html, body').animate({ scrolltop: $( $('#anchor1').attr('href') ).offset().top }, 2000); return false; });
better version:
http://jsfiddle.net/9sdlw/1432/
$(function(){ $('html, body').animate({ scrolltop: $('.myclass').offset().top }, 2000); return false; });
all need in script replace "myclass" class or id of control located on page want scroll to.
naveed
Comments
Post a Comment