Slowly soften images following a brief pause

Hey there! I have a little project where I am trying to rotate images within a div every few seconds
The code I currently have is functional, but there are a couple of things that I want to tweak and I'm hoping you can assist me with that:

  1. The div adjusts its size based on the current image displayed, but I prefer it to always maintain the size of the larger image.
  2. I would like a smooth fade transition between the images rather than an abrupt switch.

Thank you for taking the time to read this, any help would be greatly appreciated.

This is what my current code looks like:

var imgIndex = 0;
setInterval(function() {
  images[imgIndex].style.display = "none";
  imgIndex++;
  if (imgIndex >= images.length) {
    imgIndex = 0;
  }
  images[imgIndex].style.display = "block";
}, 5000);
.imageDisplay {
  display: inline-block;
  width: 100%;
  background-Color: white;
  color: black;
  border-radius: 5%;
  margin: 2px;
  padding: 1px;
  opacity: 0.5;
  transition-duration: 0.5s;
}

.imageDisplay:hover {
  opacity: 1;
}

.Image {
  width: 99%;
  height: auto;
  margin: 1px;
  padding: 1px;
  border-radius: 5%;
  cursor: pointer;
  display: none;
}
<div class="imageDisplay">
  <p>Description</p>
  <img class="Image"></img>
  <img class="Image"></img>
</div>

Answer №1

To start, consider making the div the same size as the large image and then applying:

object-fit: cover;
object-position:center;

For transitioning between images, you can utilize:

transition: opacity .3s linear;

Explore how to properly incorporate transitions by visiting the provided links. One suggestion is to stack the images and gradually fade them in one by one at intervals.

https://www.w3schools.com/css/css3_transitions.asp

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

Every time I input information into the form, it keeps coming back as undefined

function displayProduct() { var product = document.getElementById("productForm"); var item = product.elements["item"].value ; document.getElementById("outputResult").innerHTML = item ; } <div> <p id="outputResult"></p> </di ...

jQuery's event delegation feature fails to work in conjunction with AJAX requests

I'm currently facing issues with jQuery's on event delegation: Below is the code snippet I am working with: HTML portion: <body> <!-- Page Content Wrapper--> <div id="sb-site"> <div id="overlay">& ...

Is it possible to refresh a div on one .aspx page using content from another .aspx page?

Just beginning my journey with asp.net and currently tackling a project that involves two .aspx pages. Events.aspx: This page is where the site admin can update upcoming events and webinars using an available panel to input event title, date, information ...

Ways to display output on a single line using javascript

It seems like such a simple task, but no matter how I try, I cannot get proper spacing between my words. /s does not seem to work for me. Here is an example of what I have attempted: document.write('You have been alive' + */s userMsDays /s* + &ap ...

Only display icons when hovering over the icon, not the entire div

Attempting to create a pop-out menu where the main icon triggers other icons to slide out left and right upon hover or click. The issue currently is that the pop-out occurs whenever hovering within the div container, rather than just on the main icon (icon ...

Struggling to customize the carousel bootstrap CSS within a CodeIgniter framework

My carousel displays images with varying heights, causing the need to adjust the image height without distorting it. Below is my code snippet: <html lang="en"> <head> <meta charset="UTF-8> <meta name="viewport" content="width=device- ...

Running two blocks of scripts (utilizing lab.js as a loading manager)

I am currently facing an issue where I am trying to load two separate blocks of `lab.js` in different locations. However, when I attempt to utilize functions from the second block that are called from files loaded in the first block, they are showing as un ...

How do I hide a dropdown menu when the selector's value changes in iOS6?

I'm currently developing a hybrid application using Worklight. When I tap on a select control, a native dropdown appears. However, when I choose an option and the onchange event is triggered, the dropdown doesn't disappear unless I tap on the do ...

Search Filter for Live Select Dropdown in Angular JS

I have set up a newsfeed REST API to pull news items, along with a free text instant search feature. Now, I am looking to incorporate a dropdown list search filter to allow users to filter news by author. How can I integrate a dropdown filter in a similar ...

Retrieve a result following an AJAX post request to utilize the obtained value in subsequent code functionalities

Below is the code snippet where an "if" statement is used to check if a query has been executed correctly. If it has, a part of the script is stored in a variable called $output. This variable is then immediately read by the notification script included in ...

How can I replicate a div in Angular 6 using typescript?

I'm currently working on a project focused on providing detailed information about various vehicle components. Each component consists of specific descriptive fields. One feature I'm looking to implement is an "Add another component" button that ...

Fetching data using Axios from a specified URL

I am currently facing an issue with the npm package axios while attempting to execute a get request to a specific URL. The problem arises as I consistently receive an error code 503. Here is the snippet of code in question: let data, response; response = ...

Attempting to rename the "like" button in Django Ajax, however encountering difficulties

My button is currently functioning, but it's embedded within a Django for loop. I want to move the JavaScript logic to a separate file, but before that, I need to rename it appropriately. Check out this excerpt from my code: {% for post in posts %} ...

Is my utilization of Flexbox and media queries incorrect?

Currently, I am practicing flexbox and breakpoints in material UI using React.js and I am attempting to achieve a layout similar to this: https://i.sstatic.net/vAkdo.png My goal is to have the product images and type displayed in columns, while the produc ...

Increasing the value of a TypeScript variable using ngFor in an HTML template can enhance its usefulness

Can you dynamically increase a variable declared in your TypeScript file for each element generated by an ngFor loop in your HTML file? <tbody> <tr *ngFor="let item of invoiceItems"> <td>{{item.ItemName}}</td> <td>{ ...

Using only the $http.get method, you can easily retrieve data in a simple

Here is my message from JSP, I am attempting to extract information from the response. $http.get('url.jsp', data).then(successCallback, errorCallback); I encountered an issue with "data is not defined" https://i.sstatic.net/NX60Y.png Is there ...

Acquiring the Django administrator media files

I'm curious about how to include the JavaScript files from the Django Admin module for a custom view. Can anyone provide guidance on how to achieve this? I've checked the admin templates, but I'm uncertain of the most effective approach to ...

The challenge of using CSS Flexbox to position the logo with a margin-top

While learning flexbox, I encountered a challenge. I aim to center align h1, p, and button elements, with the logo positioned at the top margin-top of 1em. Essentially, I want the h1, p, and button to be centered, while the logo is positioned at the top wi ...

Determine the true size of a string, similar to how we measure with a ruler

My goal is to determine the amount of time it takes for the caret to move from the beginning to the end of a string. Explanation: Consider this string "" in the following code snippet: http://jsfiddle.net/RFuQ3/ If you place the caret before the first ...

Use AngularJS to extract information from Wikipedia and display it

Just starting out with angularjs and attempting to pull data from Wikipedia to display on the front end. I managed to fetch the data using the php code below: $url = 'http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&a ...