我想实现一个类似于不透明度的动画效果,使文本逐渐变粗。尝试使用常规的animate()
方法,但未能成功。搜索了一下,但找不到任何示例。这个有可能做到吗?
jQuery:
var Text = $('h1');
Text.click(function() {
Text.animate({'font-weight':'bold'},600)
.delay(200).animate({'font-weight':'normal'},600);
});
我想实现一个类似于不透明度的动画效果,使文本逐渐变粗。尝试使用常规的animate()
方法,但未能成功。搜索了一下,但找不到任何示例。这个有可能做到吗?
jQuery:
var Text = $('h1');
Text.click(function() {
Text.animate({'font-weight':'bold'},600)
.delay(200).animate({'font-weight':'normal'},600);
});
2021年4月更新:现在可以使用
transition: font-weight
或更流畅的可变字体来实现此目标。
字体中的每个字符都有特定的形状。此外,不同重量的相似字符也是不同的——粗体字符不仅仅是与其常规对应项的数学定义偏移量。
使用 jQuery.css() 轻松地从常规修改到加粗或斜体,但目前在浏览器中无法通过 jQuery.animate() 进行字体粗细的过渡。动画效果也很难实现,因为不同重量之间没有“帧”可用于插值处理,它们是分别绘制的。
如果您选择具有不同字重之间一致字母间距的字体(例如Exo),并从细到黑逐步进行动画,您可能会接近所需的结果。
从您的 jsFiddle 开始,这就是我能想到的:
还有一些相当愚蠢的Javascript代码:
Text.click(function() {
Text.css({'font-weight':200});
setTimeout(function(){ Text.css({'font-weight':300})}, 30)
setTimeout(function(){ Text.css({'font-weight':400})}, 60)
setTimeout(function(){ Text.css({'font-weight':500})}, 90)
setTimeout(function(){ Text.css({'font-weight':600})},120)
setTimeout(function(){ Text.css({'font-weight':700})},150)
setTimeout(function(){ Text.css({'font-weight':800})},180)
setTimeout(function(){ Text.css({'font-weight':900})},210)
});
transition: font-weight
。 - empiguet.animate {
font-size: 35px;
transition: 0.6s;
}
.animate:hover {
text-shadow: 0 0 2px black;
transition: 0.6s;
}
<div class="animate">StackOverflow</div>
你也可以使用jQuery,通过使用addClass()
:
$("#animateBold").click(function() {
$(".animate").addClass("bold");
});
.animate {
font-size: 30px;
}
/* Then .bold CSS class */
.bold {
text-shadow: 0 0 2px black;
transition: 0.6s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="animate">StackOverflow<div>
<button id="animateBold">Animate!</button>
或者,如果你想要一个切换效果,可以使用 三元运算符
:
($(".animate").hasClass("bold")) ? $(".animate").removeClass("bold") : $(".animate").addClass("bold");
$("#toggleBold").click(function() {
($(".animate").hasClass("bold")) ? $(".animate").removeClass("bold") : $(".animate").addClass("bold");
});
.animate {
font-size: 30px;
transition: 0.6s;
}
.bold {
text-shadow: 0 0 2px black, 0 0 2px black;
transition: 0.6s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="animate">StackOverflow<div>
<button id="toggleBold">Animate!</button>
使用 font-variation-settings(https://developer.mozilla.org/en-US/docs/Web/CSS/font-variation-settings)是可能的。
当你将鼠标悬停在品牌名称上时,你可以在此网站上查看它的实际效果 - https://www.ahundredmonkeys.com/
从技术上讲,font-weight可以是数字,如100或800,但实际上浏览器只实现了普通或粗体。 Animate接受数字或“hide”、“show”或“toggle”之一。 所以你的“bold”不起作用。 理论上,您可以将font-weight设置为数字,然后传递一个数字来进行动画处理。(400是“normal” weight,700是bold [1]) 可惜在jQuery中似乎无法实现。
但是!您可以使用CSS3来实现此效果,尽管它不能在Internet Explorer上工作,但您的期望效果将覆盖50%以上的人口。请参见此示例。
http://www.quackit.com/css/css3/properties/css_transition-property.cfm
.resize:hover { font-weight:bold; }
,你会发现它不是动画效果,只是在粗体和普通字体之间切换。这样是行不通的。 - jeroen
/:
我已经用text-shadow实现了,但我相信这种方法会更好。 - Barlas Apaydin