gpt4 book ai didi

javascript - 如何在 slider 悬停时暂停

转载 作者:太空宇宙 更新时间:2023-11-04 16:14:15 25 4
gpt4 key购买 nike

JS

var $slider = $('.slider'); 
var $slide = 'li';
var $transition_time = 1000;
var $time_between_slides = 4000;
function slides(){
return $slider.find($slide);

}

slides().fadeOut();
slides().first().addClass('active');
slides().first().fadeIn($transition_time);
$interval = setInterval(
function(){
var $i = $slider.find($slide + '.active').index();

slides().eq($i).removeClass('active');
slides().eq($i).fadeOut($transition_time);

if (slides().length == $i + 1) $i = -1; // loop to start

slides().eq($i + 1).fadeIn($transition_time);
slides().eq($i + 1).addClass('active');
}
, $transition_time + $time_between_slides
);

CSS

.slider { margin: 10px 0; width: 1024px; height: 500px; position: relative; overflow: hidden; } 
.slider li { display: none; position: absolute; top: 0; left: 0; }

标记

<section id="">
<div class="row">
<div class="col-sm-12 col-md-12">
<ul class="slider">
<li>
<img src="images/highlights/foundation_transparent.png" alt="First slide">
</li>
<li>
<img src="images/highlights/nps_timeline_transparent.png" alt="Second slide">
</li>
<li>
<img src="images/highlights/nps_score_transparent.png" alt="Third slide">
</li>
<li>
<img src="images/highlights/startup_bridge_india_pr.png" alt="fourth slide">
</li>
</ul>
</div>
</div>
</section>

最佳答案

在图像上使用 mouseover 和 mouseout 事件来实现此目的。

var $slider = $('.slider'); 
var $slide = 'li';
var $transition_time = 1000;
var $time_between_slides = 4000;
function slides(){
return $slider.find($slide);
}

slides().fadeOut();
slides().first().addClass('active');
slides().first().fadeIn($transition_time);

$function = function(){
var $i = $slider.find($slide + '.active').index();

slides().eq($i).removeClass('active');
slides().eq($i).fadeOut($transition_time);

if (slides().length == $i + 1) $i = -1; // loop to start

slides().eq($i + 1).fadeIn($transition_time);
slides().eq($i + 1).addClass('active');
}

$interval = setInterval($function, $transition_time + $time_between_slides );

$("img").on("mouseover",function(){
clearInterval($interval);
});

$("img").on("mouseout",function(){
$interval = setInterval($function, $transition_time + $time_between_slides );
});

.slider { margin: 10px 0; width: 1024px; height: 500px; position: relative; overflow: hidden; } .slider li { display: none; position: absolute; top: 0; left: 0; }
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<section id="">
<div class="row">
<div class="col-sm-12 col-md-12">
<ul class="slider">
<li>
<img src="images/highlights/foundation_transparent.png" alt="First slide">
</li>
<li>
<img src="images/highlights/nps_timeline_transparent.png" alt="Second slide">
</li>
<li>
<img src="images/highlights/nps_score_transparent.png" alt="Third slide">
</li>
<li>
<img src="images/highlights/startup_bridge_india_pr.png" alt="fourth slide">
</li>
</ul>
</div>
</div>
</section>

关于javascript - 如何在 slider 悬停时暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41196901/

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