我有脚本,但它似乎不起作用。我没有错误消息,但图像只是坐在那里什么都不做。我完全不知道问题出在哪里,我们将非常感谢任何帮助。
HTML:
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href='style.css'/>
<script src='script.js'></script>
</head>
<body>
<div class='a'></div>
</body>
</html>
CSS:
div.a {
width: 50px;
height:50px;
background-color:red;
position:fixed;
}
的JavaScript
$(document).ready(function () {
animateDiv();
});
function makeNewPosition() {
var h = $(window).height() - 50;
var w = $(window).width() - 50;
var nh = Math.floor(Math.random() * h);
var nw = Math.floor(Math.random() * w);
return [nh, nw];
}
function animateDiv() {
var newq = makeNewPosition();
var oldq = $('.a').offset();
var speed = calcSpeed([oldq.top, oldq.left], newq);
$('.a').animate({
top: newq[0],
left: newq[1]
}, speed, function () {
animateDiv();
});
};
function calcSpeed(prev, next) {
var x = Math.abs(prev[1] - next[1]);
var y = Math.abs(prev[0] - next[0]);
var greatest = x > y ? x : y;
var speedModifier = 0.1;
var speed = Math.ceil(greatest / speedModifier);
return speed;
}
答案 0 :(得分:0)
你应该从这里链接到jquery库:https://jquery.com/只需下载库并在html中引用它
这是您将文件添加到页面的方式:
<!DOCTYPE html>
<html>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<head>
<link rel='stylesheet' href='style.css'/>
<script src='script.js'></script>
</head>
<body>
<div class='a'></div>
</body>
</html>