Move the box upwards and change it to grayscale when hovering over the image, then reverse the effect when the hover

As I hover over the image, my goal is to make a gray box appear at the bottom and smoothly slide up. While it slides up, everything it covers should turn grayscale. When I move my mouse away from the image, I want the gray box to slide back down and remove the grayscale effect as it passes over the image.

I managed to achieve most of this using just CSS, but the issue I'm facing is figuring out how to reverse the animation when the mouse leaves the image.

Below is the code snippet I have been working on:

.wrapper {
  position: relative;
  
  img {
    width: 600px;
    height: 375px;  
  }
  
  .hover {
    position: absolute;
    bottom: 0;
    left: 0;
    z-index: 2;
    width: 600px;
    height: 100px;
    animation-name: example2;
    animation-duration: 1s;
    display: none;
    
    .gray-image {
      width: 100%;
      height: 100%;
      background-image: url('https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg');
      background-repeat: no-repeat;
      background-size: 600px 375px;
      background-position: bottom 0 left 0;
      
      filter: grayscale(100%);
      -webkit-filter: grayscale(100%);
      //position: absolute;
    }
    
    .text {
      color: #FFF;
      text-align: center;
      width: 100%;
      height: 100px;
      background-color: rgba(89, 89, 89, 0.9);
    }
  }
  
  &:hover {
    
    .hover {
      animation-name: example;
      animation-duration: 1s;
      height: 375px;
      display: block;
    }
  }
}

@keyframes example {
  from {
    height: 100px;
  }
  to {
    height: 375px;
  }
}

@keyframes example2 {
  from {
    height: 375px;
  }
  to {
    height: 100px;
  }
}
<div class="wrapper">
  <img src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg" />

  <div class="hover">
    <div class="gray-image" />
    <div class="slide-box">
      <div class="text">
          <span>This is the title</span>
      </div>
    </div>
  </div>
</div>

How can I achieve the desired full effect?

For those who prefer it, here is a link to the jsfiddle version: https://jsfiddle.net/pt15ckeL/2/

Answer №1

You can streamline your code by incorporating transitions, as demonstrated below:

.wrapper {
  position: relative;
  width: 600px; /* adjust the size */
  overflow: hidden;
}
.wrapper img {
  width: 100%;
  display: block;
}
/* create a gray image using pseudo-element */
.wrapper::before {
  content: "";
  position: absolute;
  inset: 0;
  background: url('https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg') 50%/100% auto;
  filter: grayscale(100%);
  clip-path: inset(100% 0 0); /* initially hide it */
  transition: 1s;
}
/* styling for text */
.text {
  position: absolute;
  inset: 0;
  color: #FFF;
  text-align: center;
  line-height: 100px; /* vertically center within 100px height */
  background: linear-gradient(grey 100px,#0000 0); /* apply gradient to top 100px only */
  translate: 0 100%; /* slide up to overflow the wrapper */
  transition: 1s;
}
.wrapper:hover::before {
  clip-path: inset(0); /* reveal the gray image */
}
.wrapper:hover .text {
  translate: 0 0; /* slide back to the top */
}
<div class="wrapper">
  <img src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg" />
  <div class="text">
    <span>This is the title</span>
  </div>
</div>

Furthermore, here's an alternative approach utilizing backdrop-filter, eliminating the need to set the image as a background and avoiding duplicate URLs in your code.

.wrapper {
  position: relative;
  width: 600px; 
  overflow: hidden;
}
.wrapper img {
  width: 100%;
  display: block;
}
.text {
  position: absolute;
  inset: 0;
  color: #FFF;
  text-align: center;
  line-height: 100px; 
  background: linear-gradient(grey 100px,#0000 0); 
  translate: 0 100%; 
  backdrop-filter: grayscale(100%);
  transition: 1s;
}
.wrapper:hover .text {
  translate: 0 0; 
}
<div class="wrapper">
  <img src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg" />
  <div class="text">
    <span>This is the title</span>
  </div>
</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

What is the process for creating a clickable file upload in Angular?

Currently, I am utilizing the instructions found in this guide to implement a file upload feature in my project. The code provided works perfectly: <input type="file" class="file-input (change)="onFileSelected($event)" #fileUplo ...

The webpage is displaying an error stating that "<function> is not defined."

I recently duplicated a functional web page into a new file on my site. However, after adding a new function and removing some HTML code, the JavaScript function no longer runs when clicking one of the two buttons. Instead, I receive the error message "Beg ...

Is there a way in Vue.js for me to access a method from within the same component using the component's data?

Below is the code for a specific component: <template> <li v-for="(item, i) in this.menu" :key="i" @click="item.action()"> //attempting to call the method in the component {{menu.title}} <li> </template> <script> ...

Delete with Express Router

I have created a basic "Grocery List" web application using the MERN stack (Mongo, Express, React, Node). However, I am facing an issue where my DELETE REST command does not execute unless I refresh the page. Here is the code for my event handler and the b ...

Why React.js and webpack are restricting the use of var, let, const?

I'm facing a puzzling issue that I just can't seem to solve. Let me show you a snippet from my Graph.js file: class Graph extends React.Component { @observable newTodoTitle = ""; s = 40 The error in webpack is showing up like this: E ...

Incorporate the casper function within the casper.evaluate() method

Is it possible to use the casper function inside casper.evaluate() with jQuery code? I need to iterate through elements in a way similar to how jQuery does. I have loaded the jquery.js library. Here is the script I have tried: casper.evaluate(function() ...

How can the background of a div be altered when text is typed into an input field?

I need help with the following code. I want to be able to change the background color of a div with the class "target_bg" from red (default) to green every time someone enters or types text into the input field. <div class="target_bg"></div> & ...

Modify a property within an object stored in an array using React with Redux

When trying to dispatch an action that updates values in the redux state by passing an array, I encountered an issue. It seems that despite attempting to update the array by changing object values, I kept facing this error - "Cannot assign to read only pro ...

How can I retrieve the handle for an item within a jQuery collection from within the success function of an .ajax() call?

I'm currently facing an issue with a jQuery ajax call that I have firing for each element in a collection identified by a specific jQuery selector. Here's a snippet of the code: $('.myClass').each(function () { $.ajax({ url ...

Creating a rotating wheel using JavaScript that is activated by a key press event

I need some help with implementing a keystroke event in this code so that the spinning wheel can start based on a key press, like the spacebar. Any suggestions on how to achieve this? I have found an event code for keystrokes in JavaScript: document.body. ...

How to Use PHP to Submit Form Information

I am a beginner in PHP and I need help with sending form details to an email address. I have tried looking for solutions online but I keep running into the same issue - when I submit the form, it downloads the PHP file instead of sending an email. Below i ...

When moving from Babel version 5.8.35 to 6.0.0, be prepared for app.js to throw a SyntaxError and encounter an unexpected token during compilation

Currently, I am in the process of enhancing my ReactJS components using webpack. However, I have encountered a hurdle while trying to transition from babel version 5 to 6. Upon attempting the upgrade, it resulted in a stack trace error within my app.js cl ...

Steps for extracting URL parameters from AWS API Gateway and passing them to a lambda function

After successfully setting up my API gateway and connecting it to my lambda function, I specified the URL as {id} with the intention of passing this parameter into the lambda. Despite numerous attempts using both the default template and a custom one for ...

Preserving distance between absolute elements

I'm facing an issue with my menu animation using jQuery. In order to achieve the desired effect, I am forced to use position: absolute. My challenge is maintaining consistent spacing between each text string, especially since they vary in width. Unfor ...

Achieving a seamless blend of parent background with child background: A step-by

I am trying to make the parent background color overlap the child background color in two divs. Currently, the child's background is overlapping the parent's background. Please refer to the JS-Fiddle link below for more information: <div id=" ...

What exactly is the significance of localizing an Aria label?

Looking to add an aria label that needs to be localized. What exactly does this mean? Here is the code I have: <a href="link"><i class="button" aria-label="back button"> How do I modify this code in order to make ...

Is there a way to utilize the 'interval' Rxjs function without triggering the Change Detection routine?

My goal is to display the live server time in my application. To achieve this, I created a component that utilizes the RXJS 'interval' function to update the time every second. However, this approach triggers the Change Detection routine every se ...

Converting a JavaScript variable into PHP using ajax: A comprehensive guide

Can someone please help me troubleshoot this code for passing a JavaScript variable to PHP? It doesn't seem to be working properly. I want to extract the value of an ID from JavaScript and send it over to PHP. <!DOCTYPE html> <html> ...

Encountered an error: "Type error undefined" while attempting to populate a form using AJAX and JSON

Upon inspecting the development console, it's clear that my AJAX request was successful and I've received the necessary JSON data. However, I'm struggling to display it correctly as I keep encountering the error below: Uncaught TypeError: C ...

Learn the steps for accessing and utilizing aria-label text in Google Chrome

Struggling with the aria-label attribute for blind users in Chrome. Can't seem to hear the aria-label text. Any insights on how aria-label behaves in Chrome or if I'm using the wrong approach? If I'm heading in the wrong direction or using ...