Is there a way to toggle the visibility of images on a webpage without needing a specific trigger or buttons

I've scoured the depths of Google until my eyes hurt. Despite trying numerous codes, I've had no luck at all. I am convinced that there must be a way to make images appear and disappear in a random sequence while always having one image displayed in the same location. There are a total of 4 images involved. My coding is in HTML5, and here's an example snippet:

<body>
<div id="content">
   <figure id="suv">
        <img src="./cadillac_escalade.png" alt="SUV" style="float: right" width="210" height="155">         
   </figure>
   <figure id="car">
        <img src="./ICAR.png" alt="Car" style="float: left" width="220" height="135">
   </figure>
</body>

CSS:

figure
{
display: block;
}

and

.blink {
    animation-duration: 0.5s;
    animation-name: blink;
    animation-iteration-count: infinite;
    animation-direction: alternate;
    animation-timing-function: steps(5, start);
}
@keyframes blink {
    80% {
       visibility: hidden;
    }
}

I attempted the CSS code found on http://jsfiddle.net/r6dje/, with some tweaks from this site. However, I don't want it to just blink – I need the images to truly appear/disappear/reappear. While I consider myself decently skilled in html, for some reason, this particular issue has me stumped! If anyone out there can illuminate this problem and help me get the code running smoothly, I would be eternally grateful. Thank you in advance!

Answer №1

Feel free to check out this CSS animation example for some inspiration :)

@-webkit-keyframes myAnimation {
      0% {background-image: url(img1.jpg);opacity: 0;}
   8.33% {background-image: url(img1.jpg);opacity: 1;}
  16.66% {background-image: url(img1.jpg);opacity: 0;}

     25% {background-image: url(img2.jpg);opacity: 0;}
  33.32% {background-image: url(img2.jpg);opacity: 1;}
  41.65% {background-image: url(img2.jpg);opacity: 0;}

     50% {background-image: url(img3.jpg);opacity: 0;}
  58.31% {background-image: url(img3.jpg);opacity: 1;}
  66.64% {background-image: url(img3.jpg);opacity: 0;}

     75% {background-image: url(img4.jpg);opacity: 0;}
   83.3% {background-image: url(img4.jpg);opacity: 1;}
  91.63% {background-image: url(img4.jpg);opacity: 0;}
    100% {opacity: 0;}
}
.myDiv {
    width: 300px;
    height: 150px;
    background-size: 100% 100%;
    -webkit-animation: myAnimation 5s linear infinite;
    -moz-animation: myAnimation 5s linear infinite;
    -o-animation: myAnimation 5s linear infinite;
}

Check it out for more details...

@-moz-keyframes myAnimation {
    0% { }
   50% { }
  100% { }
}
@-o-keyframes myAnimation {
    0% { }
   50% { }
  100% { }
}


http://jsfiddle.net/Ua8UV/

Answer №3

By combining these images into a CSS sprite, updating the image is as easy as adjusting its background position coordinates. This can be achieved effortlessly using JavaScript with a .css styling command. I bring up this alternative method because it is much more straightforward and there is ample documentation available on this approach. If you are not satisfied with the HTML5 solution, utilizing a sprite could be the way to go.

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

Error: The database connection is currently down

Hey there! I'm currently working on a school project and running into some issues with inserting data into the database. I can't figure out what I'm doing wrong. There are no errors showing up, but for some reason, the information I input i ...

Guide on handling asynchronous data return within a reducer

I'm struggling to properly handle the state when receiving data from the back-end of my application. This is the code I have: if (action.type === actionTypes.getList) { const id = action.payload.userId; Axios.post(`${apiUrl}/lists`, { ...

Creating an embedded object in React JS: A step-by-step guide

Is it possible to render a nested object within another object? Take a look at the code below to see what I mean. https://i.sstatic.net/aLmnQ.png I am trying to display the minimum system requirements objects such as graphics, operating system, storage, ...

What is the method for sorting in MongoDB using the objects in the URL query string?

How can I sort a MongoDB collection in a Node.js application based on the query string provided in the URL? The URL looks like this: http://localhost:5000/v1/bid/sort_bid?sort={"_id": -1,"enquiry_no": 1} I am trying to pass an object i ...

