当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - Javascript Png transparency hitTest detection with div

问题描述:

Is there a way to detect if a div png background or png image part is transparent by hittest using another div as reference?

Lets say, every time #char is over a pixel with opacity less than 100%, this will be detected and alerted. Is there a way to work this out with javascript or jquery?

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");

}

}

}

});

网友答案:

I am not sure this will work, but I think it would:

I've done a reasonable amount of research into this this issue and came to the conclusion that it was not feasible to create a feature test in JavaScript, and that IE 6 is the only browser in serious use that doesn't support PNG transparency. That being the case, your best bet is conditional comments, as recommended by Gabriel Ross. It is possible to use conditional comments in pure JavaScript, which I think is what you want:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: