gpt4 book ai didi

javascript - 如何在使用辅助类添加和删除类时应用过渡

转载 作者:行者123 更新时间:2023-11-28 01:02:10 26 4
gpt4 key购买 nike

我正在尝试在添加和删除(切换)类 .fa-heart-o.fa-heart 时添加过渡,并使用名为 .fa-heart-slow 像这样。但如您所见,.fa-heart-slow 没有在其中添加任何转换。

我该如何解决这个问题?

$(".table").hover(function() {
$(this).find(".fa").removeClass('fa-heart-o').addClass('fa-heart fa-heart-slow');

}, function() {
$(this).find(".fa").removeClass('fa-heart-slow fa-heart').addClass('fa-heart-o');

});
@import url("https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css");
body {
padding: 20px;
}

.fa-heart-slow {
-webkit-transition: all 1s ease;
-moz-transition: all 1s ease;
-o-transition: all 1s ease;
transition: all 1s ease;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table">
<tr>
<td><i class="fa fa-heart-o" aria-hidden="true"></i></td>
</tr>

</table>

最佳答案

我不相信这会按照您的预期工作,因为切换 .fa 类会有效地切换元素的 content 属性和 content 属性未在 W3 规范中列为支持过渡动画:https://www.w3.org/TR/css-transitions-1/#animatable-css

也就是说,创建相同效果的一种简单方法是让两个图标彼此重叠 - 每个 .fa 类一个 - 然后使用纯 CSS 为悬停时所需的行为设置动画。这可以通过以下方式实现。

HTML:

<table class="table">
<tr>
<td><i class="fa fa-heart-o icon-default"></i><i class="fa fa-heart icon-hover"></i></td>
</tr>
</table>

CSS:

.table .fa {
transition: opacity 1s ease;
}
.icon-hover {
opacity: 0;
left: -16px;
position: relative;
}
.table:hover .icon-default {
opacity: 0;
}
.table:hover .icon-hover {
opacity: 1;
}

首先,我根据您的示例将转换设置为 1s ease;我们只为 opacity 设置它,因为这是唯一改变的属性。然后我将第二个 .fa 图标 (.icon-hover) 设置为 opacity:0 并将其放在第一个 (.icon-default).

最后,一个 :hover 规则被用来在悬停在 .table 元素上时切换两个图标的不透明度;结合之前定义的 transition 属性,两者似乎交叉淡入/淡出。

此处为 CodePen:https://codepen.io/anon/pen/VELLgj

关于javascript - 如何在使用辅助类添加和删除类时应用过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52581249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com