jQuery .each() .hover() 幻灯片
所以我担心这是另一个让 Stack 混乱的幻灯片问题,但我对 jQuery 和 javascript 还很陌生,并且想尝试自己写一些东西而不是使用插件等。我需要的是非常直接的转发内容:
几个包含类名为 .slideshow 的图像的框,因此:
<div class="box b1">
<a href="#" title="Project Title" class="slideshow 2000">
<img src="img/portfolio/1.jpg" alt="Portfolio Item">
<img src="img/portfolio/2.jpg" alt="Portfolio Item">
</a>
</div>
<div class="box b2">
<a href="#" title="Project Title" class="slideshow 2000">
<img src="img/portfolio/1.jpg" alt="Portfolio Item">
<img src="img/portfolio/2.jpg" alt="Portfolio Item">
</a>
</div>
每个框都愉快地围绕图像循环,然后我将鼠标悬停在它上面并暂停,然后在鼠标移开时继续......简单,对吗? ......
我的 jQuery 目前看起来像这样(我处于无冲突模式......因此 jQuery):
jQuery(".slideshow").each(function(){
var $this_s;
var $that = jQuery(this);
$that.find("img:gt(0)").hide(); //hide all images after first one
function slide($this_s){
$this_s.find(":first-child").hide().next('img').show()
.end().appendTo($that);
}
var int = $that.attr("class");
int = int.replace("slideshow ", "");
interval = setInterval(slide($that), int);
$that.hover(function(){
interval = clearInterval(interval);
},function(){
interval = setInterval(slide($that), int);
});
});
目前它正在做一件奇怪的事情,在 MouseEnter 上它什么都不做,然后 MouseLeave 它更改为下一个图像。我觉得我快到了,我只是不明白为什么 setInterval 不这样做!
我已经尝试过,没有从类名中获取有趣的小间隔,而是在其中插入了一个数字,但仍然没有乐趣。
任何帮助(再次)将不胜感激!
谢谢你!
So I'm afraid this is another one of those slideshow questions that clutters up Stack, but I'm pretty new to jQuery and javascript and wanted to have a go at writing something myself rather than using plugins etc. What I need is pretty straight forward stuff:
Couple of boxes containing images with a class name of .slideshow, thus:
<div class="box b1">
<a href="#" title="Project Title" class="slideshow 2000">
<img src="img/portfolio/1.jpg" alt="Portfolio Item">
<img src="img/portfolio/2.jpg" alt="Portfolio Item">
</a>
</div>
<div class="box b2">
<a href="#" title="Project Title" class="slideshow 2000">
<img src="img/portfolio/1.jpg" alt="Portfolio Item">
<img src="img/portfolio/2.jpg" alt="Portfolio Item">
</a>
</div>
Each one is looping round the images happily then I hover on it and it pauses, then it continues on mouseout... Easy, right? ......
My jQuery currently looks like this (I'm in no-conflict mode...hence jQuery):
jQuery(".slideshow").each(function(){
var $this_s;
var $that = jQuery(this);
$that.find("img:gt(0)").hide(); //hide all images after first one
function slide($this_s){
$this_s.find(":first-child").hide().next('img').show()
.end().appendTo($that);
}
var int = $that.attr("class");
int = int.replace("slideshow ", "");
interval = setInterval(slide($that), int);
$that.hover(function(){
interval = clearInterval(interval);
},function(){
interval = setInterval(slide($that), int);
});
});
At the moment it's doing a peculiar thing where on MouseEnter it does nothing, then MouseLeave it changes to the next image. I feel I'm almost there, I just can't work out why setInterval isn't doing it's thang!
I have tried it without my funny little get interval from classname thing, and stuck a number inthere instead but still no joy.
Any help (again) would be much appreciated!
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setInterval() 使用的第二个参数(延迟)一个 int 值来设置它,您可能需要考虑在设置它时使用
parseInt(int)
,如下所示:或
希望这会有所帮助。
The second parameter (the delay) that setInterval() uses an int value to set it, you may want to consider using
parseInt(int)
when setting it, like so:or
Hope this helps.