Created
May 27, 2014 11:07
-
-
Save torounit/b90ff4cbb62474ae4596 to your computer and use it in GitHub Desktop.
ページ内リンクをスムーススクロールにするScript
This file contains 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
do($=jQuery) => | |
class pageLink | |
constructor:($el) -> | |
@$el = $el; | |
@on(); | |
on: -> | |
@$el.on "click", "a", @click.bind(@) | |
click:(event) -> | |
if event.currentTarget.href.indexOf(location.origin) > -1 and event.currentTarget.href.indexOf("#") > -1 | |
href = event.currentTarget.href; | |
hash = "#" + href.split("#").pop() | |
pathname = href.split("#").shift() | |
if pathname == location.origin + location.pathname | |
event.preventDefault(); | |
offsetTop = $(hash).offset().top; | |
$("html,body").animate({scrollTop: offsetTop},500).promise().done ()-> | |
location.hash = hash | |
window.$.fn.pageLink = -> | |
@.each -> | |
new pageLink($(@)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
とかすると、body内のaを監視し、同一ページかつ、"#"がURLに含まれていれば、スムーススクロールに変更します。?がついているURLには対応していません。