add class when element in viewport vanilla javascript
window.addEventListener('scroll', function (event) {
if (isInViewport(theElementToWatch)) {
// update the element display
}
}, false);
function isInViewPort(element) {
// Get the bounding client rectangle position in the viewport
var bounding = element.getBoundingClientRect();
// Checking part. Here the code checks if it's *fully* visible
// Edit this part if you just want a partial visibility
if (
bounding.top >= 0 &&
bounding.left >= 0 &&
bounding.right <= (window.innerWidth || document.documentElement.clientWidth) &&
bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight)
) {
console.log('In the viewport! :)');
return true;
} else {
console.log('Not in the viewport. :(');
return false;
}
}
Also in JavaScript:
- tableau javascript
- clear console javascript
- how to rename zip file nodejs
- href before onclick js
- discord.js bot github
- jquery ajax CORS
- moment add 30 days
- how to see my timezone using js
- change input placeholder text jquery
- fonction fleche javascript
- js api call
- How do you detect whether or not a word is a palindrome in javascript
- hamburger menu html and css
- como pegar as propriedades css de um elemento :after html com js
- node js mongoose async await
- info
- js order alphabetically
- toggle element javascript
- javascript open link with button
- js check if date is future
- less Than Or Equal To Zero javascript
- stopper un intervalle javascript
- new file shortcut vscode
- input in node js