简体   繁体   English

jQuery在同一页面上的多个幻灯片

[英]jquery multiple slideshow on the same page

I'm using a jquery code to display an image slideshow on my website, with a counter. 我正在使用一个jQuery代码在我的网站上显示带有计数器的图像幻灯片。

When clicking on the image, img swicth between show and hide. 单击图像时,img在显示和隐藏之间旋转。

here is the jquery code i'm using : 这是我正在使用的jQuery代码:

$(document).ready(function () {
    var count = $('.image_news').length;
    $("#total").text(count);
    // set display:none for all members of ".pic" class except the first
    $('.image_news:gt(0)').hide();

    // stores all matches for class="pic"
    var $slides = $('.image_news');

    $slides.click(function () {
        // stores the currently-visible slide
        var $current = $(this);
        if ($current.is($slides.last())) {
            $("#current").text("1");
            $current.hide();
            $slides.first().show();
        }
        // else, hide current slide and show the next one
        else {
            $("#current").text($current.next().index()+1);
            $current.hide().next().show();
        }
    });
});

it works fine with one slideshow, but I would like to have several sildeshow on the same page, and I don't know how many so I can't add a unique ID to my images ('.image_news')... is there a way of doing it using $this ? 它可以与一个幻灯片一起正常工作,但我想在同一页面上进行多个sildeshow,而且我不知道有多少个幻灯片,所以我无法向图像添加唯一ID('.image_news')...是有一种使用$ this的方法吗? I need also to have unique counter for each slideshow, and the slideshow to be fully independant... when clicking on first slideshow to navigate, only the first slideshow should slide. 我还需要为每个幻灯片都具有唯一的计数器,并且该幻灯片要完全独立...在单击第一个幻灯片进行导航时,只有第一个幻灯片可以滑动。

hope someone can help me with this, or maybe there's another way of doing it... 希望有人可以帮助我解决这个问题,或者还有另一种方法...

here is a jsfiddle to see it in action : 这是一个jsfiddle,可以看到它的实际效果:

http://jsfiddle.net/XRpeA/19/ http://jsfiddle.net/XRpeA/19/

thanks for your help 谢谢你的帮助

I can't write the codes exactly but I can give ideas to design it. 我不能精确地编写代码,但可以提出一些设计思路。 I think you should think object oriented way. 我认为您应该考虑面向对象的方式。 Because you want to use more than one instance of sliders. 因为您要使用多个滑块实例。 If I were you, I'd do these to get the results you want. 如果我是你,我会做这些以获得所需的结果。

  • Make a slider class whose constructor takes 'class name' as a parameter. 制作一个滑块类,其构造函数将“类名”作为参数。 For example in your situation, you have used "image_news" for class name. 例如,在您的情况下,您已经使用“ image_news”作为类名。 For each of sliders, use different class names. 对于每个滑块,使用不同的类名。 With using this, there will be no longer problem for separation between different sliders in the page. 使用此功能,将不再存在页面中不同滑块之间分离的问题。

  • Define the click method exactly as above. 完全按照上述定义click方法。

I may have forgotten sth but with these design there is no more problem for multiple slider in one page. 我可能已经忘记了某事,但是有了这些设计,一页中的多个滑块不再存在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM