gpt4 book ai didi

javascript - 自动滚动不会在 $ ('html,body' ).animate 处设置动画

转载 作者:行者123 更新时间:2023-11-28 06:11:09 24 4
gpt4 key购买 nike

我正在制作欢迎页面。我需要点击一下才能跳转到某个div还有一个小滚动条可以跳转到下一个 div。我不太擅长 javascript,但我尝试了一些东西,结果是这样的

$(".skippage").click(function() {
$('html, body').animate({
scrollTop: $("#content").offset().top
}, 300);
});

(function() {
var delay = false;
$(document).on('mousewheel DOMMouseScroll', function(event) {
event.preventDefault();
if (delay)
return;

delay = true;
setTimeout(function() {
delay = false
}, 200)
var wd = event.originalEvent.wheelDelta || -event.originalEvent.detail;
var a = document.getElementsByClassName('.IndexSection');

if (wd < 0) {
for (var i = 0; i < a.length; i++) {
var t = a[i].getClientRects()[0].top;
if (t >= 40) break;
}
} else {
for (var i = a.length - 1; i >= 0; i--) {
var t = a[i].getClientRects()[0].top;
if (t < -20) break;
}
}
$('html,body').animate({
scrollTop: a[i].offsetTop
});
});
})();
html,
body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
.IndexSection {
font-size: 6em;
color: #ccc;
width: 100%;
}
div#welcome {
height: 100vh;
background: white;
text-align: center;
margin: 0;
position: relative;
}
.welcometext {
background-color: red;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
height: 70%;
width: 80%;
float: none;
margin: 0 auto;
text-align: center;
position: absolute;
}
.skippage {
font-size: 12px;
color: red;
position: absolute;
bottom: 2%;
left: 50%;
transform: translate(-50%, -2%);
}
div.navigation {
background: #9C0;
font-size: 12px;
height: 10%;
}
div#content {
height: 100vh;
background: yellow;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Home</title>
<link rel="stylesheet" type="text/css" href="style/bootstrap/css/bootstrap.min.css"> <!-- Bootstrap -->
<link rel="stylesheet" type="text/css" href="style/main.css"> <!-- custom -->
</head>
<body>
<div id="welcome" class="IndexSection row">
<div class=" welcometext">
welcome
</div>
<a href="#" class="skippage">Go Down</a>
</div>
<div id="content" class="IndexSection">
<div class="navigation">
option
</div>
Content
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="style/bootstrap/js/bootstrap.min.js"></script> <!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="style/main.js"></script> <!-- custom -->
</html>

我的点击功能做得很好,但是当我向下滚动一点并移动到上一个 时,自动滚动或小滚动或它调用的任何东西都会移动到下一个 div div 当我向上滚动一点时效果不佳。

  1. 我是否搞乱了 JS 末尾的动画 $('html,body')
  2. 逻辑应该是 = 当我向下滚动大于或等于 40 时,div 将向下跳转;当我向上滚动大于或等于 -20 时,div 将向上跳转,
  3. 我刚刚弄清楚如果我改变

    var a= document.getElementsByClassName('.IndexSection');进入

    var a= document.getElementsByTagName('div');它移动了,几乎就像我想要的那样......但是为什么我不能使用通过类名获取元素?

我错过了什么?我想应该是完美的。请帮忙

最佳答案

改变

var a= document.getElementsByClassName('.IndexSection');

var a= $('.IndexSection');

并将 offsetTop 更改为 offset().top

 $('html,body').animate({
scrollTop: a.eq(i).offset().top
});

整个代码是:

(function() {
var delay = false;

$(document).on('mousewheel DOMMouseScroll', function(event) {
event.preventDefault();
if(delay) return;

delay = true;
setTimeout(function(){delay = false},200)

var wd = event.originalEvent.wheelDelta || -event.originalEvent.detail;

var a= $('.IndexSection');

if(wd < 0) {
a.each(function(){
var t = $(this).position()[0].top;
if(t >= 40) return false;
});
}
else {
for(var i = a.length-1 ; i >= 0 ; i--) {
var t = a[i].getClientRects()[0].top;
if(t < -20) break;
}
}
$('html,body').animate({
scrollTop: a.eq(i).offset().top
});
});
})();

关于javascript - 自动滚动不会在 $ ('html,body' ).animate 处设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36378410/

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