// 示例代码:简单的 JavaScript 动画特效
// 获取 HTML 元素
const box = document.getElementById('box');
// 定义动画函数
function animate() {
let position = 0;
const id = setInterval(frame, 10); // 每 10 毫秒调用一次 frame 函数
function frame() {
if (position == 350) {
clearInterval(id); // 停止动画
} else {
position++; // 更新位置
box.style.top = position + 'px'; // 更新元素的 top 样式属性
box.style.left = position + 'px'; // 更新元素的 left 样式属性
}
}
}
// 调用动画函数
animate();
document.getElementById('box')
获取了 ID 为 box
的 HTML 元素。animate
函数定义了一个动画逻辑,使用 setInterval
每隔 10 毫秒更新一次元素的位置。top
和 left
属性值达到 350px 时,动画停止。clearInterval(id)
用于停止定时器,防止无限循环。确保你的 HTML 文件中有一个 ID 为 box
的元素,并且该元素有 position: absolute;
或 position: relative;
样式,以便可以正确地调整其位置。
上一篇:js filter函数
下一篇:js import as
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站