在父悬停上旋转跨度元素边框

use*_*031 4 css transform hover css3

我在div中有3条水平线.当我将鼠标悬停在封闭的div上时,我想旋转第一行和最后一行以形成+(加号).

此时,当您将鼠标悬停在第一行和最后一行时,它们以90度旋转,但是当我将鼠标悬停在div中的任何位置时,我希望这些线条能够旋转.我不确定CSS3是否可以单独使用或者我是否需要使用jQuery.

这是下面的小提琴:

div {
  width: 100px;
  height: 100px;
  background-color: red;
  padding-top: 15px;
}
div .bar {
  border-bottom: 5px solid #ffffff;
  height: 10px;
  width: 50px;
  display: block;
  margin: 5px auto;
}
div .bar:first-child:hover {
  -ms-transform: rotate(90deg);
  -webkit-transform: rotate(90deg);
  -moz-transform: rotate(90deg);
  transform: rotate(90deg);
}
div .bar:last-child:hover {
  -ms-transform: rotate(-90deg);
  -webkit-transform: rotate(-90deg);
  -moz-transform: rotate(-90deg);
  transform: rotate(-90deg);
}
Run Code Online (Sandbox Code Playgroud)
<div>
  <span class="bar"></span>
  <span class="bar"></span>
  <span class="bar"></span>
</div>
Run Code Online (Sandbox Code Playgroud)

mis*_*Sam 6

给父div赋予:hover伪类并将其子对象 -div:hover children


也就是说,我是一个平滑动画的傻瓜; 这就是我在这个例子中所关注的内容,同时减少了所需的标记.

让我们做到这一点 - 中间的笔划逐渐消失,底部笔划完成360度旋转,最终在中间.右边的两个例子更进一步.这些低级GIF在下面的演示中看起来好多了!

示例动画

HTML

只需要一个HTML元素:

<div></div>
Run Code Online (Sandbox Code Playgroud)

CSS

完整示例 - 简单过渡

div:before,
div:after {
  transition: all 0.3s;
}
div {
  width: 100px;
  height: 100px;
  background-color: red;
  position: relative;
  cursor: pointer
}
div:before,
div:after {
  content: '';
  height: 5px;
  width: 50px;
  display: block;
  position: absolute;
  background: #FFF;
  left: 50%;
  top: 50%;
  margin-left: -25px;
  transform: rotate(0);
}
div:before {
  margin-top: -20px;
  box-shadow: 0 20px 0 #FFF;
}
div:after {
  margin-top: 20px;
}
div:hover:after {
  margin-top: 0;
  transform: rotate(360deg);
}
div:hover:before {
  margin-top: 0;
  transform: rotate(90deg);
  box-shadow: none;
}
Run Code Online (Sandbox Code Playgroud)
<div></div>
Run Code Online (Sandbox Code Playgroud)

更多例子

您可以使用以下基本概念制作各种动画:

h2 {
  display: inline-block;
  vertical-align: middle;
  font-size: 0.8em;
}

div,
div:before,
div:after {
  transition: all 0.3s;
}
div {
  width: 100px;
  height: 100px;
  background-color: red;
  position: relative;
  cursor: pointer;
  display: inline-block;
  vertical-align: middle;
  margin: 10px;
}
div:before,
div:after {
  content: '';
  height: 5px;
  width: 50px;
  display: block;
  position: absolute;
  background: #FFF;
  left: 50%;
  top: 50%;
  margin-left: -25px;
  transform: rotate(0);
}
div:before {
  margin-top: -20px;
  box-shadow: 0 20px 0 #FFF;
}
div:after {
  margin-top: 20px;
}
div.one:hover:after {
  margin-top: 0;
  transform: rotate(360deg);
}
div.one:hover:before {
  margin-top: 0;
  transform: rotate(450deg);
  box-shadow: none;
}
div.two:hover {
  border-radius: 50%;
  transform: rotate(180deg);
}
div.two:hover:after {
  margin-top: -3px;
  transform: rotate(360deg);
  width: 30px;
  margin-left: -16px;
}
div.two:hover:before {
  margin-top: -3px;
  transform: rotate(450deg);
  box-shadow: none;
  width: 30px;
  margin-left: -16px;
}
div.three {
  box-shadow: 0 0 1px #F00;
}
div.three:hover {
  border-radius: 50%;
  transform: rotate(360deg);
  box-shadow: 0 0 30px #F00;
  -webkit-animation: pulse 1s infinite;
  animation: pulse 1s infinite;
}
div.three:hover:after {
  margin-top: -3px;
  transform: rotate(360deg);
  width: 30px;
  margin-left: -16px;
}
div.three:hover:before {
  margin-top: -3px;
  transform: rotate(450deg);
  box-shadow: none;
  width: 30px;
  margin-left: -16px;
  
}
@-webkit-keyframes pulse {
  0% {
    transform: scale(1);  
    box-shadow: 0 0 5px #F00;
  }
  50% {
    transform: scale(1.05);
    box-shadow: 0 0 30px #F00;
  }
  100% {
    transform: scale(1);  
    box-shadow: 0 0 5px #F00;
  }
}
@keyframes pulse {
  0% {
    transform: scale(1);  
    box-shadow: 0 0 5px #F00;
  }
  50% {
    transform: scale(1.05);  
    box-shadow: 0 0 30px #F00;
  }
  100% {
    transform: scale(1);  
    box-shadow: 0 0 5px #F00;
  }
}
Run Code Online (Sandbox Code Playgroud)
<h2>Spinny</h2>
<div class="one"></div>

<h2>Circular Ripple</h2>
<div class="two"></div>

<h2>Heartbeat</h2>
<div class="three"></div>
Run Code Online (Sandbox Code Playgroud)