Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Javascript

Featured Replies

You can do it using setInterval()

 

HTML:

<img src="http://oi59.tinypic.com/245wk5h.jpg" class="js-image-swap" />

JavaScript:

(function() {
    var imgs = [
            "http://oi57.tinypic.com/245njlt.jpg",
            "http://oi59.tinypic.com/245wk5h.jpg"
        ],
        img = document.querySelector(".js-image-swap"),
        counter = 0;
    
    setInterval(function() {        
        img.setAttribute("src", imgs[counter]);
        counter = (counter === imgs.length-1) ? 0 : counter+1;
    }, 10000);   
    
})();

Here's an example: http://jsfiddle.net/Lp1y77xf/

  • Author

Can I add 3/4/5 images to swap with that example?

Can I add 3/4/5 images to swap with that example?

 

Yes, simply add to the imgs[] array, like so:

var imgs = [
"http://oi57.tinypic.com/245njlt.jpg",
"http://oi59.tinypic.com/245wk5h.jpg",
"Image 3 src",
"Image 4 src",
"Image 5 src"
]
  • Author

Where does the HTML bit go? I got the current background in the body style tag.

I used a standard image. You would need to do something different to target a background image.

<img src="http://oi59.tinypic.com/245wk5h.jpg" class="js-image-swap" />
  • Author

Maybe I should finish my KFC before I attempt this lol.. not sure if I'm doing it right.

This will do it:

(function() {
    var imgs = [
            "http://oi57.tinypic.com/245njlt.jpg",
            "http://oi59.tinypic.com/245wk5h.jpg"
        ],
        swap = document.querySelector(".js-image-swap"),
        counter = 0;
    
    setInterval(function() {        
        swap.style.backgroundImage = "url('" + imgs[counter] + "')";
        counter = (counter === imgs.length-1) ? 0 : counter+1;
    }, 10000);   
    
})();

http://jsfiddle.net/LyndseyB/Lp1y77xf/1/

  • Author

And the javascript bit goes in <script type="text/javascript"> and </script> right?

And the javascript bit goes in <script type="text/javascript"> and </script> right?

 

Yes, or in an external file :)

  • Author

And the CSS bit in <style type="text/css"> and </style>?

 

Just trying to figure out why it's not worked..

Use the updated code which updates the CSS of the body, not the src of the image:

(function() {
    var imgs = [
            "http://oi57.tinypic.com/245njlt.jpg",
            "http://oi59.tinypic.com/245wk5h.jpg"
        ],
        swap = document.querySelector(".js-image-swap"),
        counter = 0;
    
    setInterval(function() {        
        swap.style.backgroundImage = "url('" + imgs[counter] + "')";
        counter = (counter === imgs.length-1) ? 0 : counter+1;
    }, 10000);   
    
})();
  • Author

Ok.. just did that.. but nothing changes.

Ok. My fault, sorry. Move the script to the bottom of the page, just before the end body tag:

    <script>
    (function() {
        var imgs = [
            "http://oi57.tinypic.com/245njlt.jpg",
            "http://oi59.tinypic.com/245wk5h.jpg"
            ],
            swap = document.querySelector(".js-image-swap"),
            counter = 0;
    
        setInterval(function() {        
            swap.style.backgroundImage = "url('" + imgs[counter] + "')";
            counter = (counter === imgs.length-1) ? 0 : counter+1;
        }, 10000);      
    })();
</script>
</body>
</html>

Edited by Lyndsey

  • Author

One thing I've just noticed.. I put the same script on other pages.. but nothing happens?

 

(Edit: I forgot the class bit)

Edited by Sneijder

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.