2011-03-02 29 views
1

注:這是我剛纔問了一個問題的延伸,我已經採取了意見,但仍然沒有運氣與建議擴展:在Javascript中移動閉塞不會工作

我試圖進行編輯&一個網頁,當你點擊鏈接時,鏈接每100毫秒對角線移動一次。

所以我有我的Javascript,但現在當我點擊鏈接沒有任何反應。我已經通過JSLint運行我的代碼(因此改變comaprisions ===不==,這在JS中奇怪?)。我從JSLink得到這個錯誤:

Error: Implied global: self 15,38, document 31

你覺得我做錯了什麼?

<script LANGUAGE="JavaScript" type = "text/javascript"> 
<!-- 
    var block   = null; 
    var clockStep  = null; 
    var index   = 0; 
    var maxIndex  = 6; 
    var x    = 0; 
    var y    = 0; 
    var timerInterval = 100; // milliseconds 
    var xPos   = null; 
    var yPos   = null; 

    function moveBlock() 
    { 
     if (index < 0 || index >= maxIndex || block === null || clockStep === null) 
     { 
      self.clearInterval(clockStep); 
      return; 
     } 

     block.style.left = xPos[index] + "px"; 
     block.style.top = yPos[index] + "px"; 
     index++; 
    } 

    function onBlockClick(blockID) 
    { 
     if (clockStep !== null) 
     { 
      return; 
     } 

     block = document.getElementById(blockID); 
     index = 0; 
     x  = number(block.style.left); // parseInt(block.style.left, 10); 
     y  = number(block.style.top); // parseInt(block.style.top, 10); 
     xPos = new Array(x+10, x+20, x+30, x+40, x+50, x+60); 
     yPos = new Array(y-10, y-20, y-30, y-40, y-50, y-60); 

     clockStep = self.SetInterval(moveBlock(), timerInterval); 
    } 
--> 
</script> 

<style type="text/css" media="all"> 
    <!-- 
    @import url("styles.css"); 

    #blockMenu { z-index: 0; width: 650px; height: 600px; background-color: blue; padding: 0; } 

    #block1 { z-index: 30; position: relative; top: 10px; left: 10px; background-color: red; width: 200px; height: 200px; 
       margin: 0; padding: 0; /* background-image: url("images/block1.png"); */ } 
    #block2 { z-index: 30; position: relative; top: 50px; left: 220px; background-color: red; width: 200px; height: 200px; 
       margin: 0; padding: 0; /* background-image: url("images/block1.png"); */ } 
    #block3 { z-index: 30; position: relative; top: 50px; left: 440px; background-color: red; width: 200px; height: 200px; 
       margin: 0; padding: 0; /* background-image: url("images/block1.png"); */ } 
    #block4 { z-index: 30; position: relative; top: 0px; left: 600px; background-color: red; width: 200px; height: 200px; 
       margin: 0; padding: 0; /* background-image: url("images/block1.png"); */ } 

    #block1 a { display: block; width: 100%; height: 100%; } 
    #block2 a { display: block; width: 100%; height: 100%; } 
    #block3 a { display: block; width: 100%; height: 100%; } 
    #block4 a { display: block; width: 100%; height: 100%; } 

    #block1 a:hover { background-color: green; } 
    #block2 a:hover { background-color: green; } 
    #block3 a:hover { background-color: green; } 
    #block4 a:hover { background-color: green; } 

    #block1 a:active { background-color: yellow; } 
    #block2 a:active { background-color: yellow; } 
    #block3 a:active { background-color: yellow; } 
    #block4 a:active { background-color: yellow; } 

    --> 
</style> 
+0

您能夠將jQuery或任何第三方庫?還有,如果你使用jsfiddle.net這將是很好:-) – macarthy 2011-03-02 23:29:30

回答

2

對於初學者,self.setInterval和self.clearInterval未在任何地方定義

+0

你的意思是我應該刪除自己。部分?因爲我已經找到了很多功能與這個https://developer.mozilla.org/en/DOM/window.setInterval – Mack 2011-03-02 23:44:32

+0

@Mack的教程:我會將'self'改爲'window'...但這可能不會成爲問題。你需要在這一行的moveBlock()函數週圍添加引號:clockStep = self.SetInterval('moveBlock()',timerInterval); – Gerrat 2011-03-02 23:53:55