2015-09-23 54 views
0

有沒有辦法檢測是否一個div PNG背景或PNG圖像部分透明的測試使用另一個div作爲參考?可以說,每次#char在不透明度小於100%的像素上時,都會被檢測到並提醒。有沒有辦法解決這個問題與JavaScript或jQuery?JavaScript的Png透明hitTest檢測與div

LIVE DEMO HERE

$(document).ready(function() { 

    setInterval(moveChar, 20); 
    var keys = {} 

    $(document).keydown(function (e) { 
     keys[e.keyCode] = true; 
    }); 

    $(document).keyup(function (e) { 
     delete keys[e.keyCode]; 
    }); 


    function moveChar() { 

     for (var direction in keys) { 
      if (!keys.hasOwnProperty(direction)) continue; 
      if (direction == 37) { 
       $("#char").animate({ 
        left: "-=5" 
       }, 0); 
      } 
      if (direction == 38) { 
       $("#char").animate({ 
        top: "-=5" 
       }, 0); 
      } 
      if (direction == 39) { 
       $("#char").animate({ 
        left: "+=5" 
       }, 0); 
      } 
      if (direction == 40) { 
       $("#char").animate({ 
        top: "+=5" 
       }, 0); 
       walk("down"); 
      } 
     } 
    } 

}); 
+0

可能重複http://stackoverflow.com/questions/19807049/html5-canvas-hittest-arbitrary-shape) –

+0

它沒有重複,因爲我想爲移動的DIV應用一條規則。 –

+0

到目前爲止,Web平臺中獲取像素值的唯一選項是。 –

回答

-1

我不知道這會工作,但我認爲它會:

我所做的研究一個合理的量到這個這個問題,並得出結論,它在JavaScript中創建功能測試並不可行,並且IE 6是唯一不支持PNG透明度的嚴重使用的瀏覽器。既然如此,那麼您最好的選擇就是加布裏埃爾羅斯推薦的條件性評論。它有可能在純JavaScript,我認爲使用條件的意見是你想要什麼:

var div = document.createElement("div"); 
div.innerHTML = "<!--[if lte IE 6]><i></i><![endif]-->"; 
var isIe6orLower = (div.getElementsByTagName("i").length == 1); 

alert("Is IE 6 or lower: " + isIe6orLower); 

See the link

[HTML5畫布則hitTest任意形狀(的
+0

謝謝,但在這種情況下,IE 6不會改變,工作與否。 –

+0

如果你喜歡我的回答請投票@FernandoSouza –

+0

請參閱鏈接http://www.sitepoint.com/microsoft-drop-ie10-conditional-comments/ –