【发布时间】:2019-05-03 13:09:12
【问题描述】:
我是一个初学者,制作一个基本网页,当页面加载时,该网页的文本动画从屏幕顶部到中间。动画本身效果很好,但我无法停止循环。动画本身会停止,但我认为该功能会不断循环。因此,网页上的其他元素(例如链接等)无法正常运行,因为 javascript 在技术上尚未完成。我将如何结束该功能?谢谢。
window.onload = Animation;
function Animation() {
//Get Element, Use Position to detemrine text's position.
var Element = document.getElementById("AnimationText");
var Position = -500;
var ID = setInterval(OnFrame, 20);
function OnFrame() {
if (Position > 50) {
clearInterval(ID);
} else {
if (Position > -100 && Position <= 0) {
//Middle of animation, while text is between -100 and 0 position.
Position = Position + 3;
} else if (Position > 0) {
//End of animation until text position is > 50.
Position = Position + 2;
} else {
//Start of animation until text position is at -100.
Position = Position + 4;
}
Element.style.top = Position + 'px';
}
}
}
HTML:
<!doctype HTML>
<HTML>
<head>
<link href="https://fonts.googleapis.com/css?family=Sedgwick+Ave" rel="stylesheet">
<link href = "HomepageStyle.css" rel = "stylesheet" type = "text/css">
<script src = "HomepageAnimation.js"></script>
</head>
<body>
<div id = "AnimationText">
<h1>TNT</h1>
</div>
<center>
<a href = "LogIn.html">
<img src = "_assets/LoginButtonHome.png" style = "margin-right: 40px; margin-top: 400px;">
</a>
</center>
</body>
</HTML>
【问题讨论】:
-
调试时推荐的背景音乐:youtube.com/watch?v=JhqyZeUlE8U
标签: javascript html css animation