Home > Net >  How to turn off hover effect after certain amount of time?
How to turn off hover effect after certain amount of time?

Time:04-16

I have a hover effect where when the icon image is hovered over, an larger image appears (for clarity). I want this larger image effect to end after three seconds AND still have the hover ability. If the image is moved off of, then came back to; I want the larger image to load for another three seconds. Every time the image is hovered over, the effect would last three seconds.

I have tried CSS animations, transitions, setTimeout and none of them are working like I need. Any help is appreciated. I have a LOT of code on this project, so I will try to only include the relevant parts. Thanks in advance.

I will have the code added to the question, once i figure out what I am doing wrong.

Code for building levels for hover image




#PlayerMarker1 {
  position: absolute;
  left:2%;
  top: 2%;
  z-index: 9;
}

#Player1Final{
  position: absolute;
  left:2%;
  top: 2%;
  z-index: 9;
}

/* Elements for Image load on hover*/

.playerMarker img{
  margin: 0 5px 5px 0;
  }

.playerMarker:hover{
  background-color: transparent;
  }
  
  .playerMarker:hover img{
  border: 1px;
  }
  
  .playerMarker span{ /*CSS for enlarged image*/
  position: absolute;

  padding: 0px;
  left: -1000px;
  /*background-color: black ;*/
  visibility: hidden;
  color: black;
  text-decoration: none;
  }
  
  .playerMarker span img{ /*CSS for enlarged image*/
  border-width: 0;
  padding: 2px;
  }
  
  .playerMarker:hover span{ /*CSS for enlarged image*/
  visibility: visible;
  top: 0;
  left: 100px; /*position where enlarged image should offset horizontally */
  z-index: 50;
  }

Code for defining the images.

<div id="Player1Test">
    <a id="PlayerMarker1" href="#thumb1"><img src="Player Markers/Morty_Icon.png" width="auto" height="auto"/><span><img src="Player Images/Morty_Token.png" /><br /></span></a>
  </div>

This script adds the playerMarker classes to the element I need.

  /* Script to add class to player marker ID items */

   function Player1Function() {
   var Player1FinalTest = document.getElementById("PlayerMarker1");
   Player1FinalTest.classList.add("playerMarker");
   Player1FinalTest.id='Player1Final';
   }

CodePudding user response:

Seems like css animations to pulse the image would work fine. Run the code snippet to try.

img {
  margin: 25px;
  width: 100px;
  height: auto;
}

img:hover {
  animation: pulse 2s 1;
}

@keyframes pulse {
    0% {
        transform: scale(1);
        box-shadow: 0 0 0 0 rgba(0, 0, 0, 0.7);
    }

    50% {
        transform: scale(1.4);
        box-shadow: 0 0 0 10px rgba(0, 0, 0, 0);
    }

    100% {
        transform: scale(1);
        box-shadow: 0 0 0 0 rgba(0, 0, 0, 0);
    }
<h4>Hover the mouse over image<h4>
<img src="https://stackoverflow.design/assets/img/favicons/apple-touch-icon.png">

CodePudding user response:

Yogi has a good answer using an animation that could be adapted to clearly move your element. I wanted to add an answer manipulating the left and top values using delay.

You are essentially moving a hidden image from off screen onto the screen. Though this feels a bit strange to do, as there may be more clear ways of accomplishing this task, you can immediately move the left into view, and delay moving the top out of view.

A different delay is needed for the hover and for the base element, so it returns to the original position immediately and is available for reuse, but delays moving away on hover.

This might keep in spirit of your current project.

If you have any questions, please ask

  • Related