html - 当鼠标在公共(public) block 外时如何使动画结束?

标签 html css animation css-animations

当我将鼠标移到文本 block “services-right”上时,动画被触发并且文本向右移动。当我将光标移动到“about-us--services__button”按钮时,文本会返回到左侧的位置。

如何使光标在按钮上时文本不向左移动,当鼠标光标在文本 block 和按钮之外时又回到左边?

(运行代码以了解我的意思)

当我将鼠标悬停在按钮上时,文本不应该向左移动,如果它在按钮和文本区域之外,它应该向左移动

.servicies-right {
  margin-top: 70px;
}

.about-us--services__text {
  position: absolute;
  z-index: 1;
  right: 34%;
  top: 180px;
  transition: 0.6s ease-in;
}

.about-us--services__text:hover {
  transform: translateX(10%);
}

.about-us--services__title {
  float: right;
  font-weight: 700;
}

.about-us--services__button {
  display: inline-block;
  margin-left: 30px;
  padding: 25px 50px 25px 50px;
  background: none;
  color: blue;
  font-weight: bold;
  position: relative;
  transition: color 0.25s ease;
  border: 3px solid #383736;
  cursor: pointer;
}

.about-us--services__button:after {
  position: absolute;
  content: '';
  top: 0;
  left: 0;
  width: 0;
  height: 100%;
  background-color: #383736;
  transform-origin: left;
  transition: width 0.25s ease;
  z-index: -1;
}

.about-us--services__button:hover:after {
  width: 100%;
}

.about-us--services__button-title {
  position: absolute;
  right: 40px;
  bottom: 12px;
}

img {
  max-width: 150px;
  min-width: 150px;
  min-height: 100px;
  max-height: 100px;
}

.servicies-right {
  float: right;
}
<div class="servicies-right">
  <div class="about-us--services__text">
    <span class="about-us--services__title-desctription">Test title text first</span>
    <br>
    <span class="about-us--services__title">Test subtitle</span>
  </div>
  <img src="https://24tv.ua/resources/photos/news/201805/961055.jpg" alt="test" class="move-img">
  <button class="about-us--services__button"><span class="about-us--services__button-title">Abcdfeghrf</span></button>
</div>

最佳答案

向该元素添加另一个速度为 0 秒的 transition,因此“进入”动画发生在 0.6 秒,退出动画发生在 0 秒:

.servicies-right {
  margin-top: 70px;
}

.about-us--services__text {
  position: absolute;
  z-index: 1;
  right: 34%;
  top: 180px;
  transition: 0.6s ease-in;
}

.servicies-right:hover .about-us--services__text {
  transform: translateX(10%);
}

.about-us--services__title {
  float: right;
  font-weight: 700;
}

.about-us--services__button {
  display: inline-block;
  margin-left: 30px;
  padding: 25px 50px 25px 50px;
  background: none;
  color: blue;
  font-weight: bold;
  position: relative;
  transition: color 0.25s ease;
  border: 3px solid #383736;
  cursor: pointer;
}

.about-us--services__button:after {
  position: absolute;
  content: '';
  top: 0;
  left: 0;
  width: 0;
  height: 100%;
  background-color: #383736;
  transform-origin: left;
  transition: width 0.25s ease;
  z-index: -1;
}

.about-us--services__button:hover:after {
  width: 100%;
}

.about-us--services__button-title {
  position: absolute;
  right: 40px;
  bottom: 12px;
}

img {
  max-width: 150px;
  min-width: 150px;
  min-height: 100px;
  max-height: 100px;
}

.servicies-right {
  float: right;
}
<div class="servicies-right">
  <div class="about-us--services__text">
    <span class="about-us--services__title-desctription">Test title text first</span>
    <br>
    <span class="about-us--services__title">Test subtitle</span>
  </div>
  <img src="https://24tv.ua/resources/photos/news/201805/961055.jpg" alt="test" class="move-img">
  <button class="about-us--services__button"><span class="about-us--services__button-title">Abcdfeghrf</span></button>
</div>

关于html - 当鼠标在公共(public) block 外时如何使动画结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53963928/

相关文章:

动画问题中的 Android Alpha 淡入淡出

css - 媒体查询不会覆盖标签样式

c# - WPF Storyboard 动画不闪烁

jquery - IE 和 Chrome 之间溢出属性的不同行为

javascript - 无法在复选框上输入 addEventListener?

javascript - 根据前 3 列对 JSON 数据进行分组,并对 HTML/JS 中第 4 列的编号进行求和

javascript - IIS 中的 AngularJS 路由

javascript - 如何获得此表单验证代码的正确输出?

css - 我可以使用 Sass 将样式应用于基于另一个元素上存在的属性的元素吗?

css - onmouseenter KeyFrame 只工作一次