英文:
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.
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 -->
<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>
<!-- 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 -->
<div class="box"></div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论