-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
137 additions
and
135 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,163 +1,165 @@ | ||
function registerEventListeners() { | ||
var elements = document.querySelectorAll('.purecounter'); | ||
var intersectionSupported = intersectionListenerSupported(); | ||
|
||
if (intersectionSupported) { | ||
var intersectionObserver = new IntersectionObserver(animateElements, { | ||
"root": null, | ||
"rootMargin": '20px', | ||
"threshold": 0.5 | ||
}); | ||
export default class PureCounter { | ||
constructor() { | ||
this.registerEventListeners(); | ||
} | ||
|
||
for (var i = 0; i < elements.length; i++) { | ||
intersectionObserver.observe(elements[i]); | ||
} | ||
} else { | ||
if (window.addEventListener) { | ||
animateLegacy(elements); | ||
registerEventListeners() { | ||
var elements = document.querySelectorAll('.purecounter'); | ||
var intersectionSupported = this.intersectionListenerSupported(); | ||
|
||
if (intersectionSupported) { | ||
var intersectionObserver = new IntersectionObserver(animateElements, { | ||
"root": null, | ||
"rootMargin": '20px', | ||
"threshold": 0.5 | ||
}); | ||
|
||
window.addEventListener('scroll', function (e) { | ||
animateLegacy(elements); | ||
}, { "passive": true }); | ||
for (var i = 0; i < elements.length; i++) { | ||
intersectionObserver.observe(elements[i]); | ||
} | ||
} else { | ||
if (window.addEventListener) { | ||
this.animateLegacy(elements); | ||
|
||
window.addEventListener('scroll', function (e) { | ||
this.animateLegacy(elements); | ||
}, { "passive": true }); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function animateLegacy(elements) { | ||
for (var i = 0; i < elements.length; i++) { | ||
var config = parseConfig(elements[i]); | ||
if (config.legacy === true && elementIsInView(elements[i])) { | ||
animateElements([elements[i]]); | ||
animateLegacy(elements) { | ||
for (var i = 0; i < elements.length; i++) { | ||
var config = this.parseConfig(elements[i]); | ||
if (config.legacy === true && this.elementIsInView(elements[i])) { | ||
this.animateElements([elements[i]]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function animateElements(elements, observer) { | ||
elements.forEach(function (element) { | ||
var elementConfig = typeof element.target !== "undefined" ? parseConfig(element.target) : parseConfig(element); | ||
animateElements(elements, observer) { | ||
elements.forEach(function (element) { | ||
var elementConfig = typeof element.target !== "undefined" ? this.parseConfig(element.target) : this.parseConfig(element); | ||
|
||
if (elementConfig.duration <= 0) { | ||
return element.innerHTML = elementConfig.end.toFixed(elementConfig.decimals); | ||
} | ||
if ((!observer && !elementIsInView(element)) || (observer && element.intersectionRatio < 0.5)) { | ||
return element.target.innerHTML = elementConfig.start > elementConfig.end ? elementConfig.end : elementConfig.start; | ||
} | ||
|
||
setTimeout(function () { | ||
if (typeof element.target !== "undefined") { | ||
return startCounter(element.target, elementConfig); | ||
if (elementConfig.duration <= 0) { | ||
return element.innerHTML = elementConfig.end.toFixed(elementConfig.decimals); | ||
} | ||
if ((!observer && !this.elementIsInView(element)) || (observer && element.intersectionRatio < 0.5)) { | ||
return element.target.innerHTML = elementConfig.start > elementConfig.end ? elementConfig.end : elementConfig.start; | ||
} | ||
|
||
return startCounter(element, elementConfig); | ||
}, elementConfig.delay); | ||
}); | ||
} | ||
|
||
function startCounter(element, config) { | ||
var incrementsPerStep = (config.end - config.start) / (config.duration / config.delay); | ||
var countMode = 'inc'; | ||
if (config.start > config.end) { | ||
countMode = 'dec'; | ||
incrementsPerStep *= -1; | ||
} | ||
if (incrementsPerStep < 1 && config.decimals <= 0) { | ||
incrementsPerStep = 1; | ||
setTimeout(function () { | ||
if (typeof element.target !== "undefined") { | ||
return this.startCounter(element.target, elementConfig); | ||
} | ||
|
||
return this.startCounter(element, elementConfig); | ||
}, elementConfig.delay); | ||
}); | ||
} | ||
|
||
var currentCount = config.decimals <= 0 ? parseInt(config.start) : parseFloat(config.start).toFixed(config.decimals); | ||
element.innerHTML = currentCount; | ||
startCounter(element, config) { | ||
var incrementsPerStep = (config.end - config.start) / (config.duration / config.delay); | ||
var countMode = 'inc'; | ||
if (config.start > config.end) { | ||
countMode = 'dec'; | ||
incrementsPerStep *= -1; | ||
} | ||
if (incrementsPerStep < 1 && config.decimals <= 0) { | ||
incrementsPerStep = 1; | ||
} | ||
|
||
var currentCount = config.decimals <= 0 ? parseInt(config.start) : parseFloat(config.start).toFixed(config.decimals); | ||
element.innerHTML = currentCount; | ||
|
||
if (config.once === true) { | ||
element.setAttribute('data-purecounter-duration', 0); | ||
} | ||
if (config.once === true) { | ||
element.setAttribute('data-purecounter-duration', 0); | ||
} | ||
|
||
var counterWorker = setInterval(function () { | ||
var nextNum = nextNumber(currentCount, incrementsPerStep, config, countMode); | ||
element.innerHTML = formatNumber(nextNum, config); | ||
currentCount = nextNum; | ||
var counterWorker = setInterval(function () { | ||
var nextNum = this.nextNumber(currentCount, incrementsPerStep, config, countMode); | ||
element.innerHTML = this.formatNumber(nextNum, config); | ||
currentCount = nextNum; | ||
|
||
if ((currentCount >= config.end && countMode == 'inc') || (currentCount <= config.end && countMode == 'dec')) { | ||
clearInterval(counterWorker); | ||
if ((currentCount >= config.end && countMode == 'inc') || (currentCount <= config.end && countMode == 'dec')) { | ||
clearInterval(counterWorker); | ||
|
||
if (currentCount != config.end) { | ||
element.innerHTML = config.decimals <= 0 ? parseInt(config.end) : parseFloat(config.end).toFixed(config.decimals); | ||
if (currentCount != config.end) { | ||
element.innerHTML = config.decimals <= 0 ? parseInt(config.end) : parseFloat(config.end).toFixed(config.decimals); | ||
} | ||
} | ||
} | ||
}, config.delay); | ||
} | ||
|
||
function parseConfig(element) { | ||
var configValues = [].filter.call(element.attributes, function (attribute) { | ||
return /^data-purecounter-/.test(attribute.name); | ||
}); | ||
|
||
var newConfig = { | ||
start: 0, | ||
end: 9001, | ||
duration: 2000, | ||
delay: 10, | ||
once: true, | ||
decimals: 0, | ||
legacy: true, | ||
}; | ||
|
||
for (var i = 0; i < configValues.length; i++) { | ||
var valueInd = configValues[i].name.replace('data-purecounter-', ''); | ||
newConfig[valueInd.toLowerCase()] = valueInd.toLowerCase() == 'duration' ? parseInt(castDataType(configValues[i].value) * 1000) : castDataType(configValues[i].value); | ||
}, config.delay); | ||
} | ||
|
||
return newConfig; | ||
} | ||
parseConfig(element) { | ||
var configValues = [].filter.call(element.attributes, function (attribute) { | ||
return /^data-purecounter-/.test(attribute.name); | ||
}); | ||
|
||
var newConfig = { | ||
start: 0, | ||
end: 9001, | ||
duration: 2000, | ||
delay: 10, | ||
once: true, | ||
decimals: 0, | ||
legacy: true, | ||
}; | ||
|
||
for (var i = 0; i < configValues.length; i++) { | ||
var valueInd = configValues[i].name.replace('data-purecounter-', ''); | ||
newConfig[valueInd.toLowerCase()] = valueInd.toLowerCase() == 'duration' ? parseInt(this.castDataType(configValues[i].value) * 1000) : this.castDataType(configValues[i].value); | ||
} | ||
|
||
function nextNumber(number, steps, config, mode) { | ||
if (!mode) mode = 'inc'; | ||
if (mode === 'inc') { | ||
return config.decimals <= 0 ? parseInt(number) + parseInt(steps) : parseFloat(number) + parseFloat(steps); | ||
return newConfig; | ||
} | ||
|
||
return config.decimals <= 0 ? parseInt(number) - parseInt(steps) : parseFloat(number) - parseFloat(steps); | ||
} | ||
nextNumber(number, steps, config, mode) { | ||
if (!mode) mode = 'inc'; | ||
if (mode === 'inc') { | ||
return config.decimals <= 0 ? parseInt(number) + parseInt(steps) : parseFloat(number) + parseFloat(steps); | ||
} | ||
|
||
function formatNumber(number, config) { | ||
return config.decimals <= 0 ? parseInt(number) : number.toLocaleString(undefined, { minimumFractionDigits: config.decimals, maximumFractionDigits: config.decimals }); | ||
} | ||
return config.decimals <= 0 ? parseInt(number) - parseInt(steps) : parseFloat(number) - parseFloat(steps); | ||
} | ||
|
||
function castDataType(data) { | ||
if (/^[0-9]+\.[0-9]+$/.test(data)) { | ||
return parseFloat(data); | ||
formatNumber(number, config) { | ||
return config.decimals <= 0 ? parseInt(number) : number.toLocaleString(undefined, { minimumFractionDigits: config.decimals, maximumFractionDigits: config.decimals }); | ||
} | ||
if (/^[0-9]+$/.test(data)) { | ||
return parseInt(data); | ||
|
||
castDataType(data) { | ||
if (/^[0-9]+\.[0-9]+$/.test(data)) { | ||
return parseFloat(data); | ||
} | ||
if (/^[0-9]+$/.test(data)) { | ||
return parseInt(data); | ||
} | ||
return data; | ||
} | ||
return data; | ||
} | ||
|
||
function elementIsInView(element) { | ||
var top = element.offsetTop; | ||
var left = element.offsetLeft; | ||
var width = element.offsetWidth; | ||
var height = element.offsetHeight; | ||
|
||
while (element.offsetParent) { | ||
element = element.offsetParent; | ||
top += element.offsetTop; | ||
left += element.offsetLeft; | ||
|
||
elementIsInView(element) { | ||
var top = element.offsetTop; | ||
var left = element.offsetLeft; | ||
var width = element.offsetWidth; | ||
var height = element.offsetHeight; | ||
|
||
while (element.offsetParent) { | ||
element = element.offsetParent; | ||
top += element.offsetTop; | ||
left += element.offsetLeft; | ||
} | ||
|
||
return ( | ||
top >= window.pageYOffset && | ||
left >= window.pageXOffset && | ||
(top + height) <= (window.pageYOffset + window.innerHeight) && | ||
(left + width) <= (window.pageXOffset + window.innerWidth) | ||
); | ||
} | ||
|
||
return ( | ||
top >= window.pageYOffset && | ||
left >= window.pageXOffset && | ||
(top + height) <= (window.pageYOffset + window.innerHeight) && | ||
(left + width) <= (window.pageXOffset + window.innerWidth) | ||
); | ||
} | ||
|
||
function intersectionListenerSupported() { | ||
return ('IntersectionObserver' in window) && | ||
('IntersectionObserverEntry' in window) && | ||
('intersectionRatio' in window.IntersectionObserverEntry.prototype); | ||
} | ||
|
||
(function () { | ||
registerEventListeners(); | ||
})(); | ||
intersectionListenerSupported() { | ||
return ('IntersectionObserver' in window) && | ||
('IntersectionObserverEntry' in window) && | ||
('intersectionRatio' in window.IntersectionObserverEntry.prototype); | ||
} | ||
} |