Creating a hover effect for displaying image masks

My website features a profile page displaying the user's photo. I am attempting to create a functionality where when the user hovers over their photo, a transparent mask with text appears allowing them to update their profile picture via a modal.

However, I have been facing some issues in getting this feature to work properly. A demo of the issue is provided below:

 $(document).ready(function() {

   $('.profile-image').hover(function() {
     $('.mask-layer').css("visibility", "visible");
   }, function() {
     $('.mask-layer').css("visibility", "hidden");
   });
});

 
.profile-image-container {
  margin-right: auto;
  margin-left: auto;
  position: relative;
}
.profile-image-container .profile-image {
  border-radius: 6px;
  margin: auto;
}
.profile-image-container .mask-layer {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.3);
  border-radius: 6px;
  display: flex;
  justify-content: center;
  align-items: center;
  visibility: hidden;
}
.profile-image-container .mask-layer span,
.profile-image-container .mask-layer i {
  color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="profile-image-container">
  <img class="profile-image" src=http://nineplanets.org/images/earth.jpg alt="Profile img 0313">
  <div class="mask-layer">
    <div class="update-pic">
      <span>Update Photo</span>
    </div>
  </div>
</div>

You can view the problematic demo here.

The appearance on my live site is slightly better, as the mask aligns correctly over the image. However, it still has a flashing effect that needs fixing.

An important requirement is for the cursor to always appear as a link. Currently, when hovering over the text, the cursor changes to a text bar which is not desired.

Answer №1

Have you considered utilizing only css for this task?

.mask-layer{
  visibility: hidden:
}
.profile-image:hover mask-layer{
  visibility: visible;
  cursor: pointer;
}

After applying the above css in your jsfiddle, the flashing issue should be resolved.

Here is the updated version of your jsfiddle.

As for handling the click event, you can use the following code:

$('.mask-layer').on('click', function(e) {
  alert(e.target, 'was clicked');
});

Answer №2

If you simply want to display the mask when the user's mouse hovers over the image, there is no need to use JavaScript.

Check out this demonstration where I have added an additional CSS rule:

.profile-image-container:hover .mask-layer {
  display: flex;
}

I have set the default display for the mask-layer to be none.

Here is the 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

PHP data is not displayed by Ajax

I seem to be encountering a bit of trouble. I am attempting to utilize ajax to retrieve data from a PHP server, which in turn fetches it from a MySQL database, and then display it within a specific HTML tag location. However, for some unknown reason, nothi ...

Encountering issues with Bootstrap modal functionality within a Laravel blade template

I am facing an issue with displaying a modal when a specific link is clicked. The problem is that the modal appears briefly for less than a second and then disappears again. How can I ensure that the modal remains stable? Below is my code: <!DOCTYPE ...

Encountering the "node:internal/modules/cjs/loader:1050" error while attempting to execute a folder using the "npm run dev" command

I am encountering an issue while attempting to execute a folder using npm run dev, consistently resulting in the same error message. PS C:\Users\My Name\Desktop\My Folder> npm run dev Debugger attached. > <a href="/cdn-cgi/l/e ...

Issues have arisen with Google Maps functionality following the API integration, resulting in a blank grey

I am experiencing an issue with Google Maps where it is displaying a grey box or sometimes showing nothing/white. I have tried various solutions found online but none of them have worked for me. Does anyone have a solution? I even attempted to change the ...

Tips for ensuring an element stays anchored at the bottom even when the keyboard is displayed

I recently encountered a situation on one of my pages where an element positioned at the bottom using absolute positioning was causing issues when the keyboard was opened, as it would move to the middle of the page unexpectedly. While this may seem like a ...

Choosing a specific value for a selected row in a jQuery table

One of the challenges I'm facing is setting the selected value of a select box in row 5 of a table with a class called .tablesorter-filter, rather than an input box. Can anyone offer guidance on how to accomplish this? $('.tablesorter-filter&apo ...

Is it possible to engage with a local webpage using an application and receive real-time feedback?

I am interested in finding a way to connect my local HTML page with my C++ application. Similar to using the JavaScript console to make real-time edits to a webpage, like this: document.getElementById('divlayer').style.visibility = 'hidden& ...

Can one ascertain the cleared Kendo grid column filter using jQuery?

I am working with a kendo grid that has approximately 10 to 12 columns, each with its own filtering option. My current task requires me to identify when a column filter is cleared and determine which specific column triggered the event. Is there an event w ...

Restore checkbox to default setting

Is it possible to reset checkboxes in a form back to their initial status using javascript, PHP, jQuery, or any other method? Here is the code I am currently using: <form method="POST> <input type="text" name="name" id="name" value="default val ...

Eliminate any repeated elements within the nested arrays found within an array of objects

Looking for a way to eliminate duplicates in this array structure. While mapping over arr, the values from each nested array within the objects are retrieved. However, I want to address and filter out any duplicate values. Current Result: bye hello hello ...

Enhancing webpage styles with AngularJS

Just starting out with AngularJS and eager to get the best approach to tackle this challenge. Describing my dilemma: I have an anchor element that, when clicked, needs to toggle between classes "show-all" and "hide-all" while also updating the styling of ...

Unraveling the Mysteries of Node.js Application Logging

We've been having smooth sailing with our Kube cluster on AWS, but there seems to be a recurring issue with our Node app crashing and generating repetitive logs from our instances... I've scoured the internet for solutions but haven't had m ...

When I try to pass a variable as a prop to another .js file, it mysteriously transforms into an undefined value

Upon successful login, my app file sets the isAuthenticated variable to true and redirects to the /admin page. The Firebase API call is functioning as expected, allowing access only with a valid username and password. The issue arises when I try to hide t ...

Step-by-step guide on achieving rounded borders with a gap:

Struggling to design blocks and elements with rounded corners that have a gap between the corner and the straight line. It's proving to be quite challenging :) Additionally, I need to create buttons that look like this, and If CSS alone doesn' ...

How can I show a legend entry for every column in Amcharts?

Take a look at this code snippet: http://jsfiddle.net/ouLed1fp/ How can I create a legend entry for each column in the chart? Also, is there a way to display the column names next to them, providing a clear legend? <div id="chartdiv" style="width: 10 ...

Ways to prevent prop changes from passing up the chain?

After some experimentation, I discovered that the props I passed to a component can actually be changed within the component and affect the parent. This behavior is discussed in the official documentation. While objects and arrays cannot be directly modi ...

Prevent sidebar from adjusting size according to text length

Just started exploring Bootstrap 5 and noticed some significant differences from version 4. Check out this jsfiddle for a demonstration of my issue. The problem occurs when the lorem ipsum text exceeds a certain length, causing the sidebar width to be pu ...

Incorporate a tall button on the right edge of the division

I have successfully implemented a sidebar with the ability to hide or display it using this link. Now, I am looking to add a clickable bar on the right side of the div that can also trigger the hide/show functionality. After experimenting, I discovered th ...

Display a notification to the user prior to reloading the page

I have created a code for logging attendance at work using a barcode reader. The user simply needs to scan their card with a barcode to register their attendance. let $scannerInput = $(".scanner-input"); $(document).ready(function(){ $scannerInput.focu ...

VueJS is unable to identify the variable enclosed within the curly braces

I recently started learning VueJS and I've hit a roadblock. My goal is to display a variable, but for some reason, instead of seeing the product name, all I get is {{product}} Here's the HTML code I'm using: <!DOCTYPE html> <html l ...