简体   繁体   中英

How to make images a simple slider in js

i only know html, js and css. i'm trying to make the images change every couple of seconds., as in a slideshow.

    <script type="text/javascript">
        var temp=1;
        function slider(){
            document.getElementById("pic1").style.display = 'none';
            document.getElementById("pic2").style.display = 'none';
            document.getElementById("pic3").style.display = 'none';
            if(temp==1){
                document.getElementById("pic1").style.display = 'block';
            }
            else if(temp==2){
                document.getElementById("pic2").style.display = 'block';
            }
            else if (temp==3){
                document.getElementById("pic3").style.display = 'block';
                temp=1;
            }
            temp++;
            setTimeout(slider(),25000);
        }
    </script>

the head is above, body below.

    <div id="rightside" onload="slider()">
        <a id="pic1"><img src="photos/hamilton/candyshop.jpg" style="display:block"></a>
        <a id="pic2"><img src="photos/hamilton/hamiltonboat.jpg" style="display:none"></a>
        <a id="pic3"><img src="photos/hamilton/waterduel.jpg" style="display:none"></a>
    </div>

There are multiple errors in this that must all be fixed for it to work.

  • In the line setTimeout(slider(), 25000) , you should pass slider , the function itself, not slider() , the return value of the function. Then you need to call slider() once after defining it to start the whole thing. You can do this in the JavaScript with document.addEventListener instead of the HTML with onload , making the HTML self-contained.
  • You set the img to display:none in the HTML, and then you set the element with ID pic1 to display: block . But this element isn't the img , it's the a . So you end up with a display: block <a> containing a display: none <img> , so nothing shows after all.
  • When you set temp = 1 , immediately after that you run temp++ , so picture #1 is never seen again. temp = 0 on that line would fix this, but it is better to make temp loop through “0, 1, 2” and use the modulo operator % that makes numbers loop if they are too high.

I also added alt attributes describing each of the images so the demo will work without the images loading. This would help your users too if they can't see the images for whatever reason.

A working version:

 document.addEventListener("DOMContentLoaded", function(event) { var temp = 0; function slider() { document.getElementById("pic1").style.display = 'none'; document.getElementById("pic2").style.display = 'none'; document.getElementById("pic3").style.display = 'none'; if (temp == 0) { document.getElementById("pic1").style.display = 'block'; } else if (temp == 1) { document.getElementById("pic2").style.display = 'block'; } else if (temp == 2) { document.getElementById("pic3").style.display = 'block'; } temp = (temp + 1) % 3; setTimeout(slider, 1500); // decreased delay for demo purposes } slider(); }); 
 <div id="rightside"> <a id="pic1" style="display:block"> <img alt="candy shop" src="photos/hamilton/candyshop.jpg"> </a> <a id="pic2" style="display:none"> <img alt="Hamilton boat" src="photos/hamilton/hamiltonboat.jpg"> </a> <a id="pic3" style="display:none"> <img alt="water duel" src="photos/hamilton/waterduel.jpg"> </a> </div> 

After getting the code working like the above, you can also reduce the repetition by using loops and functions. With the following version, if you add more pictures, you will only need to change one line of code instead of copying and pasting multiple parts of your code. Splitting your code up into functions that are each simple has the additional benefit that the code is easier to understand and to check for errors in.

 document.addEventListener("DOMContentLoaded", function(event) { var currentIndex = 0; var numPictures = 3; function slideshow() { hideAllPictures(); showPicture(currentIndex); currentIndex = (currentIndex + 1) % numPictures; setTimeout(slideshow, 1500); } function hideAllPictures() { for (var i = 0; i < numPictures; i++) { hidePicture(i); } } function hidePicture(index) { getPictureElement(index).style.display = 'none'; } function showPicture(index) { getPictureElement(index).style.display = 'block'; } function getPictureElement(index) { var id = "pic" + (index + 1); return document.getElementById(id); } slideshow(); }); 
 <div id="rightside"> <a id="pic1" style="display:block"> <img alt="candy shop" src="photos/hamilton/candyshop.jpg"> </a> <a id="pic2" style="display:none"> <img alt="Hamilton boat" src="photos/hamilton/hamiltonboat.jpg"> </a> <a id="pic3" style="display:none"> <img alt="water duel" src="photos/hamilton/waterduel.jpg"> </a> </div> 

Try this with Pure Javascript and CSS, change only myimage*.jpg to your image name.

<!DOCTYPE html>
<html>
    <title>My Simple Slider</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        .fading{
            -webkit-animation:fading 10s infinite;
            animation:fading 10s infinite
        }

        @-webkit-keyframes fading {
            0%{opacity:0}
            50%{opacity:1}
            100%{opacity:0
            }
        }

        @keyframes fading {
            0%{opacity:0}
            50%{opacity:1}
            100%{opacity:0
            }
        }
    </style>
    <body>

        <div>
            <p>Simple Image Carousel</p>

            <img class="mySlides fading" src="myimage1.jpg" style="width:100%">
            <img class="mySlides fading" src="myimage2.jpg" style="width:100%">
            <img class="mySlides fading" src="myimage3.jpg" style="width:100%">
            <img class="mySlides fading" src="myimage4.jpg" style="width:100%">

        </div>

        <script>
            var myIndex = 0;
            carousel();

            function carousel() {
                var i;
                var x = document.getElementsByClassName("mySlides");
                for (i = 0; i < x.length; i++) {
                    x[i].style.display = "none";
                }
                myIndex++;
                if (myIndex > x.length) {
                    myIndex = 1
                }
                x[myIndex - 1].style.display = "block";
                setTimeout(carousel, 9000);
            }
        </script>

    </body>
</html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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