tylerperkins.xyz/themes/clortox/static/js/script.js

104 lines
3.1 KiB
JavaScript
Raw Normal View History

2023-07-15 22:44:31 +00:00
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
2023-07-19 02:44:03 +00:00
const typeWriterSpeed = 60;
2023-07-15 22:44:31 +00:00
window.onload = function() {
document.querySelectorAll(".scramble").forEach(item => {
item.addEventListener("mouseover", scramble);
});
2023-07-18 03:17:11 +00:00
window.addEventListener('scroll', function() {
let parallax = document.querySelector('.parallax')
2023-07-18 03:17:11 +00:00
let scrollPosition = window.pageYOffset;
let newSize = (document.querySelector('#content').offsetHeight * 0.66);
if(newSize < window.innerHeight)
newSize = window.innerHeight;
parallax.style.height = newSize + "px";
2023-07-18 03:17:11 +00:00
parallax.style.transform = 'translateY(' + scrollPosition * .5 + 'px)';
})
2023-07-19 02:44:03 +00:00
document.querySelectorAll(".typewriter").forEach(item => {
observer.observe(item);
});
}
function sleep(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds));
2023-07-15 22:44:31 +00:00
}
2023-07-20 02:14:23 +00:00
function replaceSpacesWithNbsp(string) {
if (string == null) return;
return string.replace(/ /g, "\xa0");
}
2023-07-16 00:59:44 +00:00
function scramble(event){
scrambleLetters(event.target);
}
function scrambleLetters(element) {
2023-07-15 22:44:31 +00:00
let iterations = 0;
2023-07-16 00:59:44 +00:00
const originalValue = element.dataset.value;
2023-07-15 22:44:31 +00:00
let interval = setInterval(() => {
2023-07-16 00:59:44 +00:00
element.innerText = element.innerText.split("")
2023-07-15 22:44:31 +00:00
.map((letter, index) => {
if(index < iterations){
return originalValue[index];
}
return letters[Math.floor(Math.random() * letters.length)];
}).join("")
iterations += 1 / 3;
if(iterations > originalValue.length){
clearInterval(interval);
2023-07-16 00:59:44 +00:00
element.innerText = originalValue;
2023-07-15 22:44:31 +00:00
}
}, 30);
}
2023-07-16 00:59:44 +00:00
2023-07-19 02:44:03 +00:00
async function typeWriter(element) {
let typeWriterText = element.getAttribute("data-value");
let output = element.innerText;
2023-07-20 02:14:23 +00:00
// write the main text
2023-07-19 02:44:03 +00:00
for(let i = 0; i < typeWriterText.length; ++i){
output += typeWriterText.charAt(i);
// sleep for typeWriterSpeed milliseconds
await sleep(typeWriterSpeed);
element.innerText = output;
}
2023-07-16 00:59:44 +00:00
2023-07-20 02:14:23 +00:00
//try and do the paragraphs associated
let hiddenElementId = element.getAttribute("data-show");
if(hiddenElementId == null) return;
let hiddenElement = document.getElementById(hiddenElementId);
let hiddenElementSpeed = 5;
let paragraphs = hiddenElement.querySelectorAll("p");
for (let p of paragraphs){
let hiddenElementText = p.getAttribute("data-value");
//hiddenElementText = replaceSpacesWithNbsp(hiddenElementText);
p.innerHtml = "";
for (let i = 0; i < hiddenElementText.length; ++i){
p.innerText += hiddenElementText.charAt(i);
await sleep(hiddenElementSpeed);
}
await sleep(50);
}
2023-07-19 02:44:03 +00:00
}
let observer = new IntersectionObserver(function(entries) {
for (let entry of entries){
// If the element is visible, start the animation
if(entry.isIntersecting) {
typeWriter(entry.target);
observer.unobserve(entry.target);
}
}
}, { threshold: 0.7 });