最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Simple JavaScript image rotator - Stack Overflow

programmeradmin0浏览0评论

I have a simple image rotator on a website consisting of 4 images that have to appear for a few seconds before showing the next one. It seems to work on its first cycle but then when it gets back to the first image it doesn't show that one but works again from the second image and so on always missing that image on every cycle. The function is called using onLoad EH in the body. In the body there is an img with my first image inside it. I'm a noob so please be gentle if I've missed anything out. Here's what I have...

<body onLoad="sunSlideShow()">

    <img src="IMAGES/slider1.gif" alt="slide-show" id="mySlider" width="900">

<body>




var quotes = new Array ("slider2.gif", "slider3.gif" ,"slider4.gif", "slider1.gif");

var i = 0


function sunSlideShow()
{
    document.getElementById("mySlider").src = ( "IMAGES/" + quotes[i] );

    if (i<4)
    {
        i++;
    }

    else

        i = 1;

    setTimeout("sunSlideShow()", 3000);

}
sunSlideShow()

I have a simple image rotator on a website consisting of 4 images that have to appear for a few seconds before showing the next one. It seems to work on its first cycle but then when it gets back to the first image it doesn't show that one but works again from the second image and so on always missing that image on every cycle. The function is called using onLoad EH in the body. In the body there is an img with my first image inside it. I'm a noob so please be gentle if I've missed anything out. Here's what I have...

<body onLoad="sunSlideShow()">

    <img src="IMAGES/slider1.gif" alt="slide-show" id="mySlider" width="900">

<body>




var quotes = new Array ("slider2.gif", "slider3.gif" ,"slider4.gif", "slider1.gif");

var i = 0


function sunSlideShow()
{
    document.getElementById("mySlider").src = ( "IMAGES/" + quotes[i] );

    if (i<4)
    {
        i++;
    }

    else

        i = 1;

    setTimeout("sunSlideShow()", 3000);

}
sunSlideShow()
Share Improve this question edited Feb 14, 2014 at 11:35 James 2,2011 gold badge20 silver badges22 bronze badges asked Feb 14, 2014 at 11:14 Orbital676Orbital676 651 silver badge8 bronze badges 2
  • 1 Change i=1 to i=0. – ElendilTheTall Commented Feb 14, 2014 at 11:17
  • Damn! Why didn't I see that. Thanks. – Orbital676 Commented Feb 14, 2014 at 11:20
Add a ment  | 

5 Answers 5

Reset to default 2

Change it to this:

else i = 0; setTimeout("sunSlideShow()", 3000);

Further to my other answer (which was wrong!)... Try this:

http://jsfiddle/pq6Gm/13/

<script src="//ajax.googleapis./ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        setInterval(sunSlideShow,3000);
    });
    var quotes = [
        "http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2007/07/11/sun128.jpg",
        "http://icons.iconarchive./icons/robinweatherall/seasonal/128/sun-icon.png",
        "http://www.astronomytoday./images/sun3.gif",
        "http://mariusbancila.ro/blog/wp-content/uploads/2011/08/sun.png"
    ];
    var i = 0;
    function sunSlideShow() {
        document.getElementById("mySlider").src = quotes[i];
        if (i < (quotes.length-1))
            {
                i++;
            }
        else
            {
                i = 0;
            }
    }
</script>
<body>
    <img src="http://mariusbancila.ro/blog/wp-content/uploads/2011/08/sun.png" id="mySlider"/>
</body>

==================================================================

EDIT: This is wrong... please find my other answer on this page.

==================================================================

To start with, I wouldn't use ... you're better off starting the script with jquery once the page is loaded.

Add this to your head section:

<script type="text/javascript">
    $(function () {
        sunSlideShow();
    }
</script>

That will fire the sunSlideShow function once the page is loaded.

Then, you're starting your slideshow with var i = 0... but when you've got to the fourth image, you're setting it to 1?

I would be tempted to use a while loop to achieve what you want.

Something like this:

<script type="text/javascript">
$(function () {
    sunSlideShow();
}
var quotes = new Array ("slider2.gif", "slider3.gif" ,"slider4.gif", "slider1.gif");
var i = 0;
function sunSlideShow(){
    while (i<4)
        {
            document.getElementById("mySlider").src = ( "IMAGES/" + quotes[i] );
            if (i<4)
                {
                    i++;
                }
            else
                {
                    i = 0;
                }
            sleep(3000);
        }
}
function sleep(miliseconds){
    var currentTime = new Date().getTime();
    while (currentTime + miliseconds >= new Date().getTime()){}
}
</script>

This script hasn't been tested... but it should start the sunSlideShow function once the page has loaded and then change the image every 3 seconds.

I too searched the web trying to find a general solution to the problem of rotating an image about its center. I came up with my own solution which works perfectly. The basic concept is simple: rotate the entire context by the desired angle (here called 'tilt'); calculate the image's coordinates in the NEW coordinate system; draw the image; lastly, rotate the context back to its original position. Here's the code:

        var xp = rocketX * Math.cos(tilt) - rocketY * Math.sin(tilt);
        var yp = rocketX * Math.sin(tilt) + rocketY * Math.cos(tilt);
        var a = rocketX - xp;
        var c = Math.sqrt(a*a + (rocketY-yp)*(rocketY-yp));
        var beta = Math.acos(a/c);
        var ap = c * Math.cos(tilt + beta);
        var bp = c * Math.sin(tilt + beta);
        var newX = rocketX + ap;
        var newY = rocketY - bp;
        context.rotate(tilt);
        context.drawImage(littleRocketImage, newX-9, newY-40);          
        context.rotate(-tilt);

In the penultimate line, the constants '9' and '40' are half the size of the image; this insures that the rotated image is placed such that its center coincides with the center of the original image.

One warning: I use this only for first quadrant rotations; you'll have to put in the standard tests for the other quadrants that change the signs of the ponents.

Update: 2021

You can use the light-weight library Ad-rotator.js to setup simple Ad-rotation like this -

<script src="https://cdn.jsdelivr/npm/ad-rotator"></script>
<script>
  const instance = rotator(
    document.getElementById('myelement'),     // a DOM element
      [                                       // array of ads
        { url: 'https://site1.', img: 'https://example/picture1.jpg' },
        { url: 'https://site2.', img: 'https://example/picture1/picture2.jpg'},
        // ...
      ]
  );
</script>

<body onLoad="instance.start()">
    <div id="myelement"></div>
<body>

Reference Tutorial

发布评论

评论列表(0)

  1. 暂无评论