Last active
October 6, 2022 14:12
-
-
Save jeff-silva/cc0eb9c115b4e19ca206737ac5f90103 to your computer and use it in GitHub Desktop.
Javascript
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
/* Use var(--scroll) to get value on CSS */ | |
window.addEventListener('scroll', () => { | |
document.body.style.setProperty('--scroll', window.pageYOffset / (document.body.offsetHeight - window.innerHeight)); | |
}, false); |
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
(() => { | |
let loops = 100; | |
let url = "https://website.com"; | |
document.body.append(Object.assign(document.createElement('script'), { | |
src: "https://unpkg.com/axios@0.24.0/dist/axios.min.js", | |
onload: () => { | |
for(let i=1; i<=loops; i++) { | |
axios.get(url).then(resp => { | |
let doc = Object.assign(document.createElement('div'), {innerHTML: resp.data}); | |
let title = doc.querySelector('title').innerHTML; | |
console.log(`load ${i} - title: ${title}`); | |
}); | |
} | |
}, | |
})); | |
})(); |
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
let onscroll = function(callback) { | |
let scrollTimeout, lastScrollTop=0; | |
let toggleClass = function(selector, obj) { | |
document.querySelectorAll(selector).forEach(function(elem) { | |
for(var i in obj) { obj[i]? elem.classList.add(i): elem.classList.remove(i); } | |
}); | |
}; | |
let scrollHandle = function(ev) { | |
if (typeof callback!=='function') return; | |
let test = { | |
scrolling: false, | |
up: false, | |
down: false, | |
top: false, | |
bottom: false, | |
}; | |
lastScrollTop = lastScrollTop||0; | |
let currentScroll = window.pageYOffset||document.documentElement.scrollTop; | |
test.scrolling = true; | |
test.up = (currentScroll<=lastScrollTop); | |
test.down = (currentScroll>lastScrollTop); | |
test.top = (window.scrollY==0); | |
test.bottom = ((window.innerHeight + window.scrollY) >= document.body.offsetHeight); | |
if (scrollTimeout) clearTimeout(scrollTimeout); | |
scrollTimeout = setTimeout(function() { | |
test.scrolling = false; | |
// test.up = false; | |
// test.down = false; | |
callback(test, toggleClass); | |
}, 50); | |
lastScrollTop = currentScroll; | |
callback(test, toggleClass); | |
}; | |
scrollHandle({}); | |
window.addEventListener('scroll', scrollHandle); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment