javascript - 如何在窗口滚动时向活动 div 添加动画?
问题描述
我的页面中有多个 div。这个 div 包含大小和颜色。向下滚动时,屏幕可见的 div 应该向上移动,向上滚动时,它应该返回到原来的位置。
这是到目前为止我尝试过的代码。使用此代码,当我向下滚动时, div 向上移动,但是当我向上滚动时, div 不会向下移动。有人可以帮我吗?
function isElementInViewport(el) {
if (typeof jQuery === "function" && el instanceof jQuery) {
el = el[0];
}
var rect = el.getBoundingClientRect();
return (
(rect.top <= 0 &&
rect.bottom >= 0) ||
(rect.bottom >= (window.innerHeight || document.documentElement.clientHeight) &&
rect.top <= (window.innerHeight || document.documentElement.clientHeight)) ||
(rect.top >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight))
);
}
window.addEventListener('scroll', function(e) {
winScrollTop = $(this).scrollTop();
var elementsToShow = document.querySelectorAll('.rectangle');
Array.prototype.forEach.call(elementsToShow, function(element) {
if (isElementInViewport(element)) {
element.classList.add('is-visible');
} else {
element.classList.remove('is-visible');
}
});
});
.rectangle {
background-color: red;
height: 444px;
width: 100px;
/* margin-top: -98px; */
z-index: -30;
transition: 0.5s;
transform: translateY(-98px);
margin-bottom: 25px;
}
.is-visible {
transform: translateY(-250px);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="rectangle"></div>
<div class="rectangle"></div>
<div class="rectangle"></div>
<div class="rectangle"></div>
解决方案
所以我在这里所做的是,首先我检查了滚动事件,然后确定滚动实际上是向上还是向下。一旦我确定我是向上还是向下滚动,我就分别添加了这些类。对于不可见的类,我正在设置样式 transform: translateY(0px) ,它只是将元素返回到其默认位置。
.not-visible{
transform: translateY(0px);
}
var lastScrollTop= 0;
window.addEventListener('scroll', function(e) {
winScrollTop = $(this).scrollTop();
var st = window.pageYOffset || document.documentElement.scrollTop; // Credits:
var elementsToShow = document.querySelectorAll('.rectangle');
if (st > lastScrollTop){ // If st is greater than lastscrollTop then it is downward
console.log("down");
// then check if elemetnts are in view
Array.prototype.forEach.call(elementsToShow, function(element) {
if (isElementInViewport(element)) {
element.classList.remove('not-visible'); // remove class notvisible if any
element.classList.add('is-visible'); // Add class isvisible
}
lastScrollTop = st <= 0 ? 0 : st; // For Mobile or negative scrolling
});
} else {
console.log("up");
Array.prototype.forEach.call(elementsToShow, function(element) {
if (isElementInViewport(element)) {
// Remove class isvisible and add class not visible to move the element to default place
element.classList.remove('is-visible');
element.classList.add('not-visible');
}
lastScrollTop = st <= 0 ? 0 : st; // For Mobile or negative scrolling
});
}
});
推荐阅读
- xcode - Xcode 中的 Target 旁边有一个 E
- python - Python将参数动态分配给Format函数
- electron - 卸载电子 msi 应用程序时如何删除它的 AppData?(使用 electron-builder)
- flutter - 拥有实例后如何访问类方法?
- python - 为什么 unittest.mock.ANY 不能与 Django 对象一起正常工作?
- python - 尝试查找列表的唯一编号时出现问题
- .htaccess - 将 401 错误页面直接重定向到下一级菜单项
- node.js - 如何在弹性搜索中为字段创建自动增量值
- angular - 离子标签中加载图像失败的错误事件
- wordpress - 如何使用 Contact Form 7 和 WP Mail SMTP 分配 SendGrid 类别