Skip to content

Instantly share code, notes, and snippets.

@fernandosavio
Last active September 26, 2018 14:49
Show Gist options
  • Save fernandosavio/3717335 to your computer and use it in GitHub Desktop.
Save fernandosavio/3717335 to your computer and use it in GitHub Desktop.
Markup Scroller
/**
* === Scroller ===
* Basta adicionar a classe "scroller" para algum elemento e atribuir o seletor ao atributo href ou data-target;
* Exemplos:
* <a href="#wrapper" class="scroller">Topo</a>
* <span class="scroller" data-target="#wrapper">Topo</span>
*/
jQuery(document).ready(function(){
var screen = $('html, window, body'),
regex = /.*(?=#[^\s]+$)/; //strip for ie7
$("body").on('click', ".scroller", function(e){
var $this = $(this),
href,
target = $this.data('target')
|| e.preventDefault()
|| (href = $this.attr('href')) && href.replace(regex, '');
screen.animate({ 'scrollTop': $(target).offset().top }, {
duration: 500,
easing: 'swing'
});
e.preventDefault();
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment