简体   繁体   English

CSS:悬停和离开之间的平滑过渡

[英]CSS: Smooth transition between hover and leaving

This is my html code:这是我的 html 代码:

 li.searchbar a.searchbar-text:hover { color: red;important: margin-left; -10px: transition. opacity,9s. margin-left,5s. margin-right;5s: -webkit-transition. opacity,9s. margin-left,5s. margin-right;5s; }
 <li class="col-sm-5 searchbar"> <a class="text-left"><i class="fa fa-search" aria-hidden="true"></i></a> <a class="searchbar-text"> <span class="first-seach-text">Search France</span> <span class="second-seach-text">and beyond</span> </a> </li>

With mouse hover, it moves to left smoothly, but when the mouse leaves, it moves back to right fast.(I expect smoothly).鼠标悬停时,它向左平滑移动,但是当鼠标离开时,它快速向右移动。(我希望平滑)。

demo演示

So the transition takes place on the hover but not on the off.所以过渡发生在悬停时而不是关闭时。 So you need to set the transition on the element without the hover state.所以你需要在没有悬停状态的元素上设置过渡。

for example: for your demo add例如:为您的演示添加

li.searchbar a.searchbar-text {
  transition:all .5s ease;
}

The transition on hover will not affect the normal state悬停时的过渡不会影响正常状态

 li.searchbar a.searchbar-text{ transition: opacity .9s, margin-left .5s, margin-right .5s; -webkit-transition: opacity .9s, margin-left .5s, margin-right .5s; } li.searchbar a.searchbar-text:hover { color: red !important; margin-left:-10px; }
 <li class="col-sm-5 searchbar"><a class="text-left"><i class="fa fa-search" aria-hidden="true"></i></a><a class="searchbar-text"><span class="first-seach-text">Search France</span><span class="second-seach-text">and beyond</span></a></li>

First, the transition settings have to be in the CSS for the normal state.首先,过渡设置必须在 CSS 中以实现正常状态。 Second, for each to-be-transitioned property you need settings in both rules between which the transition should be made.其次,对于每个要转换的属性,您需要在应该进行转换的两个规则中进行设置。 And third, transitions for which there is not setting (opacity and margin-right in your case) have no effect.第三,没有设置的过渡(在您的情况下不透明度和边距右)不起作用。

 li.searchbar a.searchbar-text { color: initial; margin-left: 0px; transition: color .9s, margin-left .5s; -webkit-transition: color .9s, margin-left .5s; } li.searchbar a.searchbar-text:hover { color: red; margin-left:-10px; }
 <li class="col-sm-5 searchbar"> <a class="text-left"><i class="fa fa-search" aria-hidden="true"></i></a> <a class="searchbar-text"> <span class="first-seach-text">Search France</span> <span class="second-seach-text">and beyond</span> </a> </li>

The easy fix is to simply add the same transition to the original element (as well as the hover state of the element).简单的解决方法是简单地将相同的过渡添加到原始元素(以及元素的悬停状态)。

 .effect { border: none; margin: 0 auto; -webkit-transition: all 0.4s ease-in-out; -moz-transition: all 0.4s ease-in-out; -o-transition: all 0.4s ease-in-out; transition: all 0.4s ease-in-out; } .effect:hover { -webkit-transform: scale(1.1); -moz-transform: scale(1.1); -o-transform: scale(1.1); transform: scale(1.1); -webkit-transition: all 0.4s ease-in-out; -moz-transition: all 0.4s ease-in-out; -o-transition: all 0.4s ease-in-out; transition: all 0.4s ease-in-out; }

As Johannes said add the transition effect on the rule element and not on the hover state and in the hover state you need to be specific on the desire effects;正如 Johannes 所说,在规则元素上添加过渡效果,而不是在悬停状态上添加过渡效果,在悬停状态下,您需要具体说明期望效果; if you want margint-right and opacity you need to specify the rule for those effects:如果你想要 margin-right 和不透明度,你需要为这些效果指定规则:

li.searchbar a.searchbar-text {
  transition: color .9s, opacity .9s, margin-left .5s, margin-right .5s;
}

li.searchbar a.searchbar-text:hover {
  color: red;
  margin-left: -10px;
  margin-right: 10px;
  opacity: .5;
}

In this way the transition will be implemented when the cursor hover-over the element and when the cursor goes away.这样,当光标悬停在元素上和光标离开时,将实现转换。

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

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