Skip to content

Instantly share code, notes, and snippets.

@Ozerich
Created March 2, 2016 20:02
Show Gist options
  • Save Ozerich/b08158c05abee909aaaf to your computer and use it in GitHub Desktop.
Save Ozerich/b08158c05abee909aaaf to your computer and use it in GitHub Desktop.
(function (global) {
'use strict';
function isHeaderFixed() {
return $('.b-header--fixed').length > 0;
}
function getOffsetY(y) {
var originalY = isHeaderFixed() ? y + 220 : y;
if (originalY > 300) {
return originalY - 300;
}
else {
return originalY - 80;
}
}
function doScroll(y) {
$('html, body').animate({
scrollTop: getOffsetY(y)
});
}
var scroll = {
scrollToY: function (y) {
doScroll(y);
},
scrollToElem: function (elem) {
var $elem;
if (typeof elem === 'string') {
$elem = $(elem);
}
else{
$elem = elem;
}
if ($elem.length === 0) {
return false;
}
return this.scrollToY($elem.first().offset().top);
},
init: function () {
var that = this;
$('body').on('click', '.js-scroll[data-target]', function () {
that.scrollToElem($(this).data('target'));
return false;
});
}
};
$(function () {
scroll.init();
});
global.Scroll = scroll;
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment