时间:2018-03-12 16:01:46

标签: html css

我有以下小提琴: https://jsfiddle.net/ur9bpgbn/164/

我尝试将悬停效果应用于整个箭头但没有成功。

我在这里有CSS:



.arrow {
  position: absolute;
  font-size: 16px;
  max-width: 350px;
  background: #FFF;
  height: 40px;
  line-height: 40px;
  margin-bottom: 20px;
  text-align: center;
  color: #000;
  box-shadow: 3px 5px 5px rgba(0, 0, 0, 0.2);
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s, opacity 0.3s ease-in-out;
  z-index: 999;
}

.arrow.active {
  visibility: visible;
  opacity: 1;
}

.arrow.active.animate-left-to-right {
  animation-name: move-left-to-right;
  animation-duration: 1s;
  animation-delay: 0.6s;
  animation-iteration-count: infinite;
  animation-direction: alternative;
}

.arrow.active.animate-right-to-left {
  animation-name: move-right-to-left;
  animation-duration: 1s;
  animation-delay: 0.6s;
  animation-iteration-count: infinite;
  animation-direction: alternative;
}

@keyframes move-left-to-right {
  0% {
    transform: translateX (5%);
    box-shadow: 3px 5px 5px rgba(0, 0, 0, 0.2);
  }
  50% {
    transform: translateX(15%);
    box-shadow: 3px 5px 5px rgba(0, 0, 0, 0.4);
  }
  100% {
    transform: translateX(5%);
    box-shadow: 3px 5px 5px rgba(0, 0, 0, 0.2);
  }
}

@keyframes move-right-to-left {
  0% {
    transform: translateX(-5%);
    box-shadow: -3px 5px 5px rgba(0, 0, 0, 0.2);
  }
  50% {
    transform: translateX(-15%);
    box-shadow: -3px 5px 5px rgba(0, 0, 0, 0.4);
  }
  100% {
    transform: translateX(-5%);
    box-shadow: -3px 5px 5px rgba(0, 0, 0, 0.2);
  }
}

/*right arrow*/

.arrow-right {
  border-radius: 0px 0px 0 0px;
  background: linear-gradient(to right, rgba(255, 255, 255, 1), rgba(255, 255, 255, 0.7));
}

.arrow-right:after {
  content: "";
  position: absolute;
  right: -20px;
  top: 0;
  border-top: 20px solid transparent;
  border-bottom: 20px solid transparent;
  border-left: 20px solid #FFF;
  opacity: 0.7;
}

.arrow-right:before {
  content: "";
  position: absolute;
  left: -20px;
  top: 0;
  border-top: 0px solid transparent;
  border-bottom: 40px solid transparent;
  border-right: 20px solid #FFF;
  opacity: 1;
}

/*left arrow*/

.arrow-left {
  border-radius: 0 0px 0px 0;
  background: linear-gradient(to left, rgba(255, 255, 255, 1), rgba(255, 255, 255, 0.7));
}

.arrow-left:before {
  content: "";
  position: absolute;
  left: -20px;
  top: 0;
  border-top: 20px solid transparent;
  border-bottom: 20px solid transparent;
  border-right: 20px solid #FFF;
  opacity: 0.7;
}

.arrow-left:after {
  content: "";
  position: absolute;
  right: -20px;
  top: 0;
  border-top: 0px solid transparent;
  border-bottom: 40px solid transparent;
  border-left: 20px solid #FFF;
  opacity: 1;
}

.arrow:hover{
background-color: darkblue;
}

<div id="app">
  <a href="https://sporedev.ro" target="_blank"><div href="#" class="arrow arrow-right animate-right-to-left">This is a text</div></a>
  <div href="#" class="arrow arrow-left animate-left-to-right" style="margin-top:30%; margin-left:30%;"><span class="room-desc">This is a text</span></div>
</div>
&#13;
&#13;
&#13;

我找到了一种应用悬停的方法:之后但是当用户将鼠标悬停在后面时,我不希望悬停效果应用,我想要反过来。它实际上应该涵盖所有情况,如果用户将鼠标悬停在主div上,之后或之前它应该将悬停状态应用于所有这些情况。

我尝试阅读一些关于伪元素的文档,但我还没有找到可行的解决方案。

这可行吗?

2 个答案:

答案 0 :(得分:2)

当箭头悬停或后面悬停时,

.arrow:hover:after { }应该触发(因为后面将在箭头元素内)

$('.arrow').addClass('active')
body{
  background: #000;
  margin: 20%;
}

.arrow {
  position: absolute;
  font-size: 16px;
  max-width: 350px;
  background: #FFF;
  height: 40px;
  line-height: 40px;
  margin-bottom: 20px;
  text-align: center;
  color: #000;
  box-shadow: 3px 5px 5px rgba(0, 0, 0, 0.2);
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s, opacity 0.3s ease-in-out;
  z-index: 999;
}

.arrow.active {
  visibility: visible;
  opacity: 1;
}

.arrow.active.animate-left-to-right {
  animation-name: move-left-to-right;
  animation-duration: 1s;
  animation-delay: 0.6s;
  animation-iteration-count: infinite;
  animation-direction: alternative;
}

.arrow.active.animate-right-to-left {
  animation-name: move-right-to-left;
  animation-duration: 1s;
  animation-delay: 0.6s;
  animation-iteration-count: infinite;
  animation-direction: alternative;
}

@keyframes move-left-to-right {
  0% {
    transform: translateX (5%);
    box-shadow: 3px 5px 5px rgba(0, 0, 0, 0.2);
  }
  50% {
    transform: translateX(15%);
    box-shadow: 3px 5px 5px rgba(0, 0, 0, 0.4);
  }
  100% {
    transform: translateX(5%);
    box-shadow: 3px 5px 5px rgba(0, 0, 0, 0.2);
  }
}

@keyframes move-right-to-left {
  0% {
    transform: translateX(-5%);
    box-shadow: -3px 5px 5px rgba(0, 0, 0, 0.2);
  }
  50% {
    transform: translateX(-15%);
    box-shadow: -3px 5px 5px rgba(0, 0, 0, 0.4);
  }
  100% {
    transform: translateX(-5%);
    box-shadow: -3px 5px 5px rgba(0, 0, 0, 0.2);
  }
}

/*right arrow*/

.arrow-right {
  border-radius: 0px 0px 0 0px;
  background: linear-gradient(to right, rgba(255, 255, 255, 1), rgba(255, 255, 255, 0.7));
}

.arrow-right:after {
  content: "";
  position: absolute;
  right: -20px;
  top: 0;
  border-top: 20px solid transparent;
  border-bottom: 20px solid transparent;
  border-left: 20px solid #FFF;
  opacity: 0.7;
}

.arrow-right:before {
  content: "";
  position: absolute;
  left: -20px;
  top: 0;
  border-top: 0px solid transparent;
  border-bottom: 40px solid transparent;
  border-right: 20px solid #FFF;
  opacity: 1;
}

/*left arrow*/

.arrow-left {
  border-radius: 0 0px 0px 0;
  background: linear-gradient(to left, rgba(255, 255, 255, 1), rgba(255, 255, 255, 0.7));
}

.arrow-left:before {
  content: "";
  position: absolute;
  left: -20px;
  top: 0;
  border-top: 20px solid transparent;
  border-bottom: 20px solid transparent;
  border-right: 20px solid #FFF;
  opacity: 0.7;
}

.arrow-left:after {
  content: "";
  position: absolute;
  right: -20px;
  top: 0;
  border-top: 0px solid transparent;
  border-bottom: 40px solid transparent;
  border-left: 20px solid #FFF;
  opacity: 1;
}

.arrow:hover:after{
  border-left-color: red;  /* turn arrow red on hover */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="app">
  <a href="https://sporedev.ro" target="_blank"><div href="#" class="arrow arrow-right animate-right-to-left">This is a text</div></a>
  <div href="#" class="arrow arrow-left animate-left-to-right" style="margin-top:30%; margin-left:30%;"><span class="room-desc">This is a text</span></div>
</div>

答案 1 :(得分:1)

你的意思是这样的吗?

.arrow:hover {
  background: darkblue;
}

.arrow:hover:before {
  border-right: 20px solid darkblue;
}

.arrow:hover:after {
  border-left: 20px solid darkblue;
}