javascript - 向下滚动时淡入淡出元素

标签 javascript jquery

我想在另一侧进行淡入淡出:fadeIn从下到上,而不像现在,fadeOut从下到上

var $elem = $('.test.fade');
for (var i = 0; i <= 5; i++) {
    $elem.clone().appendTo('body');
}
$(window).scroll(function() {
    $('.fade').each(function() {
        var bounds = this.getBoundingClientRect(),
          	op = Math.max((bounds.height + Math.min(bounds.top, 0)) / bounds.height, 0);
        $(this).css('opacity', op);
    });
});
* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

.test {
    height: 70vh;
    width: 30%;
    background-color: rgba(0, 0, 0, 0.6);
    margin: 1em auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test fade"></div>

jsfiddle

感谢您的帮助。

最佳答案

将不透明度计算更改为:

op = Math.max((bounds.height - Math.max(bounds.top, 0)) / bounds.height, 0);

所以它依赖于盒子的底部边框。

关于javascript - 向下滚动时淡入淡出元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49127426/

相关文章:

javascript - ExtJS 列中的排序会从模板列中删除节点

javascript - jQuery $ 未定义

java - spring ajax文件上传问题

c# - 清除客户端所有文本框

javascript - js 客户端脚本注册客户端脚本 block C#

javascript - 第一次点击后点击谷歌地图标记不会打开信息窗口

javascript - 在ajax中传递多个参数给我错误

javascript - 我想先使用 jquery 加载背景,但我的代码似乎不起作用

javascript - jQuery - 悬停时停止自动滚动 div

javascript - 如何访问访问过的链接历史记录