Created
January 29, 2014 08:03
-
-
Save bratsun/62ad5c4f7a6f7ea69055 to your computer and use it in GitHub Desktop.
Smooth scroller with hash
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
| // scroller for h2 titles | |
| $('.f-faqs h2').each(function() { | |
| $this = $(this); | |
| $this.attr('id',$this.text().split(' ').join('').toLowerCase()); | |
| }); | |
| var curUrl = window.location.pathname; | |
| $('a.mm-scroll').each(function() { | |
| var $this = $(this); | |
| var href = $this.attr('href'); | |
| if (curUrl.indexOf(href) !== -1) { | |
| $this.unbind(); | |
| $this.click(function(e) { | |
| e.preventDefault(); | |
| var id = $(this).attr('name'); | |
| //window.location.hash = id; | |
| var toSlide = $('h2#'+id); | |
| scroller(toSlide, -30); | |
| }); | |
| } else { | |
| $this.attr('href',$this.attr('href')+'#'+$this.attr('name')); | |
| } | |
| }); | |
| function scroller($scrTo, $off) { | |
| $.smoothScroll({ | |
| speed: 700, | |
| autoCoefficent: 2, // coefficient for "auto" speed | |
| easing: 'easeInOutCubic', | |
| afterScroll: mobileHack, | |
| offset: $off, | |
| scrollTarget: $scrTo | |
| }); | |
| } | |
| $(window).load(function(){ | |
| var hash = window.location.hash; | |
| if (hash.length > 0) { | |
| var toSlide = $('h2'+hash); | |
| scroller(toSlide, -30); | |
| } | |
| }); | |
| var $stupid = $('<div></div>') | |
| .height(1) | |
| .hide() | |
| .appendTo('body'); | |
| var mobileHack = function() { | |
| $stupid.show(); | |
| setTimeout(function() { | |
| $stupid.hide(); | |
| }, 1); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment