如果标题有点含糊,请原谅我,因为我不确定如何写下来。
基本上:
我们有一个包含 6 个“场景”的页面。他们都有类(class) .scene
和不同的 ID,例如:scene1
, scene2
等等……
起初,我们必须为每个场景动态改变颜色 -> 场景 1 是浅色,最后一个场景是黑色。到目前为止,即使在添加或删除场景时也是如此。
有一个<li>
在页面的顶部和底部。其中有一个可点击的 <a>
标签。
当我按下 nextButton
时,我需要转到下一个可见场景取决于我在页面上的位置。因此,如果我滚动到场景 2,然后按下按钮,我需要转到场景 3。如果我滚动到场景 5 并按下按钮,我需要转到场景 6,依此类推……其他情况相同按钮 previousButton
,但反过来(向上)。
此外,如果我用 nextButton
到达页面末尾(场景 6),它会回到场景 1。全部使用 scrollTop
完成
我一直在谷歌搜索这方面的问题,但我似乎无法弄清楚到底该做什么或如何开始。
在 $(window).scroll(function)
下编写的整段代码'直到each()
导师给我们的功能。
var brightness = 90 ;
var $totalScenes = $('.scenes').length;
$('.scenes').each(function() {
$(this).css({"background-color" : "hsl(99, 100%, "+ brightness + "%" + ")" });
//je hebt 6 scenes en je wilt weten hoe groot de stappen zijn. vanaf de min bereken je hoe groot de stap is. dit doe je 1x (100/totalScenes - 1), dus van 100 naar nul in 5 stappen is 20. Dit herhaalt zich telkens.
brightness = brightness - 100/($totalScenes - 1);
console.log(brightness);
});
$(window).scroll(function() {
// is in viewport function
$.fn.isInViewport = function () {
var elementTop = $(this).offset().top;
var elementBottom = elementTop + $(this).outerHeight();
var viewportTop = $(window).scrollTop();
var viewportBottom = viewportTop + $(window).height();
return elementBottom > viewportTop && elementTop < viewportBottom;
};
$('.scenes').each(function(index) {
if ($(this).isInViewport()) {
console.log("dit is " + this.id);
if(index >= $totalScenes / 2) {
$('li').css({"color": "#fff"});
}
else {
$('li').css({"color": "#000"});
}
}
});
});
$('#nextButton').click(function() {
$("html, body").animate({
scrollTop: $(this).closest('.scenes').next().offset().top });
// scrollTop: $(this).parent().next().find('.scenes').offset().top } , 100);
});
CSS
html {
scroll-behavior: smooth;
font-family: 'Roboto', sans-serif;
}
.scenes {
height: 100vh;
max-width: 100vw;
}
h2 {
text-align: center;
font-size: 10vh;
line-height: 100vh;
}
.circle{
background: rgba(255, 255, 255, 0.6);
border-radius: 50%;
font-weight: bold;
line-height: 150px;
width: 150px;
display: inline-block;
}
header {
position: fixed;
width: 100vw;
height: 100vh;
}
li {
font-weight: bold;
font-size: 1.4em;
position: absolute;
}
li a{
cursor: pointer;
text-align: center;
}
#previous {
top: 20px;
left: 50vw;
transform: translate(-50%, 0);
}
#next {
bottom: 20px;
left: 50vw;
transform: translate(-50%, 0);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<?php include 'template-parts/head.php'; ?>
</head>
<body>
<header>
<ul>
<li id="previous"><a id="previousButton" href="#">prev ↑</a></li>
<li id="next"><a id="nextButton" href="#">next ↓</a></li>
</ul>
</header>
<main>
<div id="wrapper">
<div id="scene1" class="scenes">
<h2><span class="circle">1</span></h2>
</div>
<div id="scene2" class="scenes">
<h2><span class="circle">2</span></h2>
</div>
<div id="scene3" class="scenes">
<h2><span class="circle">3</span></h2>
</div>
<div id="scene4" class="scenes">
<h2><span class="circle">4</span></h2>
</div>
<div id="scene5" class="scenes">
<h2><span class="circle">5</span></h2>
</div>
<div id="scene6" class="scenes">
<h2><span class="circle">6</span></h2>
</div>
</div>
</main>
最佳答案
检查这个
var brightness = 90;
var $totalScenes = $(".scenes").length;
var nextElment = "scene1";
$(".scenes").each(function() {
$(this).css({
"background-color": "hsl(99, 100%, " + brightness + "%" + ")"
});
//je hebt 6 scenes en je wilt weten hoe groot de stappen zijn. vanaf de min bereken je hoe groot de stap is. dit doe je 1x (100/totalScenes - 1), dus van 100 naar nul in 5 stappen is 20. Dit herhaalt zich telkens.
brightness = brightness - 100 / ($totalScenes - 1);
console.log(brightness);
});
$(window).scroll(function() {
// is in viewport function
$.fn.isInViewport = function() {
var elementTop = $(this).offset().top;
var elementBottom = elementTop + $(this).outerHeight();
var viewportTop = $(window).scrollTop();
var viewportBottom = viewportTop + $(window).height();
return elementBottom > viewportTop && elementTop < viewportBottom;
};
$(".scenes").each(function(index) {
if ($(this).isInViewport()) {
console.log("dit is " + this.id);
nextElment = this.id;
if (index >= $totalScenes / 2) {
$("li").css({ color: "#fff" });
} else {
$("li").css({ color: "#000" });
}
}
});
});
$("#nextButton").click(function() {
$("html, body").animate({
scrollTop: $("#" + nextElment)
.next()
.offset().top
});
// scrollTop: $(this).parent().next().find('.scenes').offset().top } , 100);
});
$("#previousButton").click(function() {
$("html, body").animate({
scrollTop: $("#" + nextElment)
.prev()
.offset().top
});
// scrollTop: $(this).parent().next().find('.scenes').offset().top } , 100);
});
关于jquery - 如何根据我的滚动位置将 scrollTop 设置为下一个可见 div/类的动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59329915/