Ui Sticky
Ui Sticky
var $ = require('jquery');
var UI = require('./core');
/**
* @via https://github.com/uikit/uikit/blob/master/src/js/addons/sticky.js
* @license https://github.com/uikit/uikit/blob/master/LICENSE.md
*/
// Sticky Class
var Sticky = function(element, options) {
var _this = this;
this.$window = $(window).
on('scroll.sticky.amui',
UI.utils.debounce($.proxy(this.checkPosition, this), 10)).
on('resize.sticky.amui orientationchange.sticky.amui',
UI.utils.debounce(function() {
_this.reset(true, function() {
_this.checkPosition();
});
}, 50)).
on('load.sticky.amui', $.proxy(this.checkPosition, this));
this.init();
};
Sticky.DEFAULTS = {
top: 0,
bottom: 0,
animation: '',
className: {
sticky: 'am-sticky',
resetting: 'am-sticky-resetting',
stickyBtm: 'am-sticky-bottom',
animationRev: 'am-animation-reverse'
}
};
Sticky.prototype.init = function() {
var result = this.check();
if (!result) {
return false;
}
$.each($element.css(
['marginTop', 'marginRight', 'marginBottom', 'marginLeft']),
function(name, value) {
return $elementMargin += ' ' + value;
});
return true;
};
this.animating = false;
this.sticked = false;
this.offset = $element.offset();
cb && cb();
}.bind(this);
$element.addClass(options.className.resetting);
this.animating = true;
$element.removeClass(animation).one(UI.support.animation.end, function() {
complete();
}).width(); // force redraw
Sticky.prototype.check = function() {
if (!this.$element.is(':visible')) {
return false;
}
if (media) {
switch (typeof(media)) {
case 'number':
if (window.innerWidth < media) {
return false;
}
break;
case 'string':
if (window.matchMedia && !window.matchMedia(media).matches) {
return false;
}
break;
}
}
return true;
};
Sticky.prototype.checkPosition = function() {
if (!this.inited) {
var initialized = this.init();
if (!initialized) {
return;
}
}
this.$holder.css({
height: $element.is(':visible') && $element.css('position') !== 'absolute' ?
$element.outerHeight() : ''
});
if (checkResult) {
$element.css({
top: offsetTop,
left: this.$holder.offset().left,
width: this.$holder.width()
});
/*
if (offsetBottom) {
// (底部边距 + 元素高度 > 窗口高度) 时定位到底部
if ((offsetBottom + this.offset.height > $(window).height()) &&
(scrollTop + $(window).height() >= scrollHeight - offsetBottom)) {
$element.addClass(options.className.stickyBtm).
css({top: $(window).height() - offsetBottom - this.offset.height});
} else {
$element.removeClass(options.className.stickyBtm).css({top: offsetTop});
}
}
*/
}
this.sticked = checkResult;
};
// Sticky Plugin
UI.plugin('sticky', Sticky);
// Init code
$(window).on('load', function() {
$('[data-am-sticky]').sticky();
});
module.exports = Sticky;