Styling divs to have equal height using CSS borders

My current technique involves using negative margins to create the illusion of equal heights for my horizontal divs. However, adding borders to these divs has created an issue where there is no bottom border due to the combination of padding and negative m ...

Designing three div containers of equal width without any elements being obscured

I currently have three widgets, each containing text of varying lengths. My goal is to ensure that all widgets are the same length, matching the longest text length among them. Specifically, I want the blue and green widgets to extend in length to accomm ...

Choose a specific <div> element by its unique ID and trigger a click event on it as soon as the page loads

There is a div element on my webpage tagged with the ID of #activateeditbutton. I am hoping to have this specific div trigger a click event upon the page's initial loading. In an attempt to achieve this functionality via jQuery, I have written the fo ...

Why is my CSS causing a div with 100% width to be slightly taller than expected?

I am working on a website that I want to adjust to the width of different browsers. Everything seems to be functioning properly, except for the fact that the parent div containing the 100% width divs always appears around 5 pixels too tall. This is causin ...

At times, the jQuery function may fail to execute

Here is a function I have been using to set an iframe height to match its content: $("#MSOPageViewerWebPart_WebPartWPQ1").load(function() { var Height = document.getElementById('MSOPageViewerWebPart_WebPartWPQ1').contentWindo ...

Reconnecting the bone: A guide to reestablishing the mesh skeleton in Three.js after rebind

I am facing a challenge where I need to independently rotate bones on a rigged hand mesh along the global axis using direction vectors to calculate their orientation. To tackle this, I have developed the following code: function boneLookAtLocal(bone, posit ...

The child element with a minimum height of 300 pixels is failing to inherit the height of its parent

Here is the HTML code for a div block I am working with: <div className={'row ge-container'}> <div className={'a-span3 ge-container-navigation'}> hello </div> <div className={'a-span9 ge-containe ...

Using JQuery to verify if a certain letter or word has been entered by the user in a textarea

Can anyone assist me in checking if a user has entered a specific letter or word and, if correct, display a button? I would appreciate any guidance! Here is my current code snippet: $(document).ready(function() { $(".textArea").keyup(function() { ...

Does the array.sort() method behave in-place when used on Object.keys()?

When utilizing the .sort() method of JavaScript arrays, the array is altered directly and a reference to the array is returned. Object.keys(obj) generates an array with all the keys from object obj. It's important to note that in JavaScript, object ...

Express.js: Request body with an undefined base64 encoding

I'm currently working on transforming a table in .xls format to individual rows in .csv format. I came across a helpful library for this task called XLSX My initial step involves encoding the .xls table into base64 format. Next, I'm attempting ...

Creating an XML file from an HTML form in Django: A step-by-step guide

I am looking to convert the data from an HTML form into an XML file. I am currently using Django and need to transfer the information gathered from the HTML form into my XML file. The following are the fields available in my HTML form: - a checkbox list - ...

The function of jQuery .on will not be effective when applied to elements that have been

Currently, I am experimenting with some functionality to trigger an action when hovering over a specific element (such as a link) in order to display something in the console. I came across this helpful answer here, which mentions that "you can define you ...

Upon clicking the button, the system triggers an update to the database

I am currently working on an interface that involves buttons for updating a database. For example, I have a variable named "Estado" which is initially set as "emAvaliacao". When the button "Aceite" is clicked, the value of "Estado" changes to "Aceite". Th ...

Creating dynamic images in Node.js using JavaScript without relying on HTML5 canvas

Hello everyone, I'm excited to be posting my first question on stackoverflow! Hey there, it's wabsol. I've been diving into the world of nodejs and loving every minute of it. I'm interested in creating a window and/or drawing an image ...

Automatically opening a link upon page load using JavaScript

I am attempting to write a JavaScript code that will automatically click on the link with the ID "bigPicLink" when the page loads, but I am having trouble getting it to work. I am new to JavaScript and would greatly appreciate any assistance. Thank you. ...

Tracking time using JavaScript or PHP

I am trying to create a time countdown feature using either JavaScript or PHP. Despite searching on Google and attempting it myself, I have not been able to achieve the desired outcome. My goal is for users to input a time in a text box, after which they ...