jquery - 如何锁定div的滚动
问题描述
我想创建一个锁定滚动的 div,以便“向下滚动”的唯一方法是单击向下滚动 #middle div 的 div。我还希望用户只能通过单击 .up div 来向上滚动#top,其余的 div 被锁定并且无法通过正常滚动访问
https://codepen.io/anon/pen/WPoNrw
有什么建议么?
$("#top").click(function() {
$('html,body').animate({
scrollTop: $("#middle").offset().top},
'slow');
});
$(".up").click(function() {
$('html,body').animate({
scrollTop: $("#top").offset().top},
'slow');
});
解决方案
你需要禁用滚动希望它会帮助你
更新
使用mouseout
和onmouseover
禁用特定的 div
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style type="text/css">
#top {
height: 1000px;
background-color: pink;
}
#top:hover {
cursor: url(""), auto;
}
#middle {
height: 1000px;
background-color: #add8e6;
}
#bottom {
height: 1000px;
background-color: #add8e6;
}
h1 {
text-align: center;
text-transform: uppercase;
margin: 20px;
}
h2 {
text-align: center;
text-transform: uppercase;
font-size: 1em;
}
</style>
</head>
<body>
<div>
<div id="top" onmouseover="document.body.style.overflow='hidden';" onmouseout="document.body.style.overflow='auto';">
<h1>top</h1>
</div>
<div id="middle">
<h1>middle</h1>
<div class="up">
<h2>click me</h2>
</div>
</div>
<div id="bottom">
<h1>bottom</h1>
</div>
</div>
</body>
<script type="text/javascript">
$("#top").click(function() {
$('html,body').animate({
scrollTop: $("#middle").offset().top
},
'slow');
});
$(".up").click(function() {
$('html,body').animate({
scrollTop: $("#top").offset().top
},
'slow');
});
</script>
</html>
推荐阅读
- postgresql - PGSQL 在特定条件下选择列
- java - Eclipse 无法读取位于 https://marketplace.eclipse.org/content/ibm-mq-explorer-version-9 的存储库
- groovy - 关闭 ActiveMQ 连接时 Java 线程挂起
- mongodb - 聚合内部字段 MongoDB
- wordpress - Yoast 搜索结果错误
- r - 创建按钮以子集和更新 R 绘图地图
- python - Python asyncio:单线程如何同时处理多个事情?
- .net - System.Data.SQLite 与 Microsoft.Data.Sqlite
- c# - Xunit 测试无法识别 JSON 测试数据文件何时发生更改
- google-cloud-platform - Google 最近是否更改了 UI 以授予对 Google Cloud Platform 中存储桶对象的公开访问权限?