JavaScript if 参数,忽略图像的透明度
我需要使 if 参数可选。如果这是有道理的话。
如果您发现我的两个函数 98% 相同,那么我需要将这种差异转化为参数,但它对我来说并不合适。
getElement(x, y, class)
其中新参数类更改了我在代码中标记为 //HERE 的内容,
//get the element under the mouse, ignoring all transparency.
function getElement(x, y){
var elem = document.elementFromPoint(x, y);
if($(elem).parents("#game").length != 1){ return -1; }
var pixelAlpha = mousePixelOpacity(elem, x, y);
var track = Array;
for(var i = 0;i<50;i++){
if($(elem).attr("id") == "game"){ return -1;}
if(pixelAlpha == 0){ /////////////////////////////////////////HERE
track[i] = elem;
for(var z = 0; z<i+1; z++){ //hide elements
$(track[z]).hide();
}
elem = document.elementFromPoint(x, y); //set the element right under the mouse.
pixelAlpha = mousePixelOpacity(elem, x, y);
for(var z = 0; z<i+1; z++){ //show all the recently hidden elements
$(track[z]).show();
}
}
if(pixelAlpha != 0){ ///////////////////////////////////////// AND HERE
return elem;
}
}
return -1;
}
//get the tile under the mouse, even if it's behind an object
function getTile(x, y){
var elem = document.elementFromPoint(x, y);
if($(elem).parents("#game").length != 1 && $(elem).attr("id") != "tileHighlight"){ return -1; }
var pixelAlpha = mousePixelOpacity(elem, x, y);
var track = Array;
for(var i = 0;i<50;i++){
if($(elem).attr("id") == "game"){ return -1;}
if(pixelAlpha == 0 || $(elem).attr('class') != "tile"){ /////////HERE
track[i] = elem;
for(var z = 0; z<i+1; z++){ //hide elements
$(track[z]).hide();
}
elem = document.elementFromPoint(x, y); //set the element right under the mouse.
pixelAlpha = mousePixelOpacity(elem, x, y);
for(var z = 0; z<i+1; z++){ //show all the recently hidden elements
$(track[z]).show();
}
}
if($(elem).attr('class') == "tile" && pixelAlpha != 0){ ///// AND HERE
return elem;
}
}
return -1;
}
我在想类似
getElement(x, y, "title") ;
//(This can be right) OR (both of these can be right.)
if( (pixelAlpha == 0) || (class="tile" && onlycountclassifIsaidsovar) ){}
顺便说一下,如果你想的话,我在 http://untitled.servegame.com看看这只小狗的行动。
谢谢!
I need to make if parameters optional. If that makes sense.
If you notice both of my functions are 98% the same, I need to turn this difference into a parameter and it's just not clicking for me.
getElement(x, y, class)
where the new parameter class changes what I have marked as //HERE in my code
//get the element under the mouse, ignoring all transparency.
function getElement(x, y){
var elem = document.elementFromPoint(x, y);
if($(elem).parents("#game").length != 1){ return -1; }
var pixelAlpha = mousePixelOpacity(elem, x, y);
var track = Array;
for(var i = 0;i<50;i++){
if($(elem).attr("id") == "game"){ return -1;}
if(pixelAlpha == 0){ /////////////////////////////////////////HERE
track[i] = elem;
for(var z = 0; z<i+1; z++){ //hide elements
$(track[z]).hide();
}
elem = document.elementFromPoint(x, y); //set the element right under the mouse.
pixelAlpha = mousePixelOpacity(elem, x, y);
for(var z = 0; z<i+1; z++){ //show all the recently hidden elements
$(track[z]).show();
}
}
if(pixelAlpha != 0){ ///////////////////////////////////////// AND HERE
return elem;
}
}
return -1;
}
//get the tile under the mouse, even if it's behind an object
function getTile(x, y){
var elem = document.elementFromPoint(x, y);
if($(elem).parents("#game").length != 1 && $(elem).attr("id") != "tileHighlight"){ return -1; }
var pixelAlpha = mousePixelOpacity(elem, x, y);
var track = Array;
for(var i = 0;i<50;i++){
if($(elem).attr("id") == "game"){ return -1;}
if(pixelAlpha == 0 || $(elem).attr('class') != "tile"){ /////////HERE
track[i] = elem;
for(var z = 0; z<i+1; z++){ //hide elements
$(track[z]).hide();
}
elem = document.elementFromPoint(x, y); //set the element right under the mouse.
pixelAlpha = mousePixelOpacity(elem, x, y);
for(var z = 0; z<i+1; z++){ //show all the recently hidden elements
$(track[z]).show();
}
}
if($(elem).attr('class') == "tile" && pixelAlpha != 0){ ///// AND HERE
return elem;
}
}
return -1;
}
I was thinking something like
getElement(x, y, "title");
//(This can be right) OR (both of these can be right.)
if( (pixelAlpha == 0) || (class="tile" && onlycountclassifIsaidsovar) ){}
By the way I'm at http://untitled.servegame.com if you want to see this puppy in action.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到的唯一区别是 getTile() 中的三个额外测试。您可以使用布尔第三个参数(例如“tile”),如果设置该参数将应用这些测试。
The only difference I see are three extra tests in getTile(). You could use a boolean third arg (eg, 'tile') which if set would apply those tests.