What is the best way to show a block of text when hovering over an image with a smooth easing effect

How can I make a block of text appear with ease when hovering over an image of a tree? The current setup works, but the transition effect is not as smooth as I want it to be:

HTML:

<p id="hover-text1" class="hover-text1"> Accomplished! </p>
<img id="tree_image1" class="tree_image1" src="/Tree-red.png">

CSS:

.hover-text1 {
 display: none;
 transition: .5s all ease;
 }

.tree_image1:hover {
 opacity: 0.3;
 }

JS (included in the head section of my HTML file):

<script type="text/javascript">
    $(document).ready(function(){
      $('#tree_image1').mouseenter(function(){
        $('#hover-text1').css({'display':'block'});
      });
      $('#tree_image1').mouseleave(function(){
        $('#hover-text1').css({'display':'none'});
      });
    });
  </script>

Currently, the block of text hover-text1 appears when hovering over tree_image1, and disappears when moving away from it. However, I am struggling to achieve a smooth 0.5-second transition effect. Any suggestions or solutions would be greatly appreciated!

It may also be relevant to note that I am using a local server running Node.js, EJS, and Express.

Answer №1

When using transitions, it's important to note that the display property is not one of the properties that transition works upon. Instead, you can apply transitions to visibility and opacity to achieve the desired effect.

<div class="wrapper">
    <p class="hover-text1"> Mission accomplished! </p>
    <img id="tree_image1" class="tree_image1" src="/Tree-red.png">
  </div>

.hover-text1 {
  visibility: hidden;
  opacity: 0;
  transition: visibility 2s, opacity 2s linear;
}

.tree_image1:hover {
  opacity: 0.3;
}

.wrapper:hover .hover-text1 {
  visibility: visible;
  opacity: 1;
}

Answer №2

To achieve the desired effect, you can utilize jQuery's fadeIn and fadeOut functions.

$(document).ready(function(){
  $('#tree_image1').mouseenter(function(){
    $('#hover-text1').fadeIn(500);// 500ms - duration
  });
  $('#tree_image1').mouseleave(function(){
    $('#hover-text1').fadeOut(500);
  });
});

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 methods can I employ to utilize PHP or JS/HTML for posting to two separate URLs simultaneously?

Is it possible to submit a form from one button to two different locations? I am looking for a solution to achieve this. After clicking the submit button on a form, the form tag looks like this: <FORM ACTION="http:site.com/servlets/RequestServlet" met ...

Tips for successfully passing a variable within jQuery

Recently, I've delved into the world of jQuery and I must say, I'm impressed with how it simplifies functions. However, being a newcomer to JavaScript, I've encountered a stumbling block with a particular function. My goal is to bind a coup ...

Aligning the title vertically in Ionic on Android device

Currently, I am in the process of developing an application using the Ionic framework and cordova. After uploading my app for testing purposes, I encountered a small issue. The title in the top bar on Android appears to be misaligned vertically. Is there a ...

Generating unique identifiers for jQuery carousel

<div id="container_01"> <div class="pikachoose"> <ul id="pikame" class="jcarousel-skin-pika"> <li> <img id="main_photo" name="main_photo" src="image.jpg" width="520" height="380" class="del ...

Generating a dataframe with cells that have a combination of regular and italicized text using the sjPlot package functions

I've been trying to set up a basic data table where the genus name in the "Coral_taxon" column is italicized, but the "spp." part following it remains lowercase. I thought of using the expression() function for each row in "Coral_taxon," but so far, I ...

What is the best way to add padding to each line within a block of text containing multiple lines

My <span> tag has a background color and left and right padding applied to it. However, the padding is only visible at the beginning and end of the <span>, not on each line when the text wraps onto multiple lines. Is there a way to add padding ...

Is there a way to have text appear in a popup when I reach it while scrolling?

Is there a way to make the textblocks on my website increase in size or pop up when I scroll down to them? I've attempted to search for a solution online but have been unsuccessful in finding one. ...

Abundance of DOM elements - the difference between hidden and display none

When I have numerous DOM elements on my page and assign them all a display: none property, the browser continues to perform quickly (smooth scrolling and snappy page response). But if I hide the elements using visibility: hidden instead, the browser slows ...

Customizing the text color of steps in a v-stepper component using Vuetify.js

When working with the v-stepper component of VuetifyJS, it is easy to change the color of the steps themselves by using the `color` prop. But how can I alter the text of each step? Specifically, I am looking to modify the color of the text that says Name ...

In Bootstrap 4, the positioning of :after is different compared to Bootstrap 3

Our bootstrap nav-tabs have a unique default look with an 'indicator' icon below the selected tab. This is achieved by styling the active anchor element as follows: https://i.sstatic.net/UQos5.png However, when migrating to bootstrap 4, we noti ...

Is it feasible to utilize a CSS variable within a CSS "background URL" property?

Having trouble setting a base domain for all my pictures in a CSS file. Here's what I've tried: In global.css: :root { --bgd: #C0C0C0; --picdomain: "https://somedomain.com/"; } In s1.css: @import url("global.css"); body { background-co ...

Utilizing Java to extract dynamically generated content from an HTML page

I have an HTML page that looks like this: <html> <head> <!-- necessary java scripts --> </head> <body> <div id="content"></div> </body> After the page renders, a lot of dynamic html content is placed within ...

style for a bootstrap form input

Can the form-control from Bootstrap be made inline for input without relying on the grid system like col-md-2? This is the one aspect of Bootstrap that I find particularly frustrating. ...

Show the user's name in an Express partial once they have logged in

I've been trying to find a solution for my issue without success. Here's the scenario: I have a homepage ('/'), a profile page ('/profile'), and a login/register page. I'm using passport with local, twitter, and google fo ...

Building a connection between a PostgreSQL database and implementing it with Node.js and React.js

After creating a node.js file with database connectivity, the next step is to integrate it into a webpage built with react.js. The util.js file is used to handle API calls: require("isomorphic-fetch"); function addName(time) { return fetch(&ap ...

When utilizing var_dump in PHP, the SQL query may return an empty array with an output of array(0

I am puzzled as to why my Query is not retrieving any data, despite its title My JQuery: $(".output").click(function() { var noteid = $(this).data("noteid"); $("#right-box").load("connectionDetails.php", { noteid: noteid }); }); My connectionDet ...

Utilizing Vue.js and i18n to fetch external JSON files from a server and seamlessly integrating them as globally accessible data in all components

I'm currently exploring ways to fetch translation files from a server and make them accessible across all components of the project. For instance, I can request to obtain this JSON: { "DASHBOARD_SETTINGS": "Einstellungen", ...

Ways to retrieve JSON objects from the Controller using ajax

Currently, I am developing a mail validation controller using jQuery AJAX and PHP. Once the message is validated in the PHP controller, I attempt to retrieve it using AJAX and display it on the screen using jQuery. Here is an excerpt of my AJAX code: .. ...

loading dynamic content into an appended div in HTML using Angular

Here is the HTML code from my app.component.html file: <button mat-raised-button color="primary" mat-button class="nextButton" (click)="calculatePremium()"> Calculate </button> <div id="calcul ...

Troubleshooting: Issues with jQuery Dropdown Menu

I'm currently working on a website that includes a settings feature with a button. My goal is to have the options and other links display in a dropdown menu when hovered over. Although I have written what I believe to be the correct code, it's no ...