在这段代码中,我正在制作一个包含数据的盒子,这些数据一个接一个地显示两个li 但是我希望它在鼠标移到它上面时停止并在鼠标离开时计数。
ul.bxslider li{
width:77%;
margin: 50px 0 0 80px;
height:150px;
display:none;
text-align:left;
font-size: 20px;
font-weight:normal;
font-family: 'Playball', cursive;
}
<ul class="bxslider">
<li class="fader">1) Testimonial ticker goest here,
showing all testimonials one by one.
Testimonial ticker goest here, showing
all testimonials one by one. Testimonial ticker
goest here, showing all testimonials one by one
</li>
<li class="fader">2) Testimonial ticker goest here,
showing all testimonials one by one<br />
Testimonial ticker goest here, showing
all testimonials one by one<br />
Testimonial ticker goest here, showing
all testimonials one by one
</li>
</ul>
$(document).ready(function(fadeLoop) {
var fad = $('.fader');
var counter = 0;
var divs = $('.fader').hide(); // Selecting fader divs instead of each by name.
var dur = 500;
fad.children().filter('.fader').each(function(fad) {
function animator(currentItem) {
animator(fad.children(":first"));
fad.mouseenter(function(){
$(".fader").stop();
});
fad.mouseleave(function(){
animator(fad.children(":first"));
});
};
});
function showDiv() {
divs.fadeOut(dur) // hide all divs
.filter(function(index) {
return index == counter % divs.length;
}) // figure out correct div to show
.delay(dur) // delay until fadeout is finished
.fadeIn(dur); // and show it
counter++;
}; // function to loop through divs and show correct div
showDiv(); // show first div
return setInterval(function() { // return interval so we can stop the loop
showDiv(); // show next div
}, 2 * 1000); // do this every 5 seconds
});
// JavaScript Document
答案 0 :(得分:0)
您有两个包含所有推子的变量。我假设你想要一个用于推子,一个用于ul
。
var fad = $('.bxslider');
var divs = $('.fader').hide();
var dur = 500;
我们可以创建一个变量来保存下一个元素,以及一个指示动画是否应该停止的变量。
var stop = false;
var next = $();
我们可以检查整个列表中的鼠标是否更可靠。
fad.mouseenter(function(){
stop = true;
});
fad.mouseleave(function(){
stop = false;
});
我们新的和改进的showDiv
函数检查我们是否需要在做任何事情之前停止。它也进行了重组,以确保当我们告诉它停止制作动画时我们会看到一些东西。
function showDiv() {
if (stop) return;
if (next.length) {
next.fadeOut(dur);
}
next = next.next();
if (next.length === 0)
next = divs.first();
next.delay(dur).fadeIn(dur);
};
通常的计时器。我们可以重写它来传递函数。
showDiv();
setInterval(showDiv, 2 * 1000);