gpt4 book ai didi

JavaScript 滚动检测和滚动速度

转载 作者:行者123 更新时间:2023-11-30 17:06:47 25 4
gpt4 key购买 nike

我正在做一些 JS 项目,我正在尝试更改滚动速度并滚动到下一个 100% 的分区高度 ...

我对向下滚动的想法>检测 InnerHTML 高度 + 从顶部检测滚动空间,使用 innerHTML 高度和计数器检测位置....

当用户第一次向下滚动时,position 为 0 而 scrollTop 更多,因此函数被调用并将用户带到下一个 div 。 (没关系)

但是,当用户向上滚动时,我该怎么做才能将用户带回到之前的 div?这是我的代码:

<html>
<head>
<style>
body,html {
margin: 0;
height: 100%;
width: 100%;
}
.scrollers {
height: 100%;
width: 100%;
transition: all 1s;
-webkit-transition: all 1s;
}
#N1 {
background-color: #725a5a;
}
#N2 {
background-color: #4a478d;
}
#N3 {
background-color: #478d6f;
}
#N4 {
background-color: #8d8a47;
}
#status {
text-align: center;
width: 100%;
color: #000;
font-size: 20px;
position: fixed;
z-index: 10;
top: 5px;
}
</style>
<script>
var $firstPosition = 1;
window.onscroll = scroll;
var $counter = 0;
var $status = "play";
function scroll() {
var $sctop = document.body.scrollTop;
var $inrH = window.innerHeight;
var $secst;
if (($status=="pause") || ($secst=="pause")){

} else if ($status=="play"){
var $position = $counter * $inrH;
if ($sctop>$position) {
$counter++;
$position = $counter * $inrH;
$status = "pause";
$secst = "pause";
callMeInterval = setInterval( function() { if(!($sctop==$position)){window.scrollTo(0,$firstPosition);$firstPosition++;$sctop = document.body.scrollTop;document.getElementById("status").innerHTML = "sctop = " + $sctop + " $position = " + $position + "$counter = " + $counter; } else if($sctop==$position) {$status = "play";secst==""} }, 1 );
}
// Problem Starts Here
else {
$counter--;
$position = $counter * $inrH;
$status = "pause";
$secst = "pause";
callMeInterval = setInterval( function() { if(!($sctop==$position)) {$firstPosition--;window.scrollTo(0,$firstPosition);$sctop = document.body.scrollTop;document.getElementById("status").innerHTML = "sctop = " + $sctop + " $position = " + $position + "$counter = " + $counter; } else if($sctop==$position) {secst=="";$status = "play"} }, 1 );
}
}
}

</script>
</head>
<body>
<div id="status">

</div>
<div id="N1" class="scrollers">

</div>
<div id="N2" class="scrollers">

</div>
<div id="N3" class="scrollers">

</div>
<div id="N4" class="scrollers">

</div>
</body>
</html>

我确信使用 JQUERY 整个事情会更容易,但这是我的 Javascript Class 项目。谢谢

最佳答案

你没有清除你的间隔,所以这是闪烁的原因。

clearInterval(callMeInterval);

我推荐你使用requestAnimationFrame而不是使用 window.setInterval

在这里你有工作sample用 requestAnimationFrame 重建

var lastScrollPosition = document.body.scrollTop,
sectionNo = 0;

function doScroll(scrollPosition, step, first) {
var height = window.innerHeight,
min = height * sectionNo,
max = height * (sectionNo + 1);

scrollPosition += step;

if (min < scrollPosition && scrollPosition < max) {
// here should be some animation control
document.body.scrollTop = scrollPosition;
// Call next animation frame
window.requestAnimationFrame(doScroll.bind(null, scrollPosition, step));
} else {
// It fires, when scroll is done
lastScrollPosition = scrollPosition;
document.addEventListener("scroll", scrollListener);
}
}
function scrollListener(e) {
var scrollPosition = document.body.scrollTop,
step;

// Stop scroll listening
document.removeEventListener("scroll", scrollListener);

// Get direction
step = scrollPosition >= lastScrollPosition ? 5 : -5;

// Go back to initial position
document.body.scrollTop = lastScrollPosition;

// Animate
window.requestAnimationFrame(doScroll.bind(null, lastScrollPosition, step, true));
}
document.addEventListener("scroll", scrollListener);

关于JavaScript 滚动检测和滚动速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27841433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com