Component-Builder-fork/media/uikit/js/components/sticky.js

365 lines
13 KiB
JavaScript
Raw Normal View History

2017-11-12 00:33:10 +00:00
/*! UIkit 2.27.4 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */
2016-01-30 20:28:43 +00:00
(function(addon) {
var component;
2017-11-12 00:33:10 +00:00
if (window.UIkit2) {
component = addon(UIkit2);
2016-01-30 20:28:43 +00:00
}
2017-11-12 00:33:10 +00:00
if (typeof define == 'function' && define.amd) {
define('uikit-sticky', ['uikit'], function(){
return component || addon(UIkit2);
2016-01-30 20:28:43 +00:00
});
}
})(function(UI){
"use strict";
var $win = UI.$win,
$doc = UI.$doc,
2016-03-19 01:51:35 +00:00
sticked = [],
direction = 1;
2016-01-30 20:28:43 +00:00
UI.component('sticky', {
defaults: {
top : 0,
bottom : 0,
animation : '',
clsinit : 'uk-sticky-init',
clsactive : 'uk-active',
2016-03-19 01:51:35 +00:00
clsinactive : '',
2016-01-30 20:28:43 +00:00
getWidthFrom : '',
2017-11-12 00:33:10 +00:00
showup : false,
2016-01-30 20:28:43 +00:00
boundary : false,
media : false,
target : false,
disabled : false
},
boot: function() {
// should be more efficient than using $win.scroll(checkscrollposition):
2016-03-19 01:51:35 +00:00
UI.$doc.on('scrolling.uk.document', function(e, data) {
if (!data || !data.dir) return;
direction = data.dir.y;
checkscrollposition();
});
2016-01-30 20:28:43 +00:00
UI.$win.on('resize orientationchange', UI.Utils.debounce(function() {
if (!sticked.length) return;
for (var i = 0; i < sticked.length; i++) {
sticked[i].reset(true);
2017-11-12 00:33:10 +00:00
sticked[i].self.computeWrapper();
2016-01-30 20:28:43 +00:00
}
checkscrollposition();
}, 100));
// init code
UI.ready(function(context) {
setTimeout(function(){
2017-11-12 00:33:10 +00:00
UI.$('[data-uk-sticky]', context).each(function(){
2016-01-30 20:28:43 +00:00
var $ele = UI.$(this);
2017-11-12 00:33:10 +00:00
if (!$ele.data('sticky')) {
2016-01-30 20:28:43 +00:00
UI.sticky($ele, UI.Utils.options($ele.attr('data-uk-sticky')));
}
});
checkscrollposition();
}, 0);
});
},
init: function() {
2016-03-19 01:51:35 +00:00
var boundary = this.options.boundary, boundtoparent;
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
this.wrapper = this.element.wrap('<div class="uk-sticky-placeholder"></div>').parent();
2016-01-30 20:28:43 +00:00
this.computeWrapper();
2017-11-12 00:33:10 +00:00
this.wrapper.css({
'margin-top' : this.element.css('margin-top'),
'margin-bottom' : this.element.css('margin-bottom'),
'margin-left' : this.element.css('margin-left'),
'margin-right' : this.element.css('margin-right')
})
2016-03-19 01:51:35 +00:00
this.element.css('margin', 0);
2016-01-30 20:28:43 +00:00
if (boundary) {
2016-03-19 01:51:35 +00:00
if (boundary === true || boundary[0] === '!') {
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
boundary = boundary === true ? this.wrapper.parent() : this.wrapper.closest(boundary.substr(1));
2016-01-30 20:28:43 +00:00
boundtoparent = true;
} else if (typeof boundary === "string") {
boundary = UI.$(boundary);
}
}
this.sticky = {
self : this,
options : this.options,
element : this.element,
currentTop : null,
wrapper : this.wrapper,
init : false,
2016-03-19 01:51:35 +00:00
getWidthFrom : UI.$(this.options.getWidthFrom || this.wrapper),
2016-01-30 20:28:43 +00:00
boundary : boundary,
boundtoparent : boundtoparent,
2016-03-19 01:51:35 +00:00
top : 0,
calcTop : function() {
var top = this.options.top;
// dynamic top parameter
if (this.options.top && typeof(this.options.top) == 'string') {
// e.g. 50vh
if (this.options.top.match(/^(-|)(\d+)vh$/)) {
top = window.innerHeight * parseInt(this.options.top, 10)/100;
// e.g. #elementId, or .class-1,class-2,.class-3 (first found is used)
} else {
var topElement = UI.$(this.options.top).first();
if (topElement.length && topElement.is(':visible')) {
top = -1 * ((topElement.offset().top + topElement.outerHeight()) - this.wrapper.offset().top);
}
}
}
this.top = top;
},
reset: function(force) {
this.calcTop();
2016-01-30 20:28:43 +00:00
var finalize = function() {
2017-11-12 00:33:10 +00:00
this.element.css({position:'', top:'', width:'', left:'', margin:'0'});
2016-01-30 20:28:43 +00:00
this.element.removeClass([this.options.animation, 'uk-animation-reverse', this.options.clsactive].join(' '));
2016-03-19 01:51:35 +00:00
this.element.addClass(this.options.clsinactive);
this.element.trigger('inactive.uk.sticky');
2016-01-30 20:28:43 +00:00
this.currentTop = null;
this.animate = false;
2017-11-12 00:33:10 +00:00
2016-01-30 20:28:43 +00:00
}.bind(this);
2016-03-19 01:51:35 +00:00
if (!force && this.options.animation && UI.support.animation && !UI.Utils.isInView(this.wrapper)) {
2016-01-30 20:28:43 +00:00
this.animate = true;
this.element.removeClass(this.options.animation).one(UI.support.animation.end, function(){
finalize();
}).width(); // force redraw
this.element.addClass(this.options.animation+' '+'uk-animation-reverse');
} else {
finalize();
}
},
check: function() {
if (this.options.disabled) {
return false;
}
if (this.options.media) {
switch(typeof(this.options.media)) {
case 'number':
if (window.innerWidth < this.options.media) {
return false;
}
break;
case 'string':
if (window.matchMedia && !window.matchMedia(this.options.media).matches) {
return false;
}
break;
}
}
var scrollTop = $win.scrollTop(),
documentHeight = $doc.height(),
dwh = documentHeight - window.innerHeight,
extra = (scrollTop > dwh) ? dwh - scrollTop : 0,
elementTop = this.wrapper.offset().top,
2016-03-19 01:51:35 +00:00
etse = elementTop - this.top - extra,
active = (scrollTop >= etse);
if (active && this.options.showup) {
// set inactiv if scrolling down
if (direction == 1) {
active = false;
}
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
// set inactive when wrapper is still in view
if (direction == -1 && !this.element.hasClass(this.options.clsactive) && UI.Utils.isInView(this.wrapper)) {
active = false;
}
}
return active;
2016-01-30 20:28:43 +00:00
}
};
2016-03-19 01:51:35 +00:00
this.sticky.calcTop();
2016-01-30 20:28:43 +00:00
sticked.push(this.sticky);
},
update: function() {
checkscrollposition(this.sticky);
},
enable: function() {
this.options.disabled = false;
this.update();
},
disable: function(force) {
this.options.disabled = true;
this.sticky.reset(force);
},
computeWrapper: function() {
this.wrapper.css({
2017-11-12 00:33:10 +00:00
'height' : ['absolute','fixed'].indexOf(this.element.css('position')) == -1 ? this.element.outerHeight() : '',
'float' : this.element.css('float') != 'none' ? this.element.css('float') : ''
2016-01-30 20:28:43 +00:00
});
2016-03-19 01:51:35 +00:00
if (this.element.css('position') == 'fixed') {
this.element.css({
width: this.sticky.getWidthFrom.length ? this.sticky.getWidthFrom.width() : this.element.width()
});
}
2016-01-30 20:28:43 +00:00
}
});
2016-03-19 01:51:35 +00:00
function checkscrollposition(direction) {
2016-01-30 20:28:43 +00:00
var stickies = arguments.length ? arguments : sticked;
if (!stickies.length || $win.scrollTop() < 0) return;
var scrollTop = $win.scrollTop(),
documentHeight = $doc.height(),
windowHeight = $win.height(),
dwh = documentHeight - windowHeight,
extra = (scrollTop > dwh) ? dwh - scrollTop : 0,
newTop, containerBottom, stickyHeight, sticky;
for (var i = 0; i < stickies.length; i++) {
sticky = stickies[i];
2017-11-12 00:33:10 +00:00
if (!sticky.element.is(':visible') || sticky.animate) {
2016-01-30 20:28:43 +00:00
continue;
}
if (!sticky.check()) {
if (sticky.currentTop !== null) {
sticky.reset();
}
} else {
2016-03-19 01:51:35 +00:00
if (sticky.top < 0) {
2016-01-30 20:28:43 +00:00
newTop = 0;
} else {
stickyHeight = sticky.element.outerHeight();
2016-03-19 01:51:35 +00:00
newTop = documentHeight - stickyHeight - sticky.top - sticky.options.bottom - scrollTop - extra;
newTop = newTop < 0 ? newTop + sticky.top : sticky.top;
2016-01-30 20:28:43 +00:00
}
if (sticky.boundary && sticky.boundary.length) {
2016-03-19 01:51:35 +00:00
var bTop = sticky.boundary.offset().top;
2016-01-30 20:28:43 +00:00
if (sticky.boundtoparent) {
containerBottom = documentHeight - (bTop + sticky.boundary.outerHeight()) + parseInt(sticky.boundary.css('padding-bottom'));
} else {
2017-11-12 00:33:10 +00:00
containerBottom = documentHeight - bTop;
2016-01-30 20:28:43 +00:00
}
2016-03-19 01:51:35 +00:00
newTop = (scrollTop + stickyHeight) > (documentHeight - containerBottom - (sticky.top < 0 ? 0 : sticky.top)) ? (documentHeight - containerBottom) - (scrollTop + stickyHeight) : newTop;
2016-01-30 20:28:43 +00:00
}
if (sticky.currentTop != newTop) {
sticky.element.css({
2017-11-12 00:33:10 +00:00
position : 'fixed',
2016-03-19 01:51:35 +00:00
top : newTop,
width : sticky.getWidthFrom.length ? sticky.getWidthFrom.width() : sticky.element.width()
2016-01-30 20:28:43 +00:00
});
if (!sticky.init) {
sticky.element.addClass(sticky.options.clsinit);
if (location.hash && scrollTop > 0 && sticky.options.target) {
var $target = UI.$(location.hash);
if ($target.length) {
setTimeout((function($target, sticky){
return function() {
sticky.element.width(); // force redraw
var offset = $target.offset(),
maxoffset = offset.top + $target.outerHeight(),
stickyOffset = sticky.element.offset(),
stickyHeight = sticky.element.outerHeight(),
stickyMaxOffset = stickyOffset.top + stickyHeight;
if (stickyOffset.top < maxoffset && offset.top < stickyMaxOffset) {
scrollTop = offset.top - stickyHeight - sticky.options.target;
window.scrollTo(0, scrollTop);
}
};
})($target, sticky), 0);
}
}
}
2016-03-19 01:51:35 +00:00
sticky.element.addClass(sticky.options.clsactive).removeClass(sticky.options.clsinactive);
sticky.element.trigger('active.uk.sticky');
2016-01-30 20:28:43 +00:00
sticky.element.css('margin', '');
2016-03-19 01:51:35 +00:00
if (sticky.options.animation && sticky.init && !UI.Utils.isInView(sticky.wrapper)) {
2016-01-30 20:28:43 +00:00
sticky.element.addClass(sticky.options.animation);
}
sticky.currentTop = newTop;
}
}
sticky.init = true;
}
}
return UI.sticky;
});