-
Notifications
You must be signed in to change notification settings - Fork 0
/
ponzo-vanilla.js
87 lines (75 loc) · 2.69 KB
/
ponzo-vanilla.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//some plain javascript funnctions
//https://gomakethings.com/how-to-get-the-closest-parent-element-with-a-matching-selector-using-vanilla-javascript/
export function getEach(selector, context) {
context = context || document
var elements = context.querySelectorAll(selector)
return Array.prototype.slice.call(elements)
}
export function removeClass(selector, remclass) {
var items = document.querySelectorAll('.' + selector)
for (var i in items) {
var item = items[i]
if (item.nodeName) {
item.classList.remove(remclass)
}
}
}
//offset element
export function getOffset(elem) {
// crossbrowser version
var box = elem.getBoundingClientRect()
var body = document.body
var docEl = document.documentElement
var scrollTop = window.pageYOffset || docEl.scrollTop || body.scrollTop
var scrollLeft = window.pageXOffset || docEl.scrollLeft || body.scrollLeft
var clientTop = docEl.clientTop || body.clientTop || 0
var clientLeft = docEl.clientLeft || body.clientLeft || 0
var top = box.top + scrollTop - clientTop
var left = box.left + scrollLeft - clientLeft
return { top: Math.round(top), left: Math.round(left) }
}
export function scrollBy(distance, duration) {
var initialY = document.body.scrollTop
var y = initialY + distance
var baseY = (initialY + y) * 0.5
var difference = initialY - baseY
var startTime = performance.now()
function step() {
var normalizedTime = (performance.now() - startTime) / duration
if (normalizedTime > 1) normalizedTime = 1
window.scrollTo(0, baseY + difference * Math.cos(normalizedTime * Math.PI))
if (normalizedTime < 1) window.requestAnimationFrame(step)
}
window.requestAnimationFrame(step)
}
// find any elements
export function getChild(parent, selector) {
const foundElements = parent.querySelectorAll(selector)
let labelElem = false
for (let f = 0, foundlen = foundElements.length; f < foundlen; f++) {
labelElem = foundElements[f]
}
return labelElem
}
export 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
}
}
// Get the closest matching element
for (; elem && elem !== document; elem = elem.parentNode) {
if (elem.matches(selector)) return elem
}
return null
}