简体   繁体   English

CSS鼠标悬停在translateXY中

[英]CSS hover with mouse in translateXY

Thank you for reading my question 感谢您阅读我的问题

 .ab { position:absolute;left:50%;top:50% } .logo_img { width:100px; } .logo_img:hover { -webkit-animation: hvr 0.5s ease-out 1 0s; -ms-animation: hvr 0.5s ease-out 1 0s; animation: hvr 0.5s ease-out 1 0s; } @keyframes hvr { 0% { -webkit-transform: translateX(0px);transform: translateX(0px); } 50% { -webkit-transform: translateX(900px);transform: translateX(900px);} 51% { -webkit-transform: translateX(-900px);transform: translateX(-900px);} 100% {-webkit-transform: translateX(0px);transform: translateX(0px);} } 
 <div class="ab"><img src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" class="logo_img" /></div> 

Problem is when mouse goes on it, and image moves, then mouse is not on image and sometimes hover does not work! 问题是当鼠标移到它上面并且图像移动时,鼠标不在图像上,有时悬停不起作用!

Is there any way to do animation like this hover but if mouse is not on image... it keeps going? 有什么办法可以做像这样的悬停动画,但是如果鼠标不在图像上……它会继续前进吗?

Is it possible to user jQuery hover and add class on hover? 是否可以使用jQuery鼠标悬停并在悬停时添加类? And delete that class after animation ends? 动画结束后删除该类吗?

You can create a container div for the image, wich always stays in the same place, and put the image inside this div. 您可以为图像创建一个容器div,该容器始终位于同一位置,并将图像放入该div中。 Then instead of checking, if the mouse is over the image, you can check if it is over the div. 然后,您可以检查鼠标是否在div上,而不是检查鼠标是否在图像上。

 #container { border: 1px solid black; } .logo_img { width:100px; margin-left: calc(50% - 50px); } #container:hover .logo_img { -webkit-animation: hvr 0.5s ease-out 1 0s; -ms-animation: hvr 0.5s ease-out 1 0s; animation: hvr 0.5s ease-out 1 0s; } @keyframes hvr { 0% { -webkit-transform: translateX(0px);transform: translateX(0px); } 50% { -webkit-transform: translateX(900px);transform: translateX(900px);} 51% { -webkit-transform: translateX(-900px);transform: translateX(-900px);} 100% {-webkit-transform: translateX(0px);transform: translateX(0px);} } 
 <div id="container"> <img src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" class="logo_img"> </div> 

If you mean to make animation work without hover then add this animation-iteration-count to infinite . 如果要使动画无需悬停就可以工作,请将此animation-iteration-countinfinite

.logo_img {
    -webkit-animation: hvr 5s ease-out;
    -ms-animation: hvr 5s ease-out;
    animation: hvr 5s ease-out infinite;
}

Updated another answer using jQuery, 使用jQuery更新了另一个答案,

<img src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"/>

$(document).ready(function(){
    $("img").on("mouseenter",function(){
    $(this).addClass("logo_img");
  });
  $("img").on("mouseleave",function(){
    $(this).removeClass("logo_img");
  });
});
var duration = 500;

$('img').mouseenter(function() {
  $(this).addClass('hvr').delay(duration).queue(function() {
    $(this).removeClass('hvr');
    $(this).dequeue();
  });
});

CODEPEN CODEPEN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM