简体   繁体   English

jQuery:从第一个开始隐藏并隐藏所有图像,然后滚动

[英]JQuery: Hide all images except one, starting from the first one and scrolling it

I can't change the HTML code, and it looks like this: 我无法更改HTML代码,它看起来像这样:

<div class="slider">
            <div class="slider-control">
                <button id="prev">Previous</button>
                <button class="foll">Next</button>
            </div>
            <div class="slider-image">
                <img src="html.png" alt="Html" />
                <img src="css.png" alt="Css" />
                <img src="jquery.png" alt="jQuery" />
            </div>
        </div>

When the program starts I need to show the first image. 程序启动时,我需要显示第一张图像。 If I click on "Previous" I want to show the previous img (if the currently img showed is the first one, I want to show the last one), if I click on "Next" then I want to show the next img. 如果单击“上一个”,则要显示上一个img(如果显示的当前img是第一个,则要显示最后一个),如果单击“下一个”,则要显示下一个img。 I need it to be a generic solution case I need to use it also with more images. 我需要将其用作通用解决方案案例,也需要在更多图片中使用它。 I tried with: 我尝试过:

function showImage(){
    $(".slider-image img").not(":eq(n)").hide();
}

$(document).ready(function(){
    n = 0;
    showImage(n);
    $(".slider-control button").click(function(){

        if($(this).is("div.slider-control.prev")){
            n -= 1;
            showImage(n);
        }
        else if($(this).is("div.slider-control.foll")){
            n += 1;
            showImage(n);
        }   
    });
});

But it doesn't show anything 却什么也没显示

There was a few issues that I've fixed for you, but this should now work okay for you 我已经为您解决了一些问题,但是现在对您来说应该可以


showImage was not taking n as a paramater, but you were passing it that when you were calling it showImage并未将n用作参数,但是您在调用它时传递了它

function showImage() {... // Not taking the param you're passing
function showImage(n) {... // Now we are, and assigning it the name 'n'

You weren't using .show() on the element you wanted to show - you were only ever using .hide() 您没有在要显示的元素上使用.show() -您只曾使用.hide()

There was also an issue with how spamming the next or previous buttons would change the n higher / lower than your element count, and so you would end up selecting elements that didn't exist. 垃圾邮件的nextprevious按钮如何将n更改为高于/低于元素数量的问题,因此您最终将选择不存在的元素。 To keep your n within the range of your elements, you can do this to loop back around an array's indexes 为了使n保持在元素范围内,可以执行此操作以循环返回数组的索引

n = n % $('.slider-image img').length;

Finally, you weren't passing n to the :not(:eq(n)) - I've just used template literals to insert the variable cleanly 最后,您没有将n传递给:not(:eq(n)) -我只是使用模板文字来干净地插入变量


In the HTML, I set your buttons to both use IDs, because I felt this made more sense and helps readabilit 在HTML中,我将您的按钮设置为都使用ID,因为我觉得这样做更有意义并有助于重新阅读


In the $(document).ready(... , you also had an error with your .is() - You were alredy working from the <button> element, so you're okay to check just on the ID of the buttons $(document).ready(... ,您的.is()也出现错误-您在<button>元素上工作非常糟糕,因此可以只检查按钮的ID

 function showImage(n){ n = n % $(".slider-image img").length; $(".slider-image img").not(`:eq(${n})`).hide(); $(".slider-image img").eq(n).show(); } $(document).ready(function(){ n = 0; showImage(n); $(".slider-control button").click(function(){ if($(this).is("#prev")){ n -= 1; showImage(n); } else if($(this).is("#foll")){ n += 1; showImage(n); } }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="slider"> <div class="slider-control"> <button id="prev">Previous</button> <button id="foll">Next</button> </div> <div class="slider-image"> <img src="https://via.placeholder.com/150" alt="Html" /> <img src="https://via.placeholder.com/200" alt="Css" /> <img src="https://via.placeholder.com/250" alt="jQuery" /> </div> </div> 

this is he jquery code you, I hope you'll understand it easily, just replace the images, and change the style as you expected 这是他用jquery编码的代码,希望您能轻松理解它,只需替换图像并按预期更改样式

 sliderInt=1; sliderNext=2; $(document).ready(function() { $("#slider > img#1").fadeIn(300); startSlider(); }) function startSlider() { count = $("#slider > img").size(); //run function every 3 secs loop = setInterval(function() { if(sliderNext > count) { sliderNext = 1; sliderInt = 1; } $("#slider > img").fadeOut(300); $("#slider > img#" + sliderNext).fadeIn(300); sliderInt = sliderNext; sliderNext++; }, 3000); } function prev() { newSlide = sliderInt -1; showSlide(newSlide); } function next() { newSlide = sliderInt +1; showSlide(newSlide); } function showSlide(id) { stopLoop(); if(id > count) { id = 1; } else if(id < 1) { id = count; } $("#slider > img").fadeOut(300); $("#slider > img#" + id).fadeIn(300); sliderInt = id; sliderNext = id + 1; startSlider(); } function stopLoop() { //stop the loop you created in setInterval window.clearInterval(loop); } $("#slider > img").hover( function() { stopLoop(); }, function() { startSlider(); } ); 
 .wrapper { width:600px; margin: 0 auto; } #slider { width: 600px; height: 400px; overflow: hidden; margin: 30px auto; } #slider > img { width: 600px; height: 400px; float: left; display: none; } a { padding: 5px 10px; background-color: #F0F0F0; margin-top: 30px; text-decoration:none; color: #666; } a.left { float:left; } a.right { float:right; } 
 <!-- http://www.getupanddosomething.org/wp-content/uploads/2013/11/220.jpg http://images2.fanpop.com/image/photos/9400000/Aaaaaawwwwwwwwww-Sweet-puppies-9415255-1600-1200.jpg http://offleashk9training.com/dogtrainingblog/wp-content/uploads/2014/04/Sweet-puppy-with-bunny-puppies-14749075-1600-1200.jpg --> <div class="wrapper"> <div id="slider"> <img id="1" src="http://www.getupanddosomething.org/wp-content/uploads/2013/11/220.jpg"/> <img id="2" src="http://images2.fanpop.com/image/photos/9400000/Aaaaaawwwwwwwwww-Sweet-puppies-9415255-1600-1200.jpg"/> <img id="3" src="http://offleashk9training.com/dogtrainingblog/wp-content/uploads/2014/04/Sweet-puppy-with-bunny-puppies-14749075-1600-1200.jpg"/> </div> <!-- return false stops it from going to the link "#" --> <a href="#" class="left" onclick="prev(); return false;">Previous</a> <a href="#" class="right" onclick="next(); return false;">Next</a> </div> 

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

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