Last active
August 29, 2015 14:12
-
-
Save RyoSugimoto/78d7c7e2619c4a9d81b2 to your computer and use it in GitHub Desktop.
ページ内リンクを、スムーズにスクロールさせるプラグイン。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* スムーズスクロール | |
*/ | |
!(function (window, document, $, undefined) { | |
var isHtmlScroll = (function () { | |
var $html = $('html'), | |
top = $html.scrollTop(), | |
$el = $('<div/>').height(10000).prependTo('body'); | |
$html.scrollTop(10000); | |
var rs = !!$html.scrollTop(); | |
$html.scrollTop(top); | |
$el.remove(); | |
return rs; | |
}()); | |
var $scrollElm = $(isHtmlScroll ? 'html' : 'body'); | |
/* | |
if (navigator.userAgent.indexOf('AppleWebKit') !== -1) { | |
$scrollElm = $('body'); | |
} else { | |
$scrollElm = $('html'); | |
} | |
*/ | |
$.fn.smoothScroll = function (options) { | |
options = $.extend({}, { | |
duration: 1000, | |
easing: 'linear', | |
hash: true, | |
afterScroll: function () { | |
$.noop(); | |
} | |
}, options) | |
return this.each(function () { | |
var $anchor = $(this); | |
var hash = $anchor.attr('href'); | |
var $target = $(hash); | |
if (!$target.length) { | |
return true; | |
} | |
$anchor.on('click', function (e) { | |
var point = $target.offset().top; | |
e.preventDefault(); | |
$scrollElm.animate({ | |
scrollTop: point | |
}, options.duration, options.easing, function () { | |
if (options.hash) { | |
window.location.hash = hash; | |
} | |
if (typeof options.afterScroll === 'function') { | |
options.afterScroll(); | |
} | |
}); // /$scrollElm.animate | |
}); // /$anchor.on | |
}); // /return this.each | |
}; // /$.fn.smoothScroll | |
}(window, document, jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment