gpt4 book ai didi

jquery - jsFiddle 代码无法在本地计算机上运行

转载 作者:行者123 更新时间:2023-12-01 02:45:25 26 4
gpt4 key购买 nike

我正在尝试使元素在 <div> 内随机移动

值得庆幸的是,有人找到了解决方案,可以找到here

但是,当我在本地计算机或服务器上运行它时,该元素不会移动。

这是我的源代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>

<style type="text/css">
div.a {
width: 50px;
height:50px;
background-color:red;
position:fixed;
}​
</style>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
animateDiv();

});

function makeNewPosition(){

// Get viewport dimensions (remove the dimension of the div)
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;

}​
</script>
</head>

<body>
<div class='a'></div>​
</body>
</html>

最佳答案

在 Chrome 中打开时出现错误:

Uncaught SyntaxError: Unexpected token ? 

看起来 JavaScript 中的结束大括号后面有一个不可见的字符。用Notepad++打开,删除多余的字符。

当您直接从 fiddle 复制和粘贴时,我已经看到这种情况发生。

关于jquery - jsFiddle 代码无法在本地计算机上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11988482/

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