[英]NATIVE JavaScript only - element opacity change when scrolled into viewport
I'm trying to make the elements on the page fade in on scroll. 我正在尝试使页面上的元素在滚动时淡入。 Easy enough right?
很容易吧? Not for me.
不适合我。
HTML is a standard list. HTML是标准列表。 CSS sets all elements to opacity 0 prior to scrolling.
CSS在滚动之前将所有元素设置为不透明度0。
I'm trying to use Native JavaScript only. 我正在尝试仅使用本机JavaScript。
// get current page body
var actBody = document.getElementById('acts-body');
// on scroll function
actBody.onscroll = function(){
// get screen height
var screenPosition = window.innerHeight;
// get all text elements
var artistName = document.getElementsByClassName('artist');
// loop through all elements
for(var i = 0; i < artistName.length; i++){
// get each elements position from top
var positionFromTop = artistName[i].getBoundingClientRect().top;
// if element is in viewport add class
if(positionFromTop - screenPosition <= 0){
artistName[i].classList.add('txt-fadeIn');
}
else{
artistName[i].classList.remove('txt-fadeIn');
}
console.log(artistName[i]);
}
i think it should solve it
我认为应该解决
if(screenPosition - positionFromTop <= 0){
artistName[i].classList.add('txt-fadeIn');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.