2
我試圖讓一個元素內<div>
的jsfiddle代碼不工作,在本地機器
值得慶幸的是隨機移動,有人發現可以發現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>
爲什麼你會期望它的工作?你的腳本的源代碼不是以'http:' – MetalFrog 2012-08-16 13:45:17
開始的,你的腳本源不應該包含協議,即
對不起我的英文不好
我有想法複製完整的源代碼
步驟
點擊分享按鈕
複製的2鏈接盒分享全屏效果
然後去那個鏈接(複製鏈接)。它示出了結果
然後點擊右鍵,單擊視圖幀源。
現在你的源代碼顯示
然後把腳本src和CSS的確切鏈接。
現在你的代碼工作
來源
2013-05-08 13:34:40