jQuery .each() .hover() 幻灯片

发布于 2024-12-02 01:03:54 字数 1568 浏览 1 评论 0原文

所以我担心这是另一个让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久夏青 2024-12-09 01:03:54

setInterval() 使用的第二个参数(延迟)一个 int 值来设置它,您可能需要考虑在设置它时使用 parseInt(int) ,如下所示:

int = int.replace("slideshow ","");
int = parseInt(int);

interval = setInterval(slide($that), 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:

int = int.replace("slideshow ","");
int = parseInt(int);

or

interval = setInterval(slide($that), parseInt(int));

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文