bratsun
1/29/2014 - 8:03 AM

Smooth scroller with hash

Smooth scroller with hash

// 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);
};