How about incorporating a randomized, dynamic background image that adjusts to different screen sizes and proportions

Can anyone help me with adding a responsive background image between the header and footer of my website? I want to make sure the background image changes randomly on each page load from a set of 5 different images.

I've made some progress, but I'm stuck on implementing the changing background image feature.

<body>
  <header style="position: static;">
     <div class="wrapper">
        <div class="logo"><a href="home.html"><img src="images/sh_logo3.png" /></a></div>
        <nav id="nav" role="navigation">
           <a href="#nav" title="Show navigation">Show navigation</a>
           <a href="#" title="Hide navigation">Hide navigation</a>
           <ul>
              <li><a href="home.html" aria-haspopup="true">HOME</a></li>
              <li><a href="shop.html" aria-haspopup="true">SHOP</a></li>
              <li><a href="about.html" aria-haspopup="true">ABOUT</a>
              <li><a href="sizing.html">SIZING</a></li>
              <li><a href="video.html">VIDEO</a></li>
              <li><a href="press.html">PRESS</a></li>
              <li><a href="social.html">SOCIAL</a></li>
              <li><a href="contact.html">CONTACT</a></li>
           </ul>
        </nav>
     </div>
  </header>
  <div class="hero">
     <!-- PLACEHOLDER FOR BACKGROUND IMAGE -->
  </div>
  <div>
     <p style="text-align: center; font-size: 0.9em; font-weight: 400; color: #FAEDE8; letter-spacing: 1px; padding: 2px;">FREE SHIPPING ON ALL ORDERS</p>
  </div>
  <footer>
     <div class="foot-wrap">
        <div class="foot-list-left">
           <ul>
              <li><a href="privacy.html">PRIVACY POLICY</a></li>
              <li><a href="terms.html">TERMS OF USE</a></li>
              <li><a href="customer.html">CUSTOMER SERVICE</a></li>
              <li><a href="orders.html">ORDERS</a></li>
              <li><a href="returns.html">RETURN POLICY</a></li>
           </ul>
        </div>
        <div class="foot-list-right">
           <ul>
              <li>© 2015 SUMMERHEART</li>
           </ul>
        </div>
     </div>
  </footer>
</body>

I would greatly appreciate any assistance on this matter!

Answer №1

To generate a dynamic image display on each page load, I suggest creating a JavaScript array that includes all possible images. Utilize the Math.random function along with Math.round to pick a random image from the array. Below is an example of using jQuery to add the selected image to the 'hero' element:

var images = ['image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg', 'image5.jpg'];
var randomNumber = Math.round(Math.random() * (images.length - 1));

$('.hero').append('<img src="' + images[randomNumber] + '">');

The Math.random function generates a random number ranging between 0 and 5 in this scenario but adjusts according to the image array's size. This randomly chosen index from the array is then appended onto the page during each visit, showcasing a different image every time.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Clicking on the responsive CSS menu causes it to disappear

Recently, I experimented with creating a Responsive Menu using CSS. The menu responds well to different screen resolutions, but there is one issue. When the menu is in wide mode and an anchor link is clicked, the menu quickly disappears and reappears wit ...

What is the method for retrieving the output of a command?

I am working on a project where I am developing a WebApp that is capable of sending commands to Linux servers. To achieve this functionality, I am utilizing Javascript/jQuery on the client side and Node.js on the server side. Below is an excerpt of my code ...

Combining AngularJS with a Single Core CPU can result in nearly maxing out the CPU usage

I developed a robust AngularJS application that had been functioning well up until this point. My issue arose when some users (using ancient computers with single CPUs) began reporting that the application was running unbearably slow. Upon checking the ...

Utilizing the iPhone's numeric keypad with decimal functionality for entering text in a web application

The iPhone when used with type=number and step=any truncates the trailing zeros after the decimal point. Here is a situation: I want to enter an amount as $28.90 in one box and $28.9 (with 90 and 9), but the iPhone truncates both to 28.9. Is there anyon ...

Using SQL to Insert Values into Select/Option

I am attempting to create a select form element with predetermined options. <select name="select1"> <option value="value1">Value 1</option> <option value="value2">Value 2</option> <option value="value3">Value 3 ...

Does the title of a document have any impact on how quickly it is comprehended?

In the process of developing a basic todo app, I encountered an issue with this particular section of code: app.use(express.static(__dirname + '/public')); app.use(express.static(__dirname + '/views')); app.get('/' , (req, ...

Tips for making items move to the next line after floats

Here is the code snippet I'm working with: <div>First</div> <div class="second">Second</div> <a>Alink</a> div{ display: block; float:left; } .second{ clear:right; } http://jsfiddle.net/cg4gj/ I am ...

Develop a reusable block of Vue template when creating a new component

There are times when I find myself needing to repeat certain sections of HTML code in my Template just to keep it DRY. However, creating a new component and passing multiple props and dynamic data seems like too much work. Is there a simpler way to define ...

How to export HTML table information to Excel using JQuery and display a Save As dialog box

This script has been an absolute lifesaver for my web application. Huge thanks to sampopes for providing the solution on this specific thread. function exportToExcel() { var tab_text="<table border='2px'><tr bgcolor='#87AFC6& ...

Is there a way to deactivate keyboard input on an HTML number input field? How about in a React or Material-UI environment?

I am working with an <input> tag that has the attribute type="number", and I want to disable keyboard input so that users are required to adjust the value using the spinner (up and down arrows). This will allow me to consume the input value on each c ...

Sending HTML output to a PHP script

After setting up my HTML form with an action attribute pointing to "filename.php" and a method of post, I managed to generate JSON output by clicking the Submit button using the following code: $('#result').text(JSON.stringify($('form' ...

Methods for eliminating the border of an ASP.NET ImageButton

I am working with C# markup and have the following code snippet: <asp:ImageButton ID="imgBtn" runat="server" CssClass="accordion-icon-close" value="" CommandName="Edit" /> Here is the CSS class associated with it: .accordion-icon-close { ...

I am attempting to use ajax to navigate to the next page, but I am encountering some issues with

I'm trying to figure out how to use jQuery ajax to redirect to the next page. I'm new to jQuery and ajax, so I'm struggling with the syntax. Any help would be greatly appreciated. I've searched for information on this topic, but nothin ...

A guide to resolving the "ActiveRecord::RecordNotFound" error in Rails for a missing Program ID

While conducting an initial test of my Rails API, I encountered an issue when trying to fetch data from my backend and display the results using console.log() in my browser console. The problem arose when attempting to access a list of programs through my ...

Footer is stubbornly stuck in the middle of the page, refusing to budge despite the

visit the live site here Despite setting the body height:100%, my footer continues to get stuck mid-page on certain pages such as about and contact. I have tried specifying the footer height and using position:fixed, position:absolute, and bottom:0, but n ...

Send the HTML form data to Django in JSON format using AJAX

I've created an HTML form that I want to use to post data to a URL using AJAX in JSON format. However, I'm encountering an issue where I'm not receiving the response in Django (backend). Can you please help me identify where I might be maki ...

Tips for consolidating rows with identical data using the R package GT

I have a dataset containing unique identifiers, summary statistics, and individualized comments. The structure of the table is as follows: Name Mean Median Comment Boat 7 5 Can float. Boat 7 5 Has been known to ...

Does the Bootstrap 4 flex system allow for arranging self-row items within a parent column?

Displayed below is the navbar code snippet: <div class="d-flex flex-column flex-lg-row"> ...... </div> As shown in the example: .bd-highlight { background-color: rgba(86,61,124,.15); border: 1px solid rgba(86,61,124,.15); } < ...

Trouble aligning text in circles and fadeToggle not functioning correctly

Hello StackOverflow community! This is my first time posting here, so please excuse any mistakes I may have made while asking my question. :) [Issue 1] I've been trying to center text inside circles by referring to the following posts: Center text ...

Unable to adjust offset/repeat of texture being utilized as alphaMap

demo: the alphaTexture is being modified in its offset with each render. When used as a "map" property, the offset changes, but when used as an "alphaMap" it remains static. This issue arises with the second mesh's alphaMap. relevant code from demo ...