gpt4 book ai didi

javascript - 应用于 HTML 元素的 JavaScript 动画问题

转载 作者:行者123 更新时间:2023-11-30 13:59:15 25 4
gpt4 key购买 nike

<html>
<body onload="load_script()">
<h3 id="description">Text</h3>

<script>
var size_start_h3 = 10.72;
var size_end_h3 = 18.72;

function start_h3() {
if(size_start_h3 < size_end_h3) {
document.getElementById("description").style.fontSize = size_start_h3;
size_start_h3 += 2;
var timeout = setTimeout(function() {
start_h3();
}, 1200);
}
}

function load_script() {
start_h3();
// others functions;
}

</script>

</body>
</html>

我有一段 JavaScript 代码;如果我离线,它会工作;如果我在线,当我将它包含在 .js 文件中时它不起作用,即使我将它插入到标签中也是如此

我设置了两个变量,标题 h3 在开始和结束时将采用的大小。我写了一个函数,如果初始尺寸小于最终尺寸,文本将逐渐增加 2 px 的大小,直到达到最终尺寸。启动函数将在页面加载时通过另一个函数启动(以及其他脚本函数)

然而,控制台不会返回错误,如果我在函数末尾写入 console.log(size_start_h3),它的值将变为 18.72;因此,函数可以工作,但 h3 的大小不会改变。欢迎任何帮助。谢谢。

最佳答案

CSS 度量要求将度量标识为单位值的一部分。您需要将其附加到字体大小的字符串中。

这里我加了“px”:

<html>
<body onload="load_script()">
<h3 id="description">Text</h3>

<script>
var size_start_h3 = 10.72;
var size_end_h3 = 18.72;

function start_h3() {
if(size_start_h3 < size_end_h3) {
document.getElementById("description").style.fontSize = size_start_h3 + "px";
size_start_h3 += 2;
var timeout = setTimeout(function() {
start_h3();
}, 1200);
}
}

function load_script() {
start_h3();
// others functions;
}

</script>

</body>
</html>

关于javascript - 应用于 HTML 元素的 JavaScript 动画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56652552/

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