Created
August 20, 2020 05:18
-
-
Save hasanm95/6b8d57b5371c30db6ac47e330f1e96d3 to your computer and use it in GitHub Desktop.
jQuery like functions by vanila javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Is element in viewport | |
// return boolean | |
function isInViewport(el) { | |
const rect = el.getBoundingClientRect(); | |
return ( | |
rect.top >= 0 && | |
rect.left >= 0 && | |
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && | |
rect.right <= (window.innerWidth || document.documentElement.clientWidth) | |
); | |
} | |
//Get element's siblings | |
function getSiblings(elem) { | |
var siblings = []; | |
var sibling = elem.parentNode.firstChild; | |
while (sibling) { | |
if (sibling.nodeType === 1 && sibling !== elem) { | |
siblings.push(sibling); | |
} | |
sibling = sibling.nextSibling; | |
} | |
return siblings; | |
}; | |
//Get element's sibling by class name | |
function getSiblingByClass(elem, target) { | |
var sibling; | |
var parent = elem.parentNode; | |
var children = parent.children; | |
for (var i = 0; i < children.length; i++) { | |
if (children[i].className === target) { | |
sibling = children[i]; | |
break; | |
} | |
} | |
return sibling; | |
} | |
//Get element's closest item | |
function getClosest(elem, selector) { | |
// Element.matches() polyfill | |
if (!Element.prototype.matches) { | |
Element.prototype.matches = | |
Element.prototype.matchesSelector || | |
Element.prototype.mozMatchesSelector || | |
Element.prototype.msMatchesSelector || | |
Element.prototype.oMatchesSelector || | |
Element.prototype.webkitMatchesSelector || | |
function (s) { | |
var matches = (this.document || this.ownerDocument).querySelectorAll(s), | |
i = matches.length; | |
while (--i >= 0 && matches.item(i) !== this) { } | |
return i > -1; | |
}; | |
} | |
for (; elem && elem !== document; elem = elem.parentNode) { | |
if (elem.matches(selector)) return elem; | |
} | |
return null; | |
}; | |
//jQuery Slide up effect | |
function slideUp(element, duration = 500) { | |
return new Promise(function (resolve, reject) { | |
element.style.height = element.offsetHeight + 'px'; | |
element.style.transitionProperty = `height, margin, padding`; | |
element.style.transitionDuration = duration + 'ms'; | |
element.offsetHeight; | |
element.style.overflow = 'hidden'; | |
element.style.height = 0; | |
element.style.paddingTop = 0; | |
element.style.paddingBottom = 0; | |
element.style.marginTop = 0; | |
element.style.marginBottom = 0; | |
window.setTimeout(function () { | |
element.style.display = 'none'; | |
element.style.removeProperty('height'); | |
element.style.removeProperty('padding-top'); | |
element.style.removeProperty('padding-bottom'); | |
element.style.removeProperty('margin-top'); | |
element.style.removeProperty('margin-bottom'); | |
element.style.removeProperty('overflow'); | |
element.style.removeProperty('transition-duration'); | |
element.style.removeProperty('transition-property'); | |
resolve(false); | |
}, duration) | |
}) | |
} | |
//jQuery Slide down effect | |
function slideDown(element, duration = 500) { | |
return new Promise(function (resolve, reject) { | |
element.style.removeProperty('display'); | |
let display = window.getComputedStyle(element).display; | |
if (display === 'none') | |
display = 'block'; | |
element.style.display = display; | |
let height = element.offsetHeight; | |
element.style.overflow = 'hidden'; | |
element.style.height = 0; | |
element.style.paddingTop = 0; | |
element.style.paddingBottom = 0; | |
element.style.marginTop = 0; | |
element.style.marginBottom = 0; | |
element.offsetHeight; | |
element.style.transitionProperty = `height, margin, padding`; | |
element.style.transitionDuration = duration + 'ms'; | |
element.style.height = height + 'px'; | |
element.style.removeProperty('padding-top'); | |
element.style.removeProperty('padding-bottom'); | |
element.style.removeProperty('margin-top'); | |
element.style.removeProperty('margin-bottom'); | |
window.setTimeout(function () { | |
element.style.removeProperty('height'); | |
element.style.removeProperty('overflow'); | |
element.style.removeProperty('transition-duration'); | |
element.style.removeProperty('transition-property'); | |
}, duration) | |
}) | |
} | |
//jQuery Slide toggle effect | |
function slideToggle(element, duration = 500) { | |
if (window.getComputedStyle(element).display === 'none') { | |
return slideDown(element, duration); | |
} else { | |
return slideUp(element, duration); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment