思路:1.判断鼠标向上还是向下滚动
2.执行鼠标事件
鼠标滚动事件注册事件资料:https://www.cnblogs.com/wsoft/articles/2856861.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script type="text/javascript" src='https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js'></script>
<script type="text/javascript" src='jquery-1.11.3.js'></script>
<title>Document</title>
<style>
.a {
height: 1000px;
width: 80%;
margin: 0 auto;
background: #6498e1;
}
.a2 {
height: 1000px;
width: 80%;
margin: 0 auto;
background: #bb7878;
}
</style>
</head>
<body>
<div class="a"></div>
<div class="a2"></div>
<div class="a"></div>
<div class="a2"></div>
<div class="a"></div>
<div class="a2"></div>
<script>
var number = 0
var gd = 0
// 向下滚动还是向下滚动
var scrollFunc = function (e) {
gd = e.wheelDelta;
}
if (document.addEventListener) {
document.addEventListener('DOMMouseScroll', scrollFunc, false);
} //W3C
window.onmousewheel = document.onmousewheel = scrollFunc; //IE/Opera/Chrome/Safari
// scrop距离
var isScrolling = false //是否在滚动
listenAndScroll()
// 无限滚动
function listenAndScroll() {
$(document).scroll(function () {//执行几千次
var b = $(window).scrollTop();
if (isScrolling) {
return
}//如果isScrolling为true就跳出去
isScrolling = true//
var scrollGap = 1000
if (b > 0 && gd < 0) {
let isBlock = b % scrollGap === 0 //%取余 余为零
console.log(isBlock, b % scrollGap, b)
if (isBlock) {
return isScrolling = false//如果isBlock为true就跳出并isScrolling = false
//跳出之后终止语句
}
let pos = (Math.floor(b / scrollGap) + 1) * scrollGap
console.log(pos)
$("html").animate({
scrollTop: pos
}, "slowly", function () {
isScrolling = false;
});
}
else if (b > 0 && gd > 0) {
let isBlock2 = b % scrollGap === 0
// console.log(b,isBlock2,scrollGap)
if (isBlock2) return isScrolling = false
let pos2 = (Math.ceil(b / scrollGap) - 1) * scrollGap
console.log(pos2)
$("html").animate({
scrollTop: pos2
}, "slowly", function () {
isScrolling = false
});
}
else {
isScrolling = false
}
})
}
</script>
</body>
</html>
3742



被折叠的 条评论
为什么被折叠?



