简体   繁体   中英

How to make a simple auto playing slide show with javascript?

I want to make a simple slide show that automatically plays when the page is loaded. Here is my code so far:

HTML:

<div id="slideshow">
   <div>
      <img src="slide_1.png">
   </div>   
   <div>
      <img src="slide_2.png">
   </div>
   <div>
       <img src="slide_3.png">
   </div>
</div>

CSS:

#slideshow {  

    position: relative; 

    width: 900px; 

    height: 450px;  

}

#slideshow > div { 

    position: absolute; 

}

If possible, can someone please provide me with a JavaScript code that will automatically play the slide show, as well as change the slides by using a slide transition, and also have it replay infinite amount of times while the user is still of the page? Also, maybe a navigation that has numbers in the bottom right corner. Here is an example of what I am looking for: ( http://www.http://www.suprafootwear.com/ ). The large slideshow up top is what I want, the only difference is that I want the transition to be a linear slide motion instead of the fade transition. Please maintain the 900px x 450px size. Any help is appreciated. Thank you in advance!

If you want infinite repeat on load with no buttons to control it, no need for JavaScript. Use CSS keyframe animation or transition.

see here:
http://coding.smashingmagazine.com/2012/04/25/pure-css3-cycling-slideshow/ http://www.alessioatzeni.com/CSS3-Cycle-Image-Slider/

看看KickStart http://www.99lime.com/elements/,它正是您所需要的,还有一些其他非常有用的元素可用于快速开发。

Try this simple javascript code to make image slider.

<html>
<head>
    <style>
        .container{
            position:relative;
            width:100%;
            height:300px;
            border-radius:5px;
            border:1px solid red;
            overflow:hidden;
        }
    </style>
</head>
<body>
<div id="imgGallary" class="container">
    <img src="http://www.examiningcalvinism.com/kingdavid.jpg" alt="" width="100%" height="300" />
    <img src="http://www.kingjamesbibleonline.org/1611-Bible/1611-King-James-Bible-Introduction.jpg" alt="" width="100%" height="300" />
    <img src="http://biblestudyoutlines.org/wp-content/uploads/2012/07/the-triump-of-david-over-king-hadadezer-of-zobah-1024x729.jpg" alt="" width="100%" height="300" />
    <img src="http://www.cgu.edu/Images/news/Flame%20Winter12/KJV-BibleBW.jpg" alt="" width="100%" height="300" />
</div>
<script>
(function(){
        var imgLen = document.getElementById('imgGallary');
        var images = imgLen.getElementsByTagName('img');
        var counter = 1;

        if(counter <= images.length){
            setInterval(function(){
                images[0].src = images[counter].src;
                console.log(images[counter].src);
                counter++;

                if(counter === images.length){
                    counter = 1;
                }
            },4000);
        }
})();
</script>
</body>
</html>

$("#slideshow > div:gt(0)").hide();

setInterval(function() { $('#slideshow > div:first') .fadeOut(1000) .next() .fadeIn(1000) .end() .appendTo('#slideshow'); }, 3000);

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