jquery - Offset this scroll-to-element function -
jquery - Offset this scroll-to-element function -
is there way can offset scroll-to function stops 80px above element it's scrolling to?
i have fixed header, 80px in height, , when scroll element header obscures of content.
function filterpath(string) { homecoming string .replace(/^\//,'') .replace(/(index|default).[a-za-z]{3,4}$/,'') .replace(/\/$/,''); } var locationpath = filterpath(location.pathname); var scrollelem = scrollableelement('html', 'body'); $('a[href*=#]').each(function() { 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; $(this).click(function(event) { event.preventdefault(); $(scrollelem).animate({scrolltop: targetoffset}, 700, function() { location.hash = target; }); }); } } }); // utilize first element "scrollable" function scrollableelement(els) { (var = 0, arglength = arguments.length; <arglength; i++) { var el = arguments[i], $scrollelement = $(el); if ($scrollelement.scrolltop()> 0) { homecoming el; } else { $scrollelement.scrolltop(1); var isscrollable = $scrollelement.scrolltop()> 0; $scrollelement.scrolltop(0); if (isscrollable) { homecoming el; } } } homecoming []; }
you should change:
var targetoffset = $target.offset().top;
to
var targetoffset = $target.offset().top - 80;
or improve yet
var targetoffset = $target.offset().top - $('#your-header-id').outerheight();
that should trick , bit more robust should alter height of header.
edit:
to round problem should alter ids of each of sections section-about
/ section-services
/ etc. have string replacement:
var targetid = $target.attr('id').replace('#','#section-'); var targetoffset = $(targetid).offset().top - 80;
$target
@ moment hash in url, #about
. above code replace #
#section-
. able carry on usual getting top offset()
of $('#section-about')
.
jquery
Comments
Post a Comment