Jump to content

randomly cycle a banner background image on page load?

Recommended Posts

  • Replies 6
  • Views 2.3k
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted Images

nevermind! I got it to work by taking the image out of the banner and setting background properties with CSS. I've seen some other folks struggling with this so here's what worked for me:

CSS

#page{
  background: #cacaca;
  background-attachment: fixed;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  background-blend-mode: multiply;
}

-style your background however you want. I like the parallax effect and added an overlay so the text on top would stand out more

 

footer code injection:

<script type="text/javascript">

    var images = ['image.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg', 'image5.jpg'];              
    document.getElementById("page").style.backgroundImage = 'url(" ' + images[Math.floor(Math.random() * images.length)] + '")';
</script>

-you'll have to find the urls for your images and replace image.jpg above with the full url, and replace "page" with whatever your page id is.

good luck!

 

Link to comment
  • 1 year later...
  • 7 months later...
14 hours ago, AndrejB said:

@Skwolven Thanks for sharing the script. I'm trying to implement it on SQSP 7.1 with no luck. Can someone help?

You want this for Banner Image or Banner Video? With banner image, I remember I solved a similar case.

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Link to comment
  • 5 months later...
  • 5 months later...
On 2/25/2023 at 3:14 AM, rGRAYphotog said:

@tuanphan Do you have the solution to do random banner image in 7.1? Thank you

Just an update

You can use a Gallery Section - Strips Layout (we can give code to make it 1 item/row, fullwidth, then use this code to Code Injection > Footer to make image random on page load

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<script>
 $( document ).ready(function() {
   $('.gallery-strips-wrapper').randomize('.gallery-strips-item');
 });
 (function($) {
   $.fn.randomize = function(childElem) {
       function shuffle(o) {
           for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x =
o[--i], o[i] = o[j], o[j] = x);
           return o;
       };
       return this.each(function() {
           var $this = $(this);
           var elems = $this.children(childElem);
           shuffle(elems);
           $this.detach(childElem);
           for(var i=0; i < elems.length; i++) {
               $this.append(elems[i]);
} });
   }
})(jQuery);
</script>

 

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment


×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.