Skip to content

Instantly share code, notes, and snippets.

@evild70
Last active June 6, 2016 19:03
Show Gist options
  • Save evild70/b1b342c738351e3e19cd to your computer and use it in GitHub Desktop.
Save evild70/b1b342c738351e3e19cd to your computer and use it in GitHub Desktop.
scrollToElement
// https://github.com/kswedberg/jquery-smooth-scroll
// http://www.zachstronaut.com/posts/2009/01/18/jquery-smooth-scroll-bugs.html
scrollToElement: function(id) {
// $('a[href*=#]').each(function() {
// if ($(this.hash).length) {
// $(this).click(function(event) {
var targetOffset = $(id).offset().top;
// event.preventDefault();
$(scrollElement).stop().animate(
{scrollTop: targetOffset},
500);
// });
// }
// });
}
// http://www.zachstronaut.com/posts/2009/01/18/jquery-smooth-scroll-bugs.html
var scrollElement = 'html, body';
$('html, body').each(function () {
var initScrollTop = $(this).attr('scrollTop');
$(this).attr('scrollTop', initScrollTop + 1);
if ($(this).attr('scrollTop') == initScrollTop + 1) {
scrollElement = this.nodeName.toLowerCase();
$(this).attr('scrollTop', initScrollTop);
return false;
}
});
// http://www.zachstronaut.com/posts/2009/01/18/jquery-smooth-scroll-bugs.html
function enable_smooth_scroll() {
function filterPath(string) {
return string
.replace(/^\//,'')
.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
.replace(/\/$/,'');
}
var locationPath = filterPath(location.pathname);
var scrollElement = 'html, body';
$('html, body').each(function () {
var initScrollTop = $(this).attr('scrollTop');
$(this).attr('scrollTop', initScrollTop + 1);
if ($(this).attr('scrollTop') == initScrollTop + 1) {
scrollElement = this.nodeName.toLowerCase();
$(this).attr('scrollTop', initScrollTop);
return false;
}
});
$('a[href*=#]').each(function() {
var thisPath = filterPath(this.pathname) || locationPath;
if ( locationPath == thisPath
&& (location.hostname == this.hostname || !this.hostname)
&& this.hash.replace(/#/, '')
) {
if ($(this.hash).length) {
$(this).click(function(event) {
var targetOffset = $(this.hash).offset().top;
var target = this.hash;
event.preventDefault();
$(scrollElement).animate(
{scrollTop: targetOffset},
500,
function() {
location.hash = target;
});
});
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment