This repository has been archived on 2023-02-03. You can view files and clone it, but cannot push or open issues or pull requests.
autonomic.zone/js/scrollTo.js

52 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-08-31 11:44:57 +00:00
var cj = cj ? cj : {};
(function($) {
$.extend(cj, {
scrollTo: {
// jQuery DOM caching
$scrollTo: null,
// CSS selectors
scrollToSelector: '[data-scroll-to-id]',
// Classes
// Misc
speed: 500,
init: function() {
var self = this;
self.$scrollTo = $(self.scrollToSelector);
if (!self.$scrollTo || !self.$scrollTo.length) {
return;
}
self.bindEvents();
},
bindEvents: function() {
var self = this;
self.$scrollTo.on('click', function(e){
e.preventDefault();
self.scrollToId($(this));
});
},
scrollToId: function($trigger) {
var self = this,
id = $trigger.attr('href'),
offset = $trigger.attr('data-scroll-offset') || 0,
speed = $trigger.attr('data-scroll-speed') || self.speed;
// if (history.pushState && window.location.hash !== id) {
// history.pushState(null, null, id);
// }
cj.settings.$htmlbody.animate({
scrollTop: ($(id).offset().top - offset)
}, speed);
}
}
});
$.subscribe('pageReady', function() {
cj.scrollTo.init();
});
}(jQuery));