【问题标题】:how to affect previous and next images on image hover如何在图像悬停时影响上一个和下一个图像
【发布时间】:2016-12-21 16:29:24
【问题描述】:

我有一个无法解决的问题,我有这个 html,当我将鼠标悬停在其中一个上时,我希望所有图像都在其中旋转 P.S:并非所有图像都具有相同的速度 这是 HTML

<div >
<img id="first" class="pedal turnright" />
<div class="space"/>
<img id="second" class="pedal turnright"  />
<div class="space"/>
<img id="third" class="pedal turnleft"   />
<div class="space"/>
<img id="fourth" class="pedal turnleft"  />
</div>

我可以使用 ~ 选择器使下一个图像旋转,但不是以前的 提前谢谢你

【问题讨论】:

  • 好的,这里的确切问题是什么?
  • “previous”没有原生 CSS 选择器。正如@Jeremy Thille 提供的答案中所述,您最好的选择是将:hover 应用于&lt;div&gt; 本身
  • 谢谢,但我的 div 中有一些空白区域,当鼠标悬停在这些区域上时,我不希望图像旋转

标签: javascript html css image transform


【解决方案1】:

如果您希望 div 中的所有图像在悬停其中一个时旋转,则不需要 javascript。直接对 div 应用一些:hover 效果。您可以调整每个图像的旋转速度:

div:hover  img:nth-child(1){
       animation:spin 4s linear infinite;
   }
div:hover  img:nth-child(2){
       animation:spin 7s linear infinite;
   }
div:hover   img:nth-child(3){
       animation:spin 9s linear infinite;
   }
div:hover   img:nth-child(4){
       animation:spin 2s linear infinite;
   }


@keyframes spin { 100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } }
<div>
  <img src="https://placekitten.com/80/80"/>
  <img src="https://placekitten.com/80/80" />
  <img src="https://placekitten.com/80/80"/>
  <img src="https://placekitten.com/80/80"/>
</div>

编辑:

如果 jQuery 是一个选项,这里是如何做到的:

var $imgs = $("img")

$imgs.hover( function(){
    $imgs.addClass("rotating");
}, function(){
    $imgs.removeClass("rotating");
});
img{
margin-right: 50px;
}

img.rotating{
 animation:spin 4s linear infinite;
}

@keyframes spin { 100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
      <img src="https://placekitten.com/80/80"/>
      <img src="https://placekitten.com/80/80" />
      <img src="https://placekitten.com/80/80"/>
      <img src="https://placekitten.com/80/80"/>
</div>

【讨论】:

  • 我在图像之间的 div 中有一些空格,当鼠标悬停在空白区域时,我不希望图像旋转
  • 好的,在这种情况下,jQuery 是一个选项吗?因为使用 jQuery 很容易,但在纯 JS 中很麻烦。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多