javascript - 用户上下滚动时更改图像
问题描述
我有两张图片(一个封闭的盒子和一个打开的盒子)与本网站横幅下的动画非常相似。当用户上下滚动页面时,图像会发生变化。我将如何创造这种效果?
我曾尝试使用 Jquery 鼠标滚轮,但这对我不起作用。任何建议将不胜感激!
解决方案
这是您可以学习学习的代码示例:
var lastScrollTop = 0, delta = 1;
$("div").scroll(function(){
var nowScrollTop = $(this).scrollTop();
// console.log(nowScrollTop);
if(Math.abs(lastScrollTop - nowScrollTop) >= delta){
if (nowScrollTop > lastScrollTop){
// SCROLLING DOWN
$('#image img').attr('src', "https://supamama.co.za/img/product/closed.png");
} else {
// SCROLLING UP
$('#image img').attr('src', "https://supamama.co.za/img/product/open.png");
}
lastScrollTop = nowScrollTop;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<div style="border:1px solid black;width:470px;height:550px;overflow:scroll;">
<div>In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
<br><br>
'Whenever you feel like criticizing anyone,' he told me, just remember that all the people in this world haven't had the advantages that you've had.'
</div>
<div>In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
<br><br>
'Whenever you feel like criticizing anyone,' he told me, just remember that all the people in this world haven't had the advantages that you've had.'
</div>
<div id="image" >
<img src = "https://supamama.co.za/img/product/open.png" height = "250" width = "370" />
<div>In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
<br><br>
'Whenever you feel like criticizing anyone,' he told me, just remember that all the people in this world haven't had the advantages that you've had.'
</div>
<div>In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
<br><br>
'Whenever you feel like criticizing anyone,' he told me, just remember that all the people in this world haven't had the advantages that you've had.'
</div>
</div>
</div>
</body>
推荐阅读
- powershell - 用于搜索文件夹的 Powershell 脚本删除它们创建包含特定数据的日志文件
- java - Http Ok3 返回两个响应
- c# - 不知何故,我的值永远不会等于 1,这是有原因的吗?
- javascript - 无法在我的应用程序中找到下拉输入字段的元素
- c - 将一个字节复制到 GNU C 内联汇编中的另一个寄存器,其中编译器为两个操作数选择寄存器
- php - 如何在 JSON 中计算相同的数据?
- flutter - 热重载后会显示标记,但按下按钮时不会显示。(颤振 - 谷歌地图)
- webhooks - 我正在尝试从 Autodesk Forge workflow.transition 事件接收 webhook,但它们没有触发
- sql - 如何修改我的 SQL 语句,以便每个项目都有 (x) 名评委,并且每个评委在他们的人群中平均参与?
- mysql - 我想应用一个 INNER JOIN 并使用 3 个表使用 WHERE 条件