Css Transform - Trigger event each time the mouse is out and in of a div

270 Views Asked by At

i have two questions.

In my project that i will include the code in this post i want:

  • The transform should trigger when i hover on blue square.
  • The square should not return to the first position if the mouse is in the red rectangle area or red square, only when is out of the two divs.

I hope you understand.

#square {
    position: absolute;
 width:100px;
 height:100px;
 background-color:blue;
}

#rectangle {
 width:200px;
 height:100px;
 background-color:red;
}

#square:hover {
    transform: translate(100px,0);
    -webkit-transform: translate(100px,0); /** Chrome & Safari **/
    -o-transform: translate(100px,0); /** Opera **/
    -moz-transform: translate(100px,0); /** Firefox **/
}

.animation {
    position: absolute;
    transition: all 2s ease-in-out;
    -webkit-transition: all 2s ease-in-out; /** Chrome & Safari **/
    -moz-transition: all 2s ease-in-out; /** Firefox **/
    -o-transition: all 2s ease-in-out; /** Opera **/
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Teste1 </title>
<link href="styles.css" rel="stylesheet" type="text/css">
</head>

<body>

<div id="rectangle" class="animation">

<div id="square" class="animation">
</div>

</div>

</body>
</html>

2

There are 2 best solutions below

0
On BEST ANSWER

I would use a container for your blue square.
This container would be the event triggered, and at init would have the same size as the square.
When hovered, its size would double to fit on the red rectangle.
This way, the animation is indeed triggered only from the square zone, and then, mouse detection is performed on the global rectangle.

The other tricky part is the reverse animation. When mouse leaving zone, I would use a second animation on the square container (longer one) to keep largest zone still available if the mouse get back to the red part. This occurs when square is getting back to its original point.

So I would use css animation, but only available when the mouse is not over the square container. Hope the following code will speak by itself.

Note that I set the background color of the square container to green, so you can see the trick. It has to be removed of course.

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Teste1 </title>
<link href="styles.css" rel="stylesheet" type="text/css">
</head>

<body>

<div id="rectangle" class="animation">
<div id="square-cont">
   <div id="square" class="animation">
</div>
</div>

</div>

</body>
</html>

CSS

#square {
    position: absolute;
    width:100px;
    height:100px;
    background-color:blue;
}
#square-cont {
    width:100px;
    height:100px;
    background-color:green;
    -webkit-transition: width 5s ease-in-out; /** Chrome & Safari **/
    -o-transition: width 5s ease-in-out; /** Opera **/
    -moz-transition: width 5s ease-in-out; /** Firefox **/
}

#rectangle {
    width:200px;
    height:100px;
    background-color:red;
}

#square-cont:hover #square {
    transform: translate(100px,0);
    -webkit-transform: translate(100px,0); /** Chrome & Safari **/
    -o-transform: translate(100px,0); /** Opera **/
    -moz-transform: translate(100px,0); /** Firefox **/
}
#square-cont:hover{
    width: 200px;
     -webkit-transition: none; /** Chrome & Safari **/
    -o-transition: none; /** Opera **/
    -moz-transition: none; /** Firefox **/
}

.animation {
    position: absolute;
    transition: all 2s ease-in-out;
    -webkit-transition: all 2s ease-in-out; /** Chrome & Safari **/
    -moz-transition: all 2s ease-in-out; /** Firefox **/
    -o-transition: all 2s ease-in-out; /** Opera **/
}
0
On

Here you can see. http://jsfiddle.net/srinivashappy/10ov411e. using keyframes.

Check above

<div id="rectangle" class="animation">
    <div id="square" class="animation"></div>
</div>

css

#square {
    position: absolute;
    width:100px;
    height:100px;
    background-color:blue;
    transform: translate(100px, 0);
    /** Chrome & Safari **/
}
#rectangle {
    width:200px;
    height:100px;
    background-color:red;
}
#rectangle:hover #square {
    animation: animationFrames 0.8s Infinite;
    transform: translate(0, 0);
    /** Chrome & Safari **/
}
@keyframes animationFrames {
    0% {
        transform: translate(10px, 0);
    }
    10% {
        transform: translate(20px, 0);
    }
    20% {
        transform: translate(40px, 0);
    }
    30% {
        transform: translate(60px, 0);
    }
    40% {
        transform: translate(80px, 0);
    }
    50% {
        transform: translate(100px, 0);
    }
    60% {
        transform: translate(80px, 0);
    }
    70% {
        transform: translate(60px, 0);
    }
    80% {
        transform: translate(40px, 0);
    }
    90% {
        transform: translate(20px, 0);
    }
    100% {
        transform: translate(10px, 0);
    }