gpt4 book ai didi

javascript - 使用 JavaScript 访问 "animate"我的网页

转载 作者:行者123 更新时间:2023-12-02 20:43:44 25 4
gpt4 key购买 nike

我想我应该编写一个简单的脚本来为我的网页添加一些动画。

基本的想法是,当我按下按钮一次时,让 div 变大,然后当我再次按下按钮时,缩小到原始大小。我很好地处理了生长的部分,但我无法让它再次缩小。

我提供了一个完整的示例,你能帮我修复它吗?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Test</title>
<meta http-equiv="Content-Language" content="Estonian" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15" />
</head>

<body>
<script type="text/javascript">
var i;
var increasing = new Boolean("true");
var height;

function testFunction() {
height = parseInt(document.getElementById("testDiv").offsetHeight);
if( increasing == true ) {
i = height + 4;
document.getElementById("testDiv").style.height = i + "px";
if( height >= 304 ) {
increasing = false;
}
else {
pause(30);
}
}
else {
i = height - 4;
document.getElementById("testDiv").style.height = i + "px";
if( height <= 104 ) {
increasing = true;
}
else {
pause(30);
}
}
}
function pause(ms) {
setTimeout("testFunction()", ms);
}
</script>
<button id="button" type="button" onclick="testFunction();">Do it!</button.
<div id="testDiv" style="border: 2px solid black; width: 400px; height: 100px; text-align: center;">
Hello!
</div>
</body>

</html>

最佳答案

使用像 jQuery 这样的标准库来做到这一点,不要自己做,因为它肯定不会跨浏览器。

使用 jQuery,您可以执行以下操作:

$("#div-id").animate({"height": 300}, 1000);

这将在 1000 毫秒 = 1 秒内将 div 高度更改为 300 像素。

关于javascript - 使用 JavaScript 访问 "animate"我的网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1832916/

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