gpt4 book ai didi

javascript - 在鼠标悬停时取消 Jquery Slider 事件

转载 作者:行者123 更新时间:2023-11-30 12:40:50 26 4
gpt4 key购买 nike

我编写了以下代码来尝试取消鼠标悬停事件。目标是在当前悬停幻灯片时防止幻灯片事件发生。如果当前幻灯片悬停,是否可以取消幻灯片事件?这是我的代码:

Jquery

<script type="text/javascript">
$(document).ready(function(){
var currentPosition = 0;
var slideWidth = 560;
var slides = $('.slide');
var numberOfSlides = slides.length;

// Remove scrollbar in JS
$('#slidesContainer').css('overflow', 'hidden');

// Wrap all .slides with #slideInner div
slides
.wrapAll('<div id="slideInner"></div>')
// Float left to display horizontally, readjust .slides width
.css({
'float' : 'left',
'width' : slideWidth
});

// Set #slideInner width equal to total width of all slides
$('#slideInner').css('width', slideWidth * numberOfSlides);

// Insert controls in the DOM
$('#slideshow')
.prepend('<span class="control" id="leftControl">Clicking moves left</span>')
.append('<span class="control" id="rightControl">Clicking moves right</span>');

// Hide left arrow control on first load
manageControls(currentPosition);

// Create event listeners for .controls clicks
$('.control')
.bind('click', function(){
// Determine new position
currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;

// Hide / show controls
manageControls(currentPosition);
// Move slideInner using margin-left
$('#slideInner').animate({
'marginLeft' : slideWidth*(-currentPosition)
});
});


// manageControls: Hides and Shows controls depending on currentPosition
function manageControls(position){
if(position >= 4)
{
position=0;
currentPosition=0;
}

}

function Aplay(){
// Determine new position
currentPosition = currentPosition+1 ;

// Hide / show controls
manageControls(currentPosition);
// Move slideInner using margin-left
$('#slideInner').animate({
'marginLeft' : slideWidth*(-currentPosition)
});
setTimeout(function(){Aplay();},5000);
}
setTimeout(Aplay(),50000);
});
</script>

HTML

<div id="slideshow">
<div id="slidesContainer">
<div class="slide">
<h2>Web Development Tutorial</h2>
<p><a href="#"><img src="img/img_slide_01.jpg" alt="An image that says Install X A M P P for wordpress." width="215" height="145" /></a>If you're into developing web apps, you should check out the tutorial called "<a href="#">Using XAMPP for Local WordPress Theme Development</a>"</p>
</div>
<div class="slide">
<h2>Grunge Brushes, Anyone?</h2>
<p><a href="#"><img src="img/img_slide_02.jpg" width="215" height="145" alt="A thumbnail image that says S R grunge photoshop brushes 6 high resolution grunge brushes by six revisions." /></a>In this layout, I used <a href="#">SR Grunge</a>,</p>
<p>
</div>
<div class="slide">
<h2>How About Some Awesome Grunge Textures?</h2>
<p><a href="#"><img src="img/img_slide_03.jpg" width="215" height="145" alt="A thumbnail image that says grunge extreme 15 free high resolution grunge textures six revisions." /></a>The texture used in this web page is from the Grunge Extreme Textures freebie set by JC Parmley released here on Six Revisions.</p>
<p>You can head over to the <a href="#">Grunge Extreme</a> page to download the texture set or check out Six Revisions' <a href="#">freebie section</a> for even more goodies!</p>
</div>
<div class="slide">
<h2>'Tis the End, My Friend.</h2>
<p><a href="#"><img src="img/img_slide_04.jpg" width="215" height="145" alt="Thumbnail image that says sleek button using photoshop that links to a Photoshop tutorial." /></a></p>
<p> web interface.</p>
</div>
</div>
</div>
<!-- Slideshow HTML -->

</div>

最佳答案

Live demo

用这段代码替换 Javascript 底部:

 var timeout = null;
function Aplay(){
// Determine new position
currentPosition = currentPosition+1 ;

// Hide / show controls
manageControls(currentPosition);
// Move slideInner using margin-left
$('#slideInner').animate({
'marginLeft' : slideWidth*(-currentPosition)
});
timeout = setTimeout(function(){Aplay();},5000);
}
timeout = setTimeout(function(){Aplay();},5000);

$('#slideshow').hover(function(){
clearTimeout(timeout);
},function(){
timeout = setTimeout(function(){Aplay();},5000);
});

解释:

创建一个名为 timeout 的变量,它将包含 setTimeout 事件
$('slideshow').hover(...) 将在鼠标悬停时停止事件,然后在鼠标退出幻灯片时将其重新分配给 timeout

编辑

OP 对以下评论的问题的解决方案:

在html中添加:

<span id="numbering">1</span>

在 JS 中添加这个函数:

function updateNumber(){
$('#numbering').text((+$('#numbering').text())%4 + 1);
}

然后在图片滑动涉及到的函数中调用,所以当点击文字时自动滑动。

Live demo

关于javascript - 在鼠标悬停时取消 Jquery Slider 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24503402/

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