element-in-view
Version:
Check if an element is in viewport.
43 lines (35 loc) • 1.21 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.elementInView = factory());
}(this, (function () { 'use strict';
var index = function (element, ref) {
if ( ref === void 0 ) ref = {};
var offset = ref.offset; if ( offset === void 0 ) offset = 0;
var threshold = ref.threshold; if ( threshold === void 0 ) threshold = 0;
var ref$1 = element.getBoundingClientRect();
var top = ref$1.top;
var right = ref$1.right;
var bottom = ref$1.bottom;
var left = ref$1.left;
var width = ref$1.width;
var height = ref$1.height;
var intersection = {
t: bottom,
r: window.innerWidth - left,
b: window.innerHeight - top,
l: right
};
var elementThreshold = {
x: threshold * width,
y: threshold * height
};
return (
intersection.t >= (offset.top || offset + elementThreshold.y) &&
intersection.r >= (offset.right || offset + elementThreshold.x) &&
intersection.b >= (offset.bottom || offset + elementThreshold.y) &&
intersection.l >= (offset.left || offset + elementThreshold.x)
)
};
return index;
})));