Last active
August 29, 2015 14:06
-
-
Save tanshio/0a8e06ea8dfdc374d471 to your computer and use it in GitHub Desktop.
smoothscroll.js
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 checkData(anchor){ | |
| var checked = false; | |
| for (var k = 0; k < options.noData.length; k++) { | |
| if(anchor.hasAttribute(options.noData[k])){ | |
| checked = true; | |
| return checked; | |
| } | |
| } | |
| return checked; | |
| } |
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
| if(e.target.localName!=='a'){ | |
| //hrefがなかったらある親まで遡る | |
| var parent = e.target.parentNode; | |
| while(parent.localName!=='a'){ | |
| parent = parent.parentNode; | |
| } | |
| targetURL = parent.href; | |
| targetAnchor = parent; | |
| } | |
| if(targetAnchor.hasAttribute('target')){ | |
| return; | |
| } | |
| e.preventDefault(); | |
| if(checkClass(targetAnchor.className)||checkData(targetAnchor)){ | |
| return; | |
| } | |
| if(thisURL.split('#')[0] === targetURL.split('#')[0] && e.target.hash){ | |
| smoothScroll(e.target.hash); | |
| }else{ | |
| location.href=targetURL; | |
| } |
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
| //t=now-date | |
| //b=最初の値 | |
| //c=最後の値 | |
| //d=何秒 | |
| Math.ease = function (t, b, c, d) { | |
| t /= d/2; | |
| if (t < 1) return c/2*t*t*t + b; | |
| t -= 2; | |
| return c/2*(t*t*t + 2) + b; | |
| }; | |
| function smoothScroll(hash){ | |
| var settings = { | |
| duration :1500, | |
| breakpoint:992, | |
| plus :70 | |
| }; | |
| var date = new Date(), | |
| pos = document.documentElement.scrollTop || document.body.scrollTop, | |
| end = document.getElementById(hash.split('#')[1]).offsetTop, | |
| _time, | |
| if(settings.breakpoint < window.innerWidth){ | |
| settings.plus = 0; | |
| } | |
| end -= settings.plus; | |
| function time(){ | |
| var now = new Date(); | |
| if(now-date<settings.duration){ | |
| _time = setTimeout(function(){ | |
| var scroll = Math.ease(now-date,pos,end-pos,settings.duration); | |
| window.scrollTo(0,parseInt(scroll,10)); | |
| time(); | |
| },0); | |
| }else{ | |
| clearTimeout(_time); | |
| } | |
| } | |
| time(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment