复制图像并设置悬停CSS

时间:2018-11-29 21:44:37

标签: css css-animations arrows

我要制作以下动画:

enter image description here

一个具有2个相同箭头的div,并在第一个箭头悬停时向左/向右移动。我尝试这样做,但未成功。我要为背景设置2张图像,但是如何为gif这样的1张图像设置动画呢?

.arrow-right2 {
    content: "";
    background: transparent url(https://i.imgur.com/u7cYXIo.png) 0 -185px no-repeat, transparent url(https://i.imgur.com/u7cYXIo.png) 0 -185px no-repeat;
    height: 35px;
    position: absolute;
    top: -5%;
    left: 0;
    width: 35px;
}

2 个答案:

答案 0 :(得分:5)

请尝试使用具有相同箭头的2个不同的div(绝对位置),并使用它与两个箭头重叠。如果可以,请使用单个图像,而不是精灵。然后将效果应用于悬停在其中一张图像上。

body {
  background: red;
   display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 50px;
  height: 50px;
  position: relative;
}
.arrow1 {
  background: url('https://i.imgur.com/u7cYXIo.png') no-repeat -17px -199px;
	width: 12px;
	height: 24px;
  display: block;
  left: 0;
  position: absolute;
}
.arrow2 {
  background: url('https://i.imgur.com/u7cYXIo.png') no-repeat -17px -199px;
	width: 12px;
	height: 24px;
  display: block;
  position: absolute;
  transition: all 0.4s;
  left: 0;
}
.arrow2:hover {
  left: -10px;
  transition: all 0.4s;
}
<div class="container">
  <div class="arrow1">
  </div>
  <div class="arrow2">
  </div>
</div>

答案 1 :(得分:0)

您可以如下调整background-position。您从每个人的不同职位开始,然后使他们相同:

.arrow {
  background: 
   url(https://i.imgur.com/u7cYXIo.png) -10px -185px,      
   url(https://i.imgur.com/u7cYXIo.png) 10px -185px,
   red;
  background-repeat:no-repeat;
  height: 50px;
  width: 50px;
  transition:all 0.5s;
}

.arrow:hover {
  background-position:10px -185px;
}
<div class="arrow"></div>

或者相反

.arrow {
  background: 
   url(https://i.imgur.com/u7cYXIo.png),      
   url(https://i.imgur.com/u7cYXIo.png),
   red;
  background-position:10px -185px;
  background-repeat:no-repeat;
  height: 50px;
  width: 50px;
  transition:all 0.5s;
}

.arrow:hover {
  background-position:
    -10px -185px,
    10px -185px;
}
<div class="arrow"></div>

如果要调整颜色,可以考虑mix-blend-mode

.arrow {
  background: 
   url(https://i.imgur.com/u7cYXIo.png),      
   url(https://i.imgur.com/u7cYXIo.png),
   #000;
  background-position:10px -185px;
  background-repeat:no-repeat;
  height: 50px;
  width: 50px;
  transition:all 0.5s;
  position:relative;
}
.arrow:before {
  content:"";
  position:absolute;
  top:0;
  left:0;
  right:0;
  bottom:0;
  background: red;
  mix-blend-mode: multiply;
  opacity:0;
  transition:all 0.5s;
}

.arrow:hover {
  background-position:
    -10px -185px,
    10px -185px;
}
.arrow:hover:before {
  opacity:1;
}
<div class="arrow"></div>