jquery - 如何为自动幻灯片编写循环?
问题描述
我想在幻灯片中添加一个循环,但我是 jQuery 新手,不知道该怎么做。这是代码:
$(function(){
$("#slide_container").mouseout(function(){
/*a loop here that change the images automatically when the mouse is not inside the div,
get the index of the image and add to it like a for loop (i = 0; i < slides.length; i++)
but I still don't know which var should I add and how mouseout works.*/
});
$("#slide_container").mouseover(function(){
$('#nextBtn').on('click', function(){
console.log('clicked');
var currentImg = $(".active");
var nextImg = currentImg.next();
if(nextImg.length){
currentImg.removeClass('active').css('z-index', -10);
nextImg.addClass('active').css('z-index', 10);
}
});
$('#prevBtn').on('click', function(){
console.log('clicked');
var currentImg = $(".active");
var prevImg = currentImg.prev();
if(prevImg.length){
currentImg.removeClass('active').css('z-index', -10);
prevImg.addClass('active').css('z-index', 10);
}
})
解决方案
你可以使用setInterval
. 如果代码在惯性间隔内连续执行一段
您可以触发单击按钮以转发幻灯片
let s_interval = null;
const DELAY = 1000 //1s between each transition
$("#slide_container").mouseout(function(){
const next_button = $('#nextBtn')
s_interval = setInterval(()=> next_button.click(),DELAY)
});
$("#slide_container").mouseenter(function(){
clearInterval(s_interval)
});
推荐阅读
- python - 蟒蛇 | 散点图不显示
- python - 二维列表仅保留传递给它的最后一个值
- python - 为什么我的 PNG 文件会导致我的 python pptx 文件崩溃?
- url - 为什么生成的活动 URL 中从 %20 更改为 +?
- javascript - NodeJS:控制加载模块的顺序
- html - 删除文件上传内容并在指向圆圈中间的文本时悬停
- javascript - 我无法从使用 PHP 返回的每个 html 表中获取数据
- db2 - db2prereqcheck 实用程序找不到库文件 libnuma.so.1。在 ubi7/ubi: 最新的 docker 镜像
- executorservice - 向 Executorservice 发送多种类型的 Runnable Tasks
- javascript - Webpack 突然无法编译,内部出现错误