- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
与 to a question I had before 相比,这在某种程度上是一个类似的问题但我完成了一些步骤。
我们有一个包含 6 个“场景”的页面。它们都有类.scene
。
当我按下nextButton
时,我会根据我在页面上的位置转到下一个可见场景。因此,如果我滚动到场景 2,然后按下按钮,我需要转到场景 3。依此类推...
我设法找出分配 .animate()
和 scrollTop
属性时出错的地方,并设法获取 previousButton
> 和 nextButton
工作没有问题。此时,我可以根据我需要的要求转到下一个或上一个 div。
它必须像这样动态工作,以便添加更多或更少的 div
。
现在我需要确保它“无限地”滚动。如果我在最后场景,并且按下nextButton
,我将滚动回顶部,并继续其常规功能。如果我在第一个场景并按previousButton
,我会向下滚动到最后一个场景/div。所以无论我点击多少次,它都会持续循环
完成此任务最简单的方法是什么?到目前为止,如果我到达任一端,我都会收到一条错误,指出“无法读取未定义的属性'TOP'
”
JQUERY
var brightness = 90 ;
var $totalScenes = $('.scenes').length;
// 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() {
$(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).on('resize scroll', function() {
$('.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").on('click', function() {
$('.scenes').each(function() {
if ($(this).isInViewport()) {
nextSlide = $(this).next();
}
});
$("html, body").animate ({scrollTop: $(nextSlide).offset().top});
});
$("#previousButton").on('click', function(index) {
$('.scenes').each(function() {
if ($(this).isInViewport()) {
prevSlide = $(this).prev();
}
});
$("html, body").animate ({scrollTop: $(prevSlide).offset().top});
});
HTML
<!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>
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);
}
最佳答案
我怀疑问题与此有关(以及之前的相关部分):
nextSlide = $(this).next();
在这种情况下,如果 this
是其同级集合中的最后一个,则 .next()
将返回一个空集合。稍后,当提供此空组时,.offset()
返回undefined
。当尝试访问 .top
时生成的 undefined
会生成错误。
要解决此问题,您需要确保处理 nextSlide
可能为空的可能性。做这件事有很多种方法。一种这样的解决方案可能是:
// your existing code
$('.scenes').each(function() {
if ($(this).isInViewport()) {
nextSlide = $(this).next();
}
});
// additional code to fix the issue
if(nextSlide.length == 0) {
nextSlide = $('.scenes').first();
}
// back to your existing code
$("html, body").animate ({scrollTop: $(nextSlide).offset().top});
关于javascript - 让我的点击滚动功能无限循环的最简单方法? (滚动顶部/动画),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59347527/
我制作手机游戏,但我想为我的社交游戏制作一些简单的基于浏览器的客户端,以便我们可以更轻松地调试。最简单的引擎是什么? 我研究了一些 HTML5 引擎和 GWT,但我想听听社区的意见。我正在寻找一种能够
有一个有趣的例子,有人设法为一个只占用 1kb 的论坛创建了一个 PHP 脚本:http://www.nerdparadise.com/blogs/blake/6034/ 我想知道是否有类似的小脚本可
首先我要吐槽一下,看程序的过程中遇见了yield这个关键字,然后百度的时候,发现没有一个能简单的让我懂的,讲起来真tm的都是头头是道,什么参数,什么传递的,还口口声声说自己的教程是最简单的,最浅显易
我的proguard配置太糟糕了,我的游戏崩溃了,而且似乎不正常。在我弄清楚到底哪里出了问题之前,最简单,最安全的配置用于全部安装是什么?有没有办法使它仅更改变量名?或者只是混淆代码以使其更难阅读而不
我正在重构一些 C# 代码,其中一部分是重做一些引用,因为我们正在完全重做文件夹结构。我想做的就是进入 .csproj 或 .sln 文件并修改路径。 然而,一些引用文献有类似的路径 "../../.
免责声明:这是一个理论问题,目的是增加我的理解。我知道我总是可以使用像 JSON 库这样的工具来解决问题。 假设我想创建一个逗号分隔的值列表,这些值本身可能包含逗号。这些逗号需要先转义。假设我使用 .
我对编程完全陌生,我选择 Delphi 作为我想学习的编程语言。 我基本上想构建使用套接字填写和提交 Web 表单的工具,并且我希望它们也是多线程的。 我希望它们功能丰富且性能正确。 我并不急于这样做
我正在构建一个协作创作工具,该工具允许用户共同编辑信息空间,该信息空间是节点和链接的可视化。一个客户端应用程序中所做的更改需要反映到所有其他客户端中。由于它是可视化的,因此可能需要经常更新潜在的大数据
作为一家专门开发自定义 CMS 的公司,我们被要求在下一个项目中部署开源 CMS。 我们可以自由选择系统。对于熟悉 PHP5 中的 MVC 模型和 OOP 的团队,您会推荐什么? 有人告诉我Drupa
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我完全是 Ember.js 菜鸟,需要了解后端注意事项,目前似乎很少有教程涵盖。对于快速原型(prototype)设计,最简单/最简单的后端设置是什么?我看到了一些 ember-rails 教程,但是
我正在寻找这种最简单、最简单的方法来启动 Java Web 服务。我曾经使用 Axis-1-on-Tomcat,但是对于 Axis 2,它变得太厚了。我正在寻找的一些偏好: 低内存占用 - 一个包含最
我多年来一直使用旧版 openGL 和 cocoa,但现在我正在努力过渡到 openGL 3.2。互联网上有几个例子,但它们都太复杂了(许多甚至在 XCode 5.1 下不再编译)。有人可以编写一个最
我正在构建一个简单的应用程序,它应该将开关/支票簿的状态存储 7 天。我遇到的唯一问题是我用来构建所述应用程序的网站不适合手机上的本地存储。没有通过本地存储或链接到在线数据库来存储开关/支票簿状态的良
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 个月前。 Improv
有时我需要 1 个用户类型元素的集合(或任何其他容器)并以这种方式创建它们: boost::assign::list_of(typeVariable).convert_to_container >()
我的页面上有一个 DropDownList 和一个 TextBox。当用户在 DropDownList 中选择“其他”选项时,我想在其右侧显示一个文本框。我不想使用传统的回发技术。我希望这种交互是在客
说到编码,我还很年轻,而且我听说过很多关于组织的事情。有些使用部分,有些使用 div,有些使用 div 作为按钮,其他使用 css 中的输入来更改它。作为一个喜欢让他的代码干净、简单易懂但又正确的人,
我需要将带有变量项的 ContentValues 转换为 JSON 字符串,我可以将其保存到数据库中,并在以后用作 HTTP 请求的正文。网络上的所有内容都只会以相反的方向进行转换。 最佳答案 每当您
我的电脑上安装了 Python 3.7。想用tensorflow,发现基本不支持3.7,所以想(也)安装Python 3.6。 关于如何做到这一点有什么建议吗?我是否必须卸载 3.7 并将其替换为 3
我是一名优秀的程序员,十分优秀!