Mastering the art of blending images seamlessly for a captivating cross-fading effect

I'm attempting to create an image cross fade effect using the following steps:

  1. Place the selected image in the front (position: -9999 to 0)
  2. Position the previous image in the back (position: 0 to -9999)
  3. Gradually increase the opacity of the selected image until it reaches 1
  4. Set the opacity of the previous image to 0 (to repeat the process)

Here is the code snippet:

  <a-sky id="pano-1" src="image1" class="pano" opacity="0" position="-9999"></a-sky>
  <a-sky id="pano-2" src="image2" class="pano" opacity="0" position="-9999"></a-sky>

  var opacity = 0; // initial opacity
  var step = 0.1; // step size
  var target = 1; // target value
  var time = 50; // delay in milliseconds

  var increaseOpacity = setInterval(function () {
    opacity = (opacity * 10 + step * 10) / 10;
    if (opacity > target) {
      clearInterval(increaseOpacity);
    }

  }, time);

  $('#pano-${index}').attr('position', 0);
  $('.pano').not('#pano-${index}').attr('position', -9999);
  $('.pano').not('#pano-${index}').attr('opacity', 0);

The images are fading in and out, but not crossing each other (going from the old image to a white screen before the new image appears).

Is the order incorrect? How can I achieve the effect of the images seamlessly crossing over each other?

Answer №1

It appears that you are rearranging the other elements before the fade effect is fully completed. To address this, you can adjust the positioning of the elements as follows:

  var opacity = 0; // initial opacity
  var step = 0.1; // step size
  var target = 1; // target opacity value
  var time = 50; // delay in milliseconds

  var increaseOpacity = setInterval(function () {
    // Set the opacity based on the step size
    opacity = Math.round((opacity + step) * 10) / 10;
    // Check if target opacity is reached and stop the timer
    if (opacity >= target) {
      clearInterval(increaseOpacity);
      $('.pano').not(`#pano-${index}`).attr('position', -9999);
      $('.pano').not(`#pano-${index}`).attr('opacity', 0);
    }
  }, time);

  $(`#pano-${index}`).attr('position', 0);

Another potential issue you may encounter is related to the z-index, but I recommend trying the above solution first.

Answer №2

In my opinion, it is crucial to maintain simplicity in tasks like this. It is advisable to rely on CSS whenever possible and reserve the use of Javascript for handling business logic only.

function SliderCtrl($) {
  var self = this;
  
  self.slider = $('#slider');
  self.imgs = $('img', self.slider);
  self.current = null;
  self.ACTIVE_CLASS = 'is-active';
  self.TIMING = 2000;
  
  self.changeSlide = function() {
    var current = $(self.current);
    var next = current.next().length ? current.next() : self.imgs.first();
    console.log(next, current)
    
        
    current.removeClass(self.ACTIVE_CLASS);
    next.addClass(self.ACTIVE_CLASS);
    self.current = next;
  };
  
  window.setInterval(self.changeSlide, self.TIMING);
  self.slider.removeClass('is-waiting');
}
$(document).ready(SliderCtrl);
#slider {
  width: 300px;
  height: 100px;
  overflow: hidden;
  margin: 0 auto;
  opacity: 1;
  position: relative;
  
  transition: 400ms all linear;
}

#slider img {
  width: 100%;
  height: auto;
  
  transition: 400ms all linear;
  opacity: 0;
  position: absolute;
  top: 0;
  left: 0;
}

#slider img.is-active {
  opacity: 1;
}

#slider.is-waiting {
  opacity: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="slider" class="is-waiting">
  <img src="http://static1.squarespace.com/static/53c46660e4b07557fac2eb85/t/550ee40ce4b02547ba9c325e/1427039245866/doug-rickard_a-new-american-picture_west-gallery-6-b1.jpg?format=1500w" />
  <img src="http://artandyou.ru/upload/mce/image/kerek/vs/portrait/doug-rickard_a-new-american-picture_east-gallery-6-b.jpg" />
  <img src="http://www.yossimilo.com/artists/chris-mccaw/images/full-chris_mccaw-marking_time-west_gallery-13.jpg" />
</div>

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

Having trouble retrieving the value of an HTML input field using form.value in Angular 5?

I am currently working with Angular 5 Within my HTML, I am dynamically populating the value of an input field using: <input type="number" class="form-control" id="unitCost" name="unitCost" [(ngModel)]="unitCost" placeholder="Average Unit Price"> ...

Using the $.ajax() function to retrieve the submitted data within the done() method

Picture this scenario: $('#blah').on('click', function(){ var cat_id = $(this).attr('id'); $.ajax({ // things... data: {cat_id: cat_id}, // <-------------------- // things... }).done(fun ...

The function `canvas.toDataURL()` does not produce an image as output

When I expect the image to return mirrored, it instead shows up as a black image. <!DOCTYPE html> <html> <head> <style> body { margin: 0px; padding: 0px; } </style> </head> <bo ...

What is the best way to arrange card-columns in a horizontal order?

My smart-scrolling list of cards uses the card-columns style, but I find it frustrating that they are ordered top to bottom like this: 1 4 7 2 5 8 3 6 9 This vertical ordering becomes impractical when dealing with a large number of items. When I have 50 ...

Incorporating the <sub> element while maintaining the line height

I am facing a challenge with formatting text that includes subscripts and superscripts tags. For example: <div>hello <sub>world</sub>, how are you? Translated as logical aggregates or associative compounds, these characters have been int ...

Secure animated boxes with jQuery for showing and hiding - indestructible!

My goal is to create a dynamic effect where the content box on a page changes upon hover. Initially, the box displays a heading (or image, or other element). Upon hovering over the box, the original content fades out while new content fades in and the box ...

Why is it necessary to use quotations and plus symbols when retrieving values of objects from JSON?

Initially, when attempting to call the PunkAPI for the first time, I was trying to include images (urls are an object returned in the JSON) by append("<img src='beer[i].image_url'/>"); Unfortunately, this did not work because (according t ...

Struggling to create a CSS dropdown menu where the dropdown items refuse to align to the left

I am currently working on creating a dropdown menu to reveal hidden elements on mobile devices such as smartphones. However, I am facing an issue where the child elements of the dropdown menu are appearing to the right of the parent element instead of the ...

Is it possible to alter the appearance of my menu when clicked on?

Is there a way to update the appearance of an active menu or submenu that is selected by the user? I would like the chosen submenu and its parent menu to have a distinct style once clicked (similar to a hover effect, but permanent). /*jQuery time*/ $(do ...

Even after assigning the class "img-fluid" to my image, it still fails to properly adjust to the screen size

I added the class "img-fluid" to my image in Bootstrap, but it's not fitting on my webpage. What should I do? <img src="images\406201.jpg" class="img-fluid" alt="..."> <div style="margin-top: 0p ...

Is there a way to make all DIVs in the same class have the same height as the tallest div?

I have 3 identical divs with an auto height value. How can I set the height of all divs in the same class to be equal to the highest div's height? Same Class Div Thank you in advance. ...

Troubleshooting a problem with selecting options in Jquery

Looking for assistance with a jquery script. I have a select dropdown that fetches a list of options via Ajax. When a user clicks on an option, if a certain variable equals 1, an HTML div is added. If the variable changes to another value, the HTML div dis ...

I want to create a form with two dropdown menus placed side by side within a parent DIV, and I want them to collectively occupy the entire width using Bootstrap

Currently, I am working on a search form using Bootstrap. Everything is going well, but I am facing an issue where two multi-option elements need to be placed side by side and together occupy 100% of the width within their containing DIV. Just for clarity ...

Managing exceptions triggered by jQuery's Ajax function

Within my webpage, I am utilizing a jQuery ajax function to connect with a basic Webservice. This Webservice retrieves data from a database and then binds it to various controls on the page. The ajax function is triggered by the onchange event of a select ...

Is it possible to insert uneditable text in a form field using only CSS?

I'm attempting to create a form input field with a specific format. The field should appear as: The prefix domain.com/username/ should be displayed as non-editable text (and not be submitted as form data), allowing the user to input their desired tex ...

When attempting to incorporate a video using Sencha, an error occurs stating that the resource is being interpreted as an image but is being

While attempting to incorporate a sample into Sencha, I encountered an error stating "Resource interpreted as Image but transferred with MIME type text/css". Ext.define('MyApp.view.Newpage', { extend: 'Ext.Video', xtype: ' ...

Angular application's HTML Canvas unexpectedly changes to a black color when I begin drawing

Currently, I am developing an Angular application in which users can draw text fields on PDF pages. To achieve this functionality, I have integrated the ng2-pdf-viewer library and created a PDF page component that incorporates an HTML canvas element. Howe ...

I would like to know how to create a dropdown menu that shows the country name, flag, and code based on the

I have successfully generated the telephone input field. One requirement is to display the Country flag along with the country name as soon as the dropdown is selected. Another requirement is that once the country is selected, it should display the countr ...

What is the best way to embed a section of another website within an iframe and seamlessly guide a user to complete a purchase using a shopping cart?

Seeking advice on how to improve the functionality of my website. Currently, the domain I'm focusing on serves as a landing page, redirecting users to another site for purchases. I've built a separate website for this domain in order to establis ...

Updating the image sources of a group of image tags with a predetermined list

Looking to update a series of image source references within a specific div tag. For example: <!-- language-all: lang-html --> <div id="Listofimages"> <img src="images\2page_img_3.jpg"> <img src="images\2page_img_3 ...