Is there a way to display the true colors of a picture thumbnail when we click on it?

In my project, I attempted to create a dynamic color change effect when clicking on one of four different pictures.

The images are displayed as thumbnails, and upon clicking on a thumbnail, it becomes active with the corresponding logo color, similar to the Photoshop profile example provided. The inactive thumbnails should appear in grayscale until clicked on.

Despite trying to achieve this effect using the filter property, I have not been successful. Can someone assist me in finding the correct solution?

If needed, I can share the code of my project below:

[code snippet]

Answer №1

Issue 1: The filter applied by the .active class does not remove grayscale effect

After clicking on the image, the .active class is added, which implements an intense filter that resembles a photo negative rather than removing the grayscale effect:

.active {
    filter: contrast(1000%)
    /* black to white */
    invert(100%)
    /* white to off-white */
    sepia(100%)
    /* off-white to yellow */
    saturate(10000%)
    /* do whatever you want with yellow */
    hue-rotate(90deg);
}

To fix this, the CSS should either be:

filter: grayscale(0);

OR to completely eliminate all filters:

filter: none;

Issue 2: All thumbnails are marked as .active

While the .active class dynamically affects the .thumbnail-image elements, it's also statically assigned to all thumbnail images (the img child element). This causes all images to appear in the "active" state continuously. The static class needs to be removed:

<!-- <img :src="image.thumb" class="active"/> --> <!-- DON'T DO THIS -->
<img :src="image.thumb">

Alternatively, you can transfer the dynamic application of classes from the container div to the img itself.

Issue 3: The .active class doesn't target the img directly

Although the .active class is assigned to .thumbnail-image (the container for img), the intention is to apply the filter to the actual img element. To achieve this, modify the CSS selector like so:

.active > img {
  ...
}

If you followed the suggestion to move the dynamic class assignment to the img itself, there’s no need to adjust the selector here.

Demonstration of the Solution

Vue.component("carousel", {
  template: ` <!-- Template content here -->
`,
  computed: {
    // Computed properties
  },

  data() {
    return {
      activeImage: 0
    };
  },

  methods: {
    // Methods implementation
  },

  props: ["images"]
});

new Vue({
  el: "#app",
  data: () => ({
    images: [
      // Image data objects
    ]
  })
});
.section {
  /* Section styles */
}

/* Other CSS rules */

.active > img {
  Filter properties
}

#textespan {
  Text span styles
}
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7701021237455942594640">[email protected]</a>"></script>

<section class="section" id="app">
  <carousel :starting-image="0"
            :show-progress-bar="true"
            :images="images"></carousel>
</section>

Answer №2

All you require is CSS.

.product-image > img {
    filter: blur(5px);
    -webkit-filter: blur(5px);
}

.product-image > img:hover {
    filter: blur(0);
    -webkit-filter: blur(0);
}

Answer №3

To create a unique class that excludes the `grayscale` effect, and apply the normal `grayscale` to other image tags. Simply add a class, like `color`, to the clicked element while removing the `color` class from others. Here's an example code snippet:

function onClick(it) {
 
  const withColor = it.parentElement
    .getElementsByClassName('color')[0];
    
  if(withColor)
    withColor.classList.remove('color');
    
  it.classList.add( 'color' );
  
  
}
#images {
  display: flex;
}

img {
  margin: 10px;
  width: 100px;
  height: 100px;
  filter: grayscale( 100% );
  border: 3px solid transparent;
}

img.color {
  filter: grayscale( 0% );
  transition: filter 400ms;
  border: 3px solid green;
  border-radius: 2px
}
<div id="images">

   <img src="https://i.stack.imgur.com/MagOw.jpg" class="image" onclick="onClick(this)">
   
   <img src="https://i.stack.imgur.com/MagOw.jpg" class="image" onclick="onClick(this)">
   
   <img src="https://i.stack.imgur.com/MagOw.jpg" class="image" onclick="onClick(this)">
   
   

</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

The element is being offset by SVG animation that incorporates transform properties

I'm working on creating a halo effect by rotating an SVG circular element using the transform rotate function. I've been using getBox to find the center point of the element, but when the rotation occurs, the overall image is getting misaligned w ...

Utilizing getElementsByClassName for web scraping: encountering inaccurate outcomes

I am attempting to extract the inner text from all classes with the className = "disabled" within the provided snippet of HTML Code: HTML code In my effort to achieve this task using MS Access (VBA), the code I have implemented is as follows: Set IE = C ...

Obtaining JSON data in a separate JavaScript file using PHP

I have an HTML file with the following content: // target.html <html xmlns="http://www.w3.org/1999/xhtml"> ... <script src="../../Common/js/jquery-ui-1.10.3.js"></script> <script src="../../Common/js/select.js" type="text/javascript"& ...

Creating a countdown timer that is determined by the word count of a specific <div> element

What I have: A unique countdown timer that starts at 3 seconds and counts down to 0s. <div class="phrase"> This is a statement.</div> <p> <div style='font-family: Arial; font-size: 12px; color:gray'> <br><s ...

What impact does the //g flag in Regex JavaScript have on the program's state?

I recently had a question that was answered, but I'm still trying to grasp why the regex behaves in a certain way. According to w3schools, it explains: g: Perform a global match (find all matches rather than stopping after the first match) Okay, ...

Ways to halt code execution if the component is not actively in use in Nativescript Vue

I'm facing an issue with my application where it continues to send data about the device every 5 seconds even when I minimize the app and reopen it or navigate away from the page. This leads to a situation where the function of sending data is trigger ...

What is the best way to configure multiple environmental variables in webpack?

I'm having trouble figuring out how to pass multiple environment variables to webpack. I've been attempting to execute the script below, but it doesn't seem to be working: "cross-env NODE_ENV=production DTM_ENV=staging webpack --config ...

Error message stating 'compression is not defined' encountered while attempting to deploy a Node.js application on Heroku

Why is Heroku indicating that compression is undefined? Strangely, when I manually set process.env.NODE_ENV = 'production' and run the app with node server, everything works perfectly... Error log can be found here: https://gist.github.com/anony ...

Clicking will open the link in both a new tab and the current tab

I'm looking to enable ng click functionality to work in both new tabs and the current tab. The URL is dynamically generated based on a certain condition. Here is the HTML: <a ng-href="{{myPathVariable }} " ng-click=" GoToThemes()" class="shift-l ...

Tallying the number of messages on Facebook

Greetings everyone! I have been utilizing the Facebook JS SDK to retrieve the number of messages sent. Below is the code snippet: <script> function statusChangeCallback(response) { console.log('statusChangeCallback'); console.log ...

Displaying JSON data in HTML using Angular

If you have a straightforward controller: controller: function($scope, $http){ $http.get(templateSource+'/object.customer') .then(function(result){ $scope = result.data; }); } The content of my object.customer file is a ...

Ways of accessing an array within an if statement

I have a dashboard with admin privileges for my application. In this dashboard, the admin can select a user from a dropdown list. Once a user is selected, I make an AJAX call to retrieve the user's data and store it in a variable named $result. Howeve ...

Should Redux Reducer deep compare values or should it be done in the Component's ShouldComponentUpdate function?

Within my React Redux application, I have implemented a setInterval() function that continuously calls an action creator this.props.getLatestNews(), which in turn queries a REST API endpoint. Upon receiving the API response (an array of objects), the actio ...

Maintain original image file name when downloading

I have successfully created a download link containing a base 64 encoded image. The only issue is that I need to retain the original file name, which is dynamic and cannot be hardcoded. downloadImage: function(){ var image_url = document.getEleme ...

`vue.js: li element tag numbers not displaying correctly`

Issue with Number list not displaying correctly using Vue.js I have tried sending it like this. Is there a fix for this problem? This is the code snippet I used to output in Vue.js: p(v-html="selectedProduct.description") Snapsho ...

What is the best way to distinguish between relative blocks and convert them to absolute positioning?

What is the best way to locate relative blocks and convert them to absolute position while keeping them in the same place? Is this something that can be achieved using JavaScript or jQuery, or is it simply not possible? Thank you very much. ...

Trouble displaying data table using Vue JS (v-for)

My goal is to display a table of data fetched from Firebase Firestore. I have successfully stored all the data in an array, but when I try to display it, the entire array appears instead of individual items. You can see the issue in the image below: Here& ...

How can I clear the div styling once the onDismiss handler has been triggered

Seeking assistance with resetting a div on a Modal after it has been closed. The issue I am facing with my pop-up is that the div retains the previous styling of display: none instead of reverting to display: flex. I have searched for a solution without su ...

Ways to trigger a Vue.js method after a delay of 500 milliseconds

Currently, I am developing a search system that triggers an ajax call with every key press. However, I would like to implement a delay of 500ms before the ajax call is made after typing begins. Any ideas on how this can be achieved? ...

Design an arrangement using div elements and CSS styling

Creating a layout for a website can be challenging. I am using three div tags - container, left, and right. My goal is to have the left side fixed-width for navigation, while the right side displays dynamic content loaded from a database. However, I'm ...