jquery - 如何使用jQuery动画显示循环中的下一个div元素
问题描述
我想编写一个 jQuery 动画,在循环中显示下一个 div。悬停时,动画应该停止。
目前,我让它在点击上工作,但我不知道如何为它制作动画。
这是代码:
var Text = $(".text");
Text.on("click", function() {
var TextVisibility = $(this).css("display", "none");
var NextText = TextVisibility.next(".text");
if (!NextText.length) NextText = Text.eq(0);
$(NextText).css("display", "block");
});
.text {
display: none;
white-space: pre-wrap;
}
.text:nth-child(1) {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section>
<div class="text">Text Text Text</div>
<div class="text">Link Link Link</div>
<div class="text">Another Text</div>
</section>
我需要进行哪些更改才能自动更改而不必单击文本?
解决方案
您可以通过创建一个函数来处理显示,并使用它setInterval
来告诉代码调用它的频率。
你需要做什么:
- 变量以跟踪当前文本,并将其初始化为第一个文本元素
- 创建我们用于更改文本的函数 - 这基于您现有的代码,并进行以下更改
a. 如果NextText
元素为空,则再次将其设置为第一个元素
b. 设置下一个元素时,将NextText
元素设置为currentText
元素 - 用于
setInterval
尽可能频繁地调用我们的函数(例如,在下面的示例中每 2 秒一次)
执行此操作的代码,注释引用了上面的数字,以便您了解它在做什么:
// 1. Variable to keep track of the current text
// We initialise this to the first text element
var firstText = $(".text:first-child");
var currentText = firstText;
// 2. Create or function for changing the text
function changeText(){
$(currentText).css("display", "none");
var NextText = currentText.next(".text");
// 2a. If its empty, get the first one again
if (!NextText.length) NextText = firstText;
$(NextText).css("display", "block");
// 2b. Set the current element to the new one
currentText = NextText;
}
// call our changeText function every 2 seconds
var loopTimer = setInterval(changeText, 2000);
要在悬停时暂停动画:
- 在元素上使用
hover
方法(我已经向animatedText
容器添加了一个类并使用了它) - 悬停时,清除我们
loopTimer
以使用停止动画clearInterval
- 在“mouseLeave”事件中,再次设置我们
loopTimer
以重新启动动画
注意:如果您希望动画与下一个元素一起重新启动,changeText()
请先调用。
执行此操作的代码:
$('.animatedText').hover(function(e){
clearInterval(loopTimer); // stop animation
}, function(e){
// If you want to that animation to restart with the NEXT element:
changeText();
loopTimer = setInterval( changeText, 2000); // restart animation
})
工作示例:
var animationDuration = 2000; // 2 seconds
// Variable to keep track of the current text
// We initialise this to the first text element
var firstText = $(".text:first-child");
var currentText = firstText;
function changeText(){
$(currentText).css("display", "none");
// Get the next text element
var NextText = currentText.next(".text");
// If its empty, get the first one again
if (!NextText.length) NextText = firstText;
// Show the next element and set the current text
$(NextText).css("display", "block");
currentText = NextText;
}
// call our changeText function every "animationDuration" seconds
var loopTimer = setInterval( changeText, animationDuration);
$('.animatedText').hover(function(e){
// stop animation
clearInterval(loopTimer);
}, function(e){
// If you want animation to restart with the NEXT element:
changeText();
// restart animation
loopTimer = setInterval( changeText, animationDuration);
})
.text {
display: none;
white-space: pre-wrap;
}
.text:nth-child(1) {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section class="animatedText">
<div class="text">1 Text Text Text</div>
<div class="text">2 Link Link Link</div>
<div class="text">3 Another Text</div>
<div class="text">4 More Text</div>
<div class="text">5 Nearly Done</div>
<div class="text">6 Last Text</div>
</section>
推荐阅读
- hibernate - 如何在本机查询中访问子类实体字段?
- flask - 为什么我的服务器抱怨“错误的授权标头”?
- git - 是否可以将更改拉/推到在另一台 PC 上克隆的 Git 存储库?
- android - 从 Android 手机设置中的 24 小时格式更改后,gson 无法反序列化日期时间
- django - 如何使用来自 Django 的序列化数据访问 Angular 中的 ForeignKey 模型属性
- sql - FROM 关键字不是预期的
- c# - Asp.net mvc 5 code first class的字段名不存储在数据库中
- java - 如何在android中摇晃手机3次后检测摇晃和敬酒消息
- angular - 选项的默认值仅在 IE 中不以角度形式出现
- node.js - 在 Nodejs 中使用数千个不同的代理 IP 转发 TCP 连接?