Indeed...but also not quite.
The issue lies in the fact that when you hover over your div, it will shift its position, causing the cursor to move away from it and thus end the hover effect.
To solve this, consider transferring the hover effect to another element (maybe the body
).
However, you cannot smoothly transition from left
to right
(or vice versa), so you'll need to adjust the element's position with a transform property.
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#example {
width: 40px;
height: 40px;
background: red;
position: absolute;
top: 0;
left: 0;
transition: all 300ms ease 0s;
}
body:hover #example {
left: 100%;
top: 100%;
transform: translate(-100%, -100%)
}
<div id="example"></div>
Note: If you know the dimensions of the element, you can use the calc
function instead and skip using the transform.
body:hover #example {
left:calc(100% - 40px);
top:calc(100% - 40px);
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#example {
width:40px;
height:40px;
background:red;
position:absolute;
top:0;
left:0;
transition: all 300ms ease 0s;
}
body:hover #example {
left:calc(100% - 40px);
top:calc(100% - 40px);
}
<div id="example"></div>