Show Random Section

Hey there! I'm currently working on randomizing the display of these divs on my homepage. The specific CSS I have in place is making it a bit tricky, so I haven't been able to implement any of the usual methods for randomizing images. Any suggestions or assistance would be greatly appreciated!

<div class="banner-images">

<div class="feature" style="background-image: url(images/typewriter.png)">
    <img  class="hero-poster" src="images/typewriter.png" />

    <div class="hero-decoration">
        <div class="side-l"></div>
        <div class="arrow"><img alt="Hero-triangle" src="images/triangle.png" /></div>
        <div class="side-r"></div>
    </div>
</div>

<div class="feature" style="background-image: url(images/printing.png)">
    <img  class="hero-poster" src="images/printing.png" />

    <div class="hero-decoration">
        <div class="side-l"></div>
        <div class="arrow"><img alt="Hero-triangle" src="images/triangle.png" /></div>
        <div class="side-r"></div>
    </div>
</div>

<div class="feature" style="background-image: url(images/newspaper.png)">
    <img alt="Releaser" class="hero-poster" src="images/newspaper.png" />

    <div class="hero-decoration">
        <div class="side-l"></div>
        <div class="arrow"><img alt="Hero-triangle" src="images/triangle.png" /></div>
        <div class="side-r"></div>
    </div>
</div>
</div>

Answer №1

Here is a possible solution:

$(document).ready(function() {
    var randomNumber = Math.floor(Math.random()*3);
     var chosenElement = $('.feature')[randomNumber];
     $(chosenElement).show();
});

This code generates a random number and displays the element associated with that number.

JSFiddle Example

Answer №2

I decided to use a random array shuffle function that I stumbled upon online instead of creating one from scratch. It's a convenient way to achieve the desired outcome. Essentially, you would need to take the array of elements with the class name $('.feature'), shuffle them, and then display them in a randomized sequence.

Here is an example of how it can be implemented:

$(function() {
    var shuffle = function(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;
    }
    var showArr = shuffle($('.feature'));
    $.each(showArr, function(k,v) { //key, value
        window.setTimeout(function() {
            $(v).css('opacity', 1);
        },k*1000);
    });
});

Feel free to experiment and have fun by trying it out on this demo: http://jsfiddle.net/9bva204y/1/

Remember, this is just a basic illustration. You can customize and enhance it further to suit your preferences.

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

Executing a protractor test on Safari results in the message "Angular was not located on the webpage https://angularjs.org/"

Recently, I encountered an issue while trying to run a Protractor test on Safari, even when following the official example provided at http://www.protractortest.org/. Below are my conf.js and todo-spec.js files. The problem arises when I set browser.ignor ...

The function FileReader() is not functioning properly within a Vue computed property

I'm attempting to display a set of image thumbnails by dragging images onto the screen. Here is an example of my data structure: data() { return { files: [Image1, Image2, Image3] } } ...where each Image is in a blob format. Below is my co ...

The background image spanning across the entire screen, not just confined to the center

view image details here Can anyone explain why my background image for the main game is displaying like this? I want to set it for the entire screen as I have different backgrounds planned for the menu of the game and the game itself. Any suggestions to ma ...

Using PHP code in CSS for the background styling

I'm having trouble with this line of CSS... background-image:url(upload/<?php echo $array['image']; ?>); It's not working properly, and all I see is a blank screen. The image exists in the database and on the server in the corre ...

Ways to generate a fixed-length string without relying on the rand() function

Is it possible to create a fixed length string without relying on the rand() function? I currently have a method for generating random strings, but I would prefer not to use the rand() function function generateRandomString($length =6) { $characters ...

"Have you ever noticed that the countdown date and time on your computer references the time from your personal device rather than the server

When using this code for a countdown date time, the timer references the time from the user's PC rather than the server. How can this be changed? Currently, when I change the date time on my PC, the countdown timer also changes accordingly (since it ...

Help needed with PHP, MYSQL, and AJAX! Trying to figure out how to update a form dynamically without triggering a page refresh. Can anyone

Hey there! I'm fairly new to the world of dynamically updating databases without needing a page refresh. My goal is to build something similar to The end result I'm aiming for includes: Dynamically generating fields (Done) Loading existing dat ...

Receive alerts in Swift from WKWebView when a particular screen is displayed in HTML

Check out this sample HTML file I have. I'm currently using the WKWebView to display this HTML. My goal is to receive a notification in our Swift code when the user finishes the game and the "high score" screen appears, so we can dismiss the view and ...

Transitioning to the Bootstrap library from the jQuery library with the use of several image modals

After coming across this specific question about implementing multiple image modals on a webpage, I noticed that it primarily focused on javascript and jQuery. However, my project involves utilizing the latest version of Bootstrap, so I'm curious if t ...

Adjustable Title in Line Plot

After receiving a JSON object from a web server in response, I am encountering some challenges with extracting and displaying the data. The JSON array consists of monthly key-value pairs like {"JAN":"17"}, {"FEB":"19"}, {"MAR":"21"}, etc. To display these ...

Sliding and repositioning elements using jQuery

My goal is to build a simple slideshow for learning purposes. I want the list items to slide automatically to the left in a continuous loop. I've written some code that makes the slides move, but I'm struggling to set the correct position for ea ...

Switch up the text inside a div using jQuery

I am developing a small grocery shopping application. My goal is to create a page where users can view the contents of their shopping cart by clicking a button. I have designed a template that displays the contents in a white space. The idea is to store ...

The BottomNavigation component in MUI has a minimum size limit of 400px when it displays 5 items

My issue involves a bottom navigation bar with 5 elements. When the window is resized to less than 400px, the bottom navigation does not shrink, instead maintaining a minimum width of 400px and causing a scrollbar to appear on the x-axis. You can view a m ...

Storing distinct values in separate variables within a JavaScript for loop

I'm attempting to assign various values generated by an outer loop to different variables and then utilize those variables as an array. var code = []; for (i = 0; i < 5; i++) { code[i] = mp3; } Is there a way to access variables in this manner ...

Canvas is unable to duplicate image content

I am trying to duplicate a PNG captcha image in order to remove its transparency so that the captcha resolver (ocrad.js) function can work properly. However, I am facing an issue where the duplicated captcha image is being moved and resized. How can I ke ...

How can I add an item to an array within another array in MongoDB?

I currently have a Mongoose Schema setup as follows: const UserSchema = new mongoose.Schema({ mail: { type: String, required: true }, password: { type: String, required: true }, folders: [ { folderName: { type: S ...

What is the best way to trigger a tooltip when an input field is clicked?

I want to implement a form that displays tooltips whenever a user clicks or focuses on an input field, and the tooltip should disappear when the user clicks elsewhere. While I understand the basics of using JavaScript's getElementById and click event ...

Unable to extract tabular information using BeautifulSoup on a specific webpage

I recently came across a helpful tutorial online about web scraping with Python using Beautiful Soup. The tutorial can be found at this link. Following the steps in the tutorial, I successfully scraped data from an HTML table. However, when I attempted to ...

How to stop Accordion from automatically collapsing when clicking on AccordionDetails in Material-UI

I am working on a React web project with two identical menus. To achieve this, I created a component for the menu and rendered it twice in the App component. For the menu design, I opted to use the Material UI Accordion. However, I encountered an issue wh ...

Solutions for Showing Pop-up Tabs in React Native Webview

I have a website that displays content based on user subscription status. If the subscription is active, a book is loaded; otherwise, if it's expired, a specific page is shown: https://i.sstatic.net/HLv0B.png To enhance user experience, I preload th ...