简体   繁体   English

在同一页面和jQuery上具有相同类的多个幻灯片显示

[英]multiple slideshow with same class on the same page and jquery

the below code is html code: 下面的代码是html代码:

 <div class="slide">
        <button class="next" name="next">Next</button>
    <img src="../pic/09.jpg"/>
    <img src="../pic/429716731_202913.jpg"/>
    <img src="../pic/431701709_3813.jpg"/>
    </div>
    <div class="slide">
        <button class="next" name="next">Next</button>
    <img src="../pic/09.jpg"/>
    <img src="../pic/429716731_202913.jpg"/>
    <img src="../pic/431701709_3813.jpg"/>
    </div>
    <div class="slide">
        <button class="next" name="next">Next</button>
    <img src="../pic/09.jpg"/>
    <img src="../pic/429716731_202913.jpg"/>
    <img src="../pic/431701709_3813.jpg"/>
    </div>

the below code is jquery code: 下面的代码是jquery代码:

$(document).ready(function(){
            var slides = $(".slide img");
            slides.hide();
            var len = slides.length;
            var current = 0;
            slides.eq(current).show();
            $(".next").click(function(){
                slides.hide();
                slides.eq(current++).show();
            });
        });

when i click the next just first div is show image not other. 当我单击下一个只是第一个div是显示图像而不是其他。 i want working for other divs.i want when i click on next on same div just change the image on the same div not all.please help me. 我想为其他div工作。我想当我在同一div上单击下一步时,只需更改同一div上的图像即可。

Try something like this. 尝试这样的事情。 It's a few minor adjustments to what you have: 对您所拥有的内容进行了一些小的调整:

$(document).ready(function(){
    $(".slide img").hide();
    $(".slide").each(function() {
        $(this).find("img:first").show();
    });  
    $(".next").click(function() {
        var currentImg = $(this).parent().find("img:visible");
        $(currentImg).hide();
        $(currentImg).next("img").show();
    });
});

And here's the working jsFiddle . 这是工作中的jsFiddle Hope it helps! 希望能帮助到你!

My answer is a little overkill, but if you look at the event listeners for the prev and next, you'll see that they're pretty similar to what you're already doing. 我的回答有点过大,但是如果您查看事件侦听器的上一个和下一个,您会发现它们与您已经在做的非常相似。 Kind of. 的种类。 One word of advice is make sure you capture the "I've gone PAST the next one" scenario, by redisplaying the first. 一个建议是,确保您通过重新显示第一个场景来捕获“下一个场景我已经过去了”。

The rest of my answer is in the interest of completeness. 我剩下的答案都是为了完整性。 The slide controls are a programmatic element, and thus I removed them from the HTML itself. 幻灯片控件是一个编程元素,因此我将它们从HTML本身中删除了。 I take in a div containing a collection of image elements, and I create the slideshow structure around them. 我进入一个包含图像元素集合的div,然后围绕它们创建幻灯片结构。 Then, after all the slideshow elements have been initialized, I create the event listeners. 然后,在所有幻灯片元素都已初始化之后,我创建了事件侦听器。 The advantage to this approach is, I know that I'll have a next and previous button, as I've created them. 这种方法的优点是,我知道创建它们后将拥有一个下一个和上一个按钮。 I'm not relying on someone to make sure they remember to put them into the HTML. 我并不是依靠某人来确保他们记得将其放入HTML。

Again, the ONLY sections you really ask about are the event listeners (the $(".nextBtn").on("click", function(){...} part. The rest is just because I'm a perfectionist. Ish. ;) 同样,您真正要问的唯一部分是事件侦听器( $(".nextBtn").on("click", function(){...}部分。其余部分只是因为我是一个完美主义者。伊什。

 $(document).ready(function() { var slideShowEls = $(".slide"); /*** * this function will create all the slideshow * functionality, given a div with a collection * of image elements. ***/ var createSlideShow = function createSlideShow(elements) { // first, we want to initialize the slideshow. // this will mean moving the images into a container, // and adding a div containing the slideshow controls. $(elements).each(function() { // Gather all images in this div. var slideEls = $(this).children("img"); // create two divs: one for controls and one for slides. // The controls div will contain a prev and next button. var slideControls = $("<div>").addClass("slideShowControls"); var prevBtn = $("<button>") .addClass("prevBtn") .text("Prev"); var nextBtn = $("<button>") .addClass("nextBtn") .text("Next"); slideControls.append(prevBtn, [nextBtn]); // The slides div will be the new home of the // slide els from above. var slideContents = $("<div>").addClass("slideContents"); slideEls.detach(); slideContents.append(slideEls); slideEls.hide(); slideEls.first().show(); // both newly created divs are placed in the slideshow container. $(this).append(slideControls, [slideContents]); }) // End .each(), the initialization routine. // Now, we need to create the listeners for the // next and prev clicks. $(".nextBtn").on("click", function() { // We need to get the slides container... var slidePane = $(this).parent().siblings(".slideContents"); // ... hide the visible slide and show the next one. slidePane.find("img:visible").hide() .next().show(); // If no slide is currently showing, there WAS no next one. // Redisplay the first one. if (!slidePane.find("img").is(":visible")) slidePane.children("img").first().show(); }); $(".prevBtn").on("click", function() { // We need to get the slides container... var slidePane = $(this).parent().siblings(".slideContents"); // ... hide the visible slide and show the previous one. slidePane.find("img:visible").hide() .prev().show(); // If no slide is currently showing, there WAS no prev one. // Redisplay the last one. if (!slidePane.find("img").is(":visible")) slidePane.children("img").last().show(); }); } // Run the initialize routine for all slideshow divs. // This will create the slideshow structure and implement and // handle event listeners. createSlideShow(slideShowEls); }); 
 .slide { width: 200px; height: 70px; border: 1px solid black; } .slide img { width: 50px; height: 50px; border: 1px dotted blue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="slide"> <img src="../pic/09.jpg" /> <img src="../pic/429716731_202913.jpg" /> <img src="../pic/431701709_3813.jpg" /> </div> <div class="slide"> <img src="../pic/09.jpg" /> <img src="../pic/429716731_202913.jpg" /> <img src="../pic/431701709_3813.jpg" /> </div> <div class="slide"> <img src="../pic/09.jpg" /> <img src="../pic/429716731_202913.jpg" /> <img src="../pic/431701709_3813.jpg" /> </div> 

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

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