UNPKG

@egjs/visible

Version:

A class that checks if an element is visible in the base element or viewport.

121 lines (110 loc) 3.17 kB
const SUPPORT_ADDEVENTLISTENER = !!("addEventListener" in document); const SUPPORT_PASSIVE = (() => { let supportsPassiveOption = false; try { if (SUPPORT_ADDEVENTLISTENER && Object.defineProperty) { document.addEventListener("test", null, Object.defineProperty({}, "passive", { get() { supportsPassiveOption = true; }, })); } } catch (e) { } return supportsPassiveOption; })(); export function toArray(nodes) { // SCRIPT5014 in IE8 const array = []; if (nodes) { for (let i = 0, len = nodes.length; i < len; i++) { array.push(nodes[i]); } } return array; } /** * Select or create element * @param {String|HTMLElement|jQuery} param * when string given is as HTML tag, then create element * otherwise it returns selected elements * @param {Boolean} multi * @returns {HTMLElement|HTMLElement[]|undefined} */ export function $(param, multi = false) { let el; if (param === undefined) { return undefined; } if (typeof param === "string") { // String (HTML, Selector) // check if string is HTML tag format const match = param.match(/^<([a-z]+)\s*([^>]*)>/); // creating element if (match) { // HTML const dummy = document.createElement("div"); dummy.innerHTML = param; el = toArray(dummy.childNodes); } else { // Selector el = toArray(document.querySelectorAll(param)); } if (!multi) { el = el.length >= 1 ? el[0] : undefined; } } else if (param === window) { // window el = param; } else if (param.nodeName && (param.nodeType === 1 || param.nodeType === 9)) { // HTMLElement, Document el = param; } else if (("jQuery" in window && param instanceof jQuery) || param.constructor.prototype.jquery) { // jQuery el = multi ? param.toArray() : param.get(0); } else if (Array.isArray(param)) { el = param.map(v => $(v)); if (!multi) { el = el.length >= 1 ? el[0] : undefined; } } return el; } export function getWindowRect() { // [IE7] document.documentElement.clientHeight has always value 0 (bug) return { top: 0, left: 0, bottom: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight, right: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth, }; } export function hasClass(el, className) { if (el.classList) { return el.classList.contains(className); } else { return new RegExp(`(^| )${className}( |$)`, "gi").test(el.className); } } export function addEvent(element, type, handler, eventListenerOptions) { if (SUPPORT_ADDEVENTLISTENER) { let options = eventListenerOptions || false; if (typeof eventListenerOptions === "object") { options = SUPPORT_PASSIVE ? eventListenerOptions : false; } element.addEventListener(type, handler, options); } else if (element.attachEvent) { element.attachEvent(`on${type}`, handler); } else { element[`on${type}`] = handler; } } export function removeEvent(element, type, handler) { if (element.removeEventListener) { element.removeEventListener(type, handler, false); } else if (element.detachEvent) { element.detachEvent(`on${type}`, handler); } else { element[`on${type}`] = null; } }