CSS边框悬停离开时的过渡效果

huangapple go评论68阅读模式
英文:

CSS Border transition on hover leave

问题

我正在尝试在一个 div 上创建一个动画效果,当鼠标悬停时,边框和阴影会使 div 看起来是3D的,然后在光标离开时慢慢淡出/消失。

正如您所看到的,第一个示例(我的代码)没有边框消失的过渡效果,而第二个示例(我想要的效果)有。这是一个轻微的动画效果,但确实存在。

我该如何复制边框消失的动画效果?

到目前为止,当鼠标悬停时,我已经让动画播放了,但我不确定如何制作边框消失的动画效果,我假设我需要使用 onmouseleave 以及一些过渡效果。

blog-post:hover 上使用 border-width 的 CSS 过渡效果。

但我不确定如何实现类似 GIF 中所示的 onmouseleave 过渡效果。

.blog-post {
  border-radius: 25px;
  height: fit-content;
  transition-property: border-width;
  transition-timing-function: cubic-bezier(.4, 0, .2, 1);
  transition-duration: .2s;
  display: block;
}

.blog-post:hover {
  border-radius: 25px;
  border-image: none;
  border-style: none none solid solid;
  border-width: medium medium 8px 6px;
  border-color: #1D89E7;
  filter: drop-shadow(0px 2px 2px #000);
  background: #f0f6fb;
}

.blog-img {
  padding: 30px;
  border-radius: 25px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">

<div class="bg-white blog-post shadow-lg rounded-lg max-w-sm mb-5">
  <a href="#">
    <img class="img-responsive blog-img" src="https://flowbite.com/docs/images/blog/image-1.jpg" alt="">
  </a>
  <div class="pl-5 pr-5 pb-3 mt-2">
    <a href="#">
      <h5 class="text-gray-900 font-bold text-2xl tracking-tight mb-2">Noteworthy technology acquisitions 2021</h5>
    </a>
    <p class="text-muted blog-date-text">June 6, 2023</p>
  </div>
</div>
英文:

I'm trying to create an animation effect on a div where a border and drop shadow makes the div look 3d on hover and then slowly fades/disappears when the cursor leaves.

CSS边框悬停离开时的过渡效果

As you can see there is no border disappear transition for the first 1 example (my code) while there is for the second (what I want). It's a slight animation but it's there.

How do I replicate the border disappear animation?

So far I've gotten the animation to play when hover but I'm not sure how I'd go about animating the border disappear, I'm assuming I need to use onmouseleave and some transition.

CSS transition on border-width that plays on blog-post:hover

However I'm not sure how to get a transition onmouseleave like shown in the gif.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.blog-post {
  border-radius: 25px;
  height: fit-content;
  transition-property: border-width;
  transition-timing-function: cubic-bezier(.4, 0, .2, 1);
  transition-duration: .2s;
  display: block;
}

.blog-post:hover {
  border-radius: 25px;
  border-image: none;
  border-style: none none solid solid;
  border-width: medium medium 8px 6px;
  border-color: #1D89E7;
  filter: drop-shadow(0px 2px 2px #000);
  background: #f0f6fb;
}

.blog-img {
  padding: 30px;
  border-radius: 25px;
}

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css&quot; integrity=&quot;sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N&quot; crossorigin=&quot;anonymous&quot;&gt;

&lt;div class=&quot;bg-white blog-post shadow-lg rounded-lg max-w-sm mb-5&quot;&gt;
  &lt;a href=&quot;#&quot;&gt;
    &lt;img class=&quot;img-responsive blog-img&quot; src=&quot;https://flowbite.com/docs/images/blog/image-1.jpg&quot; alt=&quot;&quot;&gt;
  &lt;/a&gt;
  &lt;div class=&quot;pl-5 pr-5 pb-3 mt-2&quot;&gt;
    &lt;a href=&quot;#&quot;&gt;
      &lt;h5 class=&quot;text-gray-900 font-bold text-2xl tracking-tight mb-2&quot;&gt;Noteworthy technology acquisitions 2021&lt;/h5&gt;
    &lt;/a&gt;
    &lt;p class=&quot;text-muted blog-date-text&quot;&gt;June 6, 2023&lt;/p&gt;

  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

我会考虑使用box-shadow结合translation效果:

.box {
  width: 200px;
  height: 200px;
  margin: 10px;
  border-radius: 25px;
  background: #ccc;
  box-shadow: 0 0 blue;
  transition: .4s linear;
  cursor: pointer;
}
.box:hover {
  box-shadow: -8px 8px blue;
  transform: translate(8px, -8px);
}
<div class="box"></div>
英文:

I would consider box-shadow combined with translation:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.box {
  width: 200px;
  height: 200px;
  margin:10px;
  border-radius: 25px;
  background: #ccc;
  box-shadow: 0 0 blue;
  transition: .4s linear;
  cursor: pointer;
}
.box:hover {
  box-shadow: -8px 8px blue;
  transform: translate(8px, -8px);
}

<!-- language: lang-html -->

&lt;div class=&quot;box&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月9日 12:48:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定