Seamless changes with graceful fades while transitioning between classes

Is it more efficient to handle this in CSS than using jQuery? I'm not entirely sure. If anyone has a solution, that would be greatly appreciated. However, I am currently facing an issue with the jQuery method I have implemented. It successfully fades out the image, but when loading the new one, it doesn't seem to fade.

Any thoughts or suggestions on how to address this?

$("#product_color_select li").on("click", function() {
  var select_product_id = $(this).attr('data-value');
  sd_refresh_product_form_update(select_product_id);
});

var product_color_select_first = $("#product_color_select li:first-child").attr('data-larger');
$("#larger-colour").fadeIn("slow", function() {
  $('#larger-colour').addClass(product_color_select_first);
});

$("#product_color_select li").on("mouseover", function() {
  var select_product_swatch = $(this).attr('data-larger');
  $("#larger-colour").fadeOut("slow", function() {
    $('#larger-colour').removeClass();
  });
  $("#larger-colour").fadeIn("slow", function() {
    $('#larger-colour').addClass(select_product_swatch);
  });
});
#product_color_select {
  width: 76%;
  float: left;
}
#product_color_select li {
  display: inline-block;
  width: 30px;
  height: 25px;
  text-indent: -999999em;
  cursor: pointer;
  margin: 0 2px 5px 0;
}
#larger-colour {
  float: right;
  max-width: 86px;
  width: 86px;
  height: 62px;
}
/* interior colours */

#product_color_select li.eco-weave,
#larger-colour.eco-weave {
  background-image: url('http://2015.ambientlounge.com/design/themes/ambientlounge/media/images/swatches/mini/eco-weave.png');
}

... (CSS rules continue as per original content)

</ul>

<div id="larger-colour" class="aubergine-dream" style="display: block;"></div>

Answer №1

When you're implementing it, the background image is being added after the fadeIn animation has completed. This causes it to suddenly appear. To avoid this, set the background before fading it in.

$("#product_color_select li").on("click", function() {
  var select_product_id = $(this).attr('data-value');
  sd_refresh_product_form_update(select_product_id);
});

var product_color_select_first = $("#product_color_select li:first-child").attr('data-larger');
$("#larger-colour").fadeIn("slow", function() {
  $('#larger-colour').addClass(product_color_select_first);
});

$("#product_color_select li").on("mouseover", function() {
  var select_product_swatch = $(this).attr('data-larger');
  var thumbnail = $('#larger-colour');
  thumbnail.stop();
  thumbnail.fadeOut("fast", function() {
    thumbnail.removeClass();
    thumbnail.addClass(select_product_swatch);
    thumbnail.fadeIn("slow");
  });
});
#product_color_select {
  width: 76%;
  float: left;
}
#product_color_select li {
  display: inline-block;
  width: 30px;
  height: 25px;
  text-indent: -999999em;
  cursor: pointer;
  margin: 0 2px 5px 0;
}
#larger-colour {
  float: right;
  max-width: 86px;
  width: 86px;
  height: 62px;
}
/* interior colours */

#product_color_select li.eco-weave,
#larger-colour.eco-weave {
  background-image: url('http://2015.ambientlounge.com/design/themes/ambientlounge/media/images/swatches/mini/eco-weave.png');
}
#product_color_select li.aubergine-dream,
#larger-colour.aubergine-dream {
  background-image: url('http://2015.ambientlounge.com/design/themes/ambientlounge/media/images/swatches/mini/aubergine-dream.png');
}
#product_color_select li.lime-citrus,
#larger-colour.lime-citrus {
  background-image: url('http://2015.ambientlounge.com/design/themes/ambientlounge/media/images/swatches/mini/lime-citrus.png');
}
#product_color_select li.blue-jazz,
#larger-colour.blue-jazz {
  background-image: url('http://2015.ambientlounge.com/design/themes/ambientlounge/media/images/swatches/mini/blue-jazz.png');
}
#product_color_select li.sakura-pink,
#larger-colour.sakura-pink {
  background-image: url('http://2015.ambientlounge.com/design/themes/ambientlounge/media/images/swatches/mini/sakura-pink.png');
}
#product_color_select li.hot-chocolate,
#larger-colour.hot-chocolate {
  background-image: url('http://2015.ambientlounge.com/design/themes/ambientlounge/media/images/swatches/mini/hot-chocolate.png');
}
#product_color_select li.tundra-spring,
#larger-colour.tundra-spring {
  background-image: url('http://2015.ambientlounge.com/design/themes/ambientlounge/media/images/swatches/mini/tundra-spring.png');
}
#product_color_select li.black-sapphire,
#larger-colour.black-sapphire {
  background-image: url('http://2015.ambientlounge.com/design/themes/ambientlounge/media/images/swatches/mini/black-sapphire.png');
}
#product_color_select li.luscious-grey,
#larger-colour.luscious-grey {
  background-image: url('http://2015.ambientlounge.com/design/themes/ambientlounge/media/images/swatches/mini/luscious-grey.png');
}
#product_color_select li.wildberry-deluxe,
#larger-colour.wildberry-deluxe {
  background-image: url('http://2015.ambientlounge.com/design/themes/ambientlounge/media/images/swatches/mini/wildberry-deluxe.png');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul class="fabric-select" id="product_color_select">
  <li class=" aubergine-dream" data-value="745" data-larger="aubergine-dream" title="Aubergine Dream">Aubergine Dream</li>
  <li class=" black-sapphire" data-value="746" data-larger="black-sapphire" title="Black Sapphire">Black Sapphire</li>
  <li class=" wildberry-deluxe" data-value="727" data-larger="wildberry-deluxe" title="Wildberry Deluxe">Wildberry Deluxe</li>
  <li class=" tundra-spring" data-value="747" data-larger="tundra-spring" title="Tundra Spring">Tundra Spring</li>
  <li class="selected luscious-grey" data-value="744" data-larger="luscious-grey" title="Luscious Grey">Luscious Grey</li>
  <li class=" sakura-pink" data-value="743" data-larger="sakura-pink" title="Sakura Pink">Sakura Pink</li>
  <li class=" lime-citrus" data-value="748" data-larger="lime-citrus" title="Lime Citrus">Lime Citrus</li>
  <li class=" eco-weave" data-value="742" data-larger="eco-weave" title="Eco Weave">Eco Weave</li>
  <li class=" blue-jazz" data-value="749" data-larger="blue-jazz" title="Blue Jazz">Blue Jazz</li>
  <li class=" hot-chocolate" data-value="741" data-larger="hot-chocolate" title="Hot Chocolate">Hot Chocolate</li>
</ul>

<div id="larger-colour" class="aubergine-dream" style="display: block;"></div>

Answer №2

Just thought I'd share a different approach to optimizing your code:

Instead of the original block:

$("#product_color_select li").on("mouseover", function() {
  var select_product_swatch = $(this).attr('data-larger');
  $("#larger-colour").fadeOut("slow", function() {
    $('#larger-colour').removeClass();
  });
  $("#larger-colour").fadeIn("slow", function() {
    $('#larger-colour').addClass(select_product_swatch);
  });
});

Consider using this updated version:

$("#product_color_select li").on("mouseenter", function () {
    var select_product_swatch = $(this).attr('data-larger');
    $('#larger-colour').stop().fadeOut("fast", function(){
        $(this).removeClass().addClass(select_product_swatch).fadeIn("slow");
    });
});

Take a look at the JSFiddle demo :)

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

Optimal methods for refreshing website lists

I've designed an ASP.NET MVC application with a list that displays all the current items from a database. There is also a button that triggers a popup-dialog to create a new entry. The goal is to automatically add the new item to the list once it is c ...

An issue with the animation script in Ionic

I'm having trouble converting this to an Ionic application. Here's my code sample. Can anyone help me with putting this code correctly in Ionic? I'm trying to achieve something like this example <script src="https://cdnjs.cloudflare.com ...

What is the rationale behind not passing $scope to a service in AngularJS, and is it considered bad practice?

Is it advisable not to pass $scope to a service for certain reasons? I understand that services are intended to be reusable singletons, and passing a (potentially) large object to the service could lead to maintenance issues. However, assuming there is so ...

Customizing the primary CSS style of an element without the need to individually reset every property

Is there a way to reset just one property of a selector that has multiple properties stored in main.css, without listing all the properties and setting them to default values? I always struggle with CSS interference and constantly need to reset properties ...

`Cannot locate the CSS file on my local server`

My Node.js application uses the Express.js framework, and I am attempting to execute the following code: app.use('/public', express.static(path.join(__dirname,'public'))); However, I encountered the following error message: The CSS ...

Adjust Mui Autocomplete value selection in real-time

I have implemented Mui AutoComplete as a select option in my Formik Form. <Autocomplete disablePortal options={vendors} getOptionLabel={(option) => option.vendor_company} onChange={(e, value) => {setFieldValue("vendor_id", value. ...

What is the best way to extract a value from a JSON object?

I am having trouble deleting data from both the table and database using multiple select. When I try to delete, it only removes the first row that is selected. To get the necessary ID for the WHERE condition in my SQL query, I used Firebug and found this P ...

The rotation of an image in Microsoft Edge results in an HTML file display

One of the images on my website is sourced like this: <p> <img src="images/image.jpg" style="width: 50%;" /> </p> Surprisingly, in Chrome and Firefox, the image looks fine (just how I uploaded it to the server). H ...

Utilize jQuery and AJAX to dynamically update an array of forms based on user selections

Here is the HTML structure that I am working with: <div class="event-logistics"> <div class="logistics assig-row" id="logistic-block"> <div class="row"> <div class="col-3"> <select class=" ...

Printing HTML to a VueJS page is simple and efficient

I have a situation where one of my attributes in a property contains an HTML string. Instead of rendering the HTML as expected, when I output it within my template, the browser displays the raw HTML code with tags intact. It doesn't interpret it as a ...

Transform JSON data into an HTML table display using JavaScript/JQuery

To retrieve the JSON output on the user interface, I use the following function: $("#idvar").click(function(){ var d1 = document.getElementById("dText"); var d2 = document.getElementById("dJson"); var mytext = d1.textContent; alert(mytext); $.po ...

Calculate the sum of values in a JSON array response

I recently received a JSON string as part of an API response, and it has the following structure: { "legend_size": 1, "data": { "series": [ "2013-05-01", "2013-05-02" ], "values": { "Sign Up": { "2013-05-05": 10, ...

Tips for retrieving a variable from an XML file with saxonjs and nodejs

I came across an xml file with the following structure: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE agent SYSTEM "http://www.someUrl.com"> <myData> <service> <description>Description</description> < ...

Modify the error message in jQuery validation when the dropdown selection changes

I'm a beginner in jquery and I'm using the validation plugin from . It has been working well for me so far, but I encountered an issue on a specific page where I need to change the error message when a dropdown is changed, rather than when the su ...

How to Align Video Alongside Image in HTML without Using CSS

Is there a way to place a video on the left and an image on the right of the same line in HTML without relying on CSS? ...

Unable to successfully submit a form using PHP

I am currently working on a PHP page that retrieves data from MySQL. The goal is to fetch a list of objects from the database, display each object in a separate form for editing, and then save only the edited values. However, I am encountering difficulties ...

Change the attribute to read-only upon modification of the dropdown value

I have a dropdown menu with four options. My goal is to make the "number" input field readonly if the user selects either "option3" or "option4" from the dropdown. <select id="dropdown"> <option value="option1">option1</option> ...

Managing and retrieving data in bulk from an indexed database as JSON format

I am currently developing an application that utilizes IndexexDB for local data storage. I am looking for a way to extract and insert large amounts of data from an indexed DB in JSON format. Below is the code snippet illustrating what I have accomplished s ...

Encountering the error message "Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client" while attempting to identify duplicate entries in my database

I am currently working on a backend written in express.js that handles standard CRUD operations. My ORM of choice is sequelize. Here's the code snippet: import { Sequelize} from 'sequelize'; import User from '../Models/User.js'; im ...

angucomplete-alto automatically fills in data based on another input

Having two autocomplete select boxes with a unique feature has been quite interesting. The first input accepts a code that is related to a label, autofilling the second input with the corresponding object once the code is selected in the first input. Howev ...