Flash ã® z-index ã Javascript ããæå¹ã«ãã
趣æ¨
Flash ã¯ããã ã®ã¤ã³ã©ã¤ã³ã¬ãã«ç½®æè¦ç´ ã§ããã«ãããããããããã©ã«ãã§ã¯ CSS ã® z-index ã«é¢ãã決ã¾ããã¨ã«å¾ããªããz-index ã¨ã¯ãç»é¢ã® x軸ã»y軸ã®ä¸¡æ¹ã«ç´äº¤ãã軸ã§ãè¦ç´ ãç»é¢ã®æåã«ãããã¦ããã®ãããã¨ã奥ããæ示ãã CSS ããããã£ã§ããããã¨ãã°ãposition:absolute 㧠z-index ãæå®ãã DIV è¦ç´ ã z-index ã®æå®ã®ãªã Flash ã¨éããã¨ãæ¬æ¥ãªãã°ãDIV 㯠Flash ã®æåã«è¡¨ç¤ºãããªããã°ãªããªãã®ã«ãå®éã«ã¯ Flash ã®ã»ããæåã«æ¥ã¦ãã¾ãã
ã§ã¯æã¤æã¯ãªãã®ããã¨ããã¨å®ã¯ wmode ã¨ãã Flash ã®ããããã£ããããããã "transparent" ã«ãã¦ããã°ããããã¨ãã°ã¤ãã®ãããªæãã ã
<!-- EMBED ã使ãã¨ã --> <embed src="foobar.swf" wmode="transparent" /> <!-- OBJECT ã使ãã¨ã --> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"> <param name="movie" value="foobar.swf"/> <param name="wmode" value="transparent"/> </object>
ãã㧠z-index ã«é¢ãã¦æ®éã® HTML è¦ç´ ã®ããã«æ¯èãããã«ãªãããã ã(CSS21:9.9.1)
ä»åã®è©±ã¯ããã® wmode ããããã£ã Javascript ã§åçã«è¨å®ããã«ã¯ã©ãããããã¨ããã話ã§ãããç°¡åã«æããããããããªãããããããªããªã骨ãªã®ã§ããã
Javascript ã®ç½
次ã®ãµã³ãã«ãèãã¦ã¿ããred_clock.swf 㨠blue_clock.swf ã¨ãã Flash ãããã¨æ³å®ãããï¼å®éã®ã¨ãããµã³ãã«ã«ã¯ãFlashbucks ã使ããã¦ããã ãããããã§æè¬ã表æããã)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>flash test</title> <style type="text/css"> #div1 { background-color: blue; position:absolute; width:100px; height:100px; left:50px; top:10px; z-index: 30; } #blue_clock { z-index: 20; position:absolute; width:100px; height:100px; left:50px; top:10px; } #div2 { background-color: red; position:absolute; width:100px; height:100px; left:200px; top:10px; z-index: 30; } #red_clock { position:absolute; width:100px; height:100px; left:200px; top:10px; z-index: 20; } #link1 { position:absolute; width:100px; height:30px; left:350px; top:10px; } #link2 { position:absolute; width:100px; height:30px; left:350px; top:50px; } </style> <script type="text/javascript"> function bring_div1_to_front() { var flash = document.getElementById("blue_clock"); var param = document.createElement("param"); param.setAttribute("name", "wmode"); param.setAttribute("value", "transparent"); flash.appendChild(param); } function bring_div2_to_front() { var elem = document.getElementById("red_clock"); elem.setAttribute("wmode", "transparent"); } </script> </head> <body> <div id="div1"></div> <object id="blue_clock" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"> <param name="movie" value="blue_clock.swf" /> </object> <div id="div2"></div> <embed id="red_clock" src="red_clock.swf" /> <a href="#" id="link1" onclick="bring_div1_to_front();">bring_div1_to_front</a> <a href="#" id="link2" onclick="bring_div2_to_front();">bring_div2_to_front</a> </body> </html>
ä¸ã®ãµã³ãã«ããã©ã¦ã¶ã§è¦ã¦ã¿ãã¨ã2ã¤ã®æè¨ã® Flash ã®å³å´ã« bring_div1_to_front, bring_div2_to_front ã¨ãããªã³ã¯ããããããããã¯ãªãã¯ããã¨ååã®é¢æ°ãå®è¡ããã¦ããããããOBJECT, EMBED åè¦ç´ ã«ããã¦ãwmode ããããã£ã« "transparent" ã¨ããå¤ãè¨å®ãããã¹ã¿ã¤ã«ã§ div1, div2 ã®ã»ãã Flash ãã z-index ã®å¤ã大ããã®ã§ãæ¬æ¥ãªãã° div ã Flash ã®æåã«è¦ããã¯ãã ããä½ãèµ·ãããªããFlash ã¯è©³ãããªãã®ã§ãã©ããã¦ãããªã£ã¦ãã¾ãã®ãããããªããå¼·å¶åæç»ã¿ãããªæ段ãããã®ãããããªããã»ã»ã»ã
対ç
ã¯ã£ããè¨ãã¨ã¾ã£ã¨ããªå¯¾çã¯ããããããç§ããã£ããã¨ã¯ããªãç¡çãããªãã¨ã ã£ãããããã試ãã¦ã¿ãããçµå± EMBED è¦ç´ ãä¸åº¦ãæååã«ããæååã¨ã㦠wmode="transparent" ã追å ããã¤ãã§ããããããã¹ãè¦ç´ ã® innerHTML ã«çªã£è¾¼ããã¨ã«ããããµããã³ EMBED è¦ç´ ã«ããã¨ããèæ¥ããæãã¤ããªãã£ãã ãããããOBJECT è¦ç´ ã®ã¿ã§ãFlash ãçµã¿è¾¼ãã§ãã HTML ææ¸ã«ã¯å¯¾å¿ã§ããªãã以ä¸ã«ã³ã¼ããæ²ããã®ã§ãèå³ãããã°èªãã§ã»ããããç¹ã« OBJECT è¦ç´ ã EMBED è¦ç´ ã«ç½®ãæãã¦ãã¾ãããããããã¤ã±ã¯ã½æããæ¼ããã¨ãããã㯠IE ã§ã Firefox ã§ãåããï¼è©¦ãã¦ããªãã Safari ã§ã大ä¸å¤«ãªã¯ãã ) ãªãããã¯ã¾ãã ããã¨ãããã¨ã§ãåå¼é¡ãããããããããããã¾ãããæ¹ããåããªæ¹ã¯ãã²æãã¦ãã ããã
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>flash test</title> <style type="text/css"> #div1 { background-color: blue; position:absolute; width:50px; height:50px; left:100px; top:30px; z-index: 30; } .blue_clock { /*position:absolute;*/ z-index: 20; width:100px; height:100px; left:20px; top:10px; z-index: 20; } #div2 { background-color: red; position:absolute; width:50px; height:50px; left:280px; top:30px; z-index: 30; } .red_clock { position:absolute; width:100px; height:100px; left:200px; top:10px; z-index: 20; } #link1 { position:absolute; width:100px; height:30px; left:350px; top:10px; } #link2 { position:absolute; width:100px; height:30px; left:350px; top:50px; } </style> <script type="text/javascript"> function _getComputedStyle(element) { if(window.getComputedStyle) { return document.defaultView.getComputedStyle(element, null); } else if(element.currentStyle) { return element.currentStyle; } }; function copyDisplayProperties(src, dest) { var ss = _getComputedStyle(src); var ds = dest.style; var props = "display,position,float,left,top,width,height," + "marginTop,marginRight,marginBottom,marginLeft," + "borderTopWidth,borderRightWidht,borderBottomWidth,borderLeftWidth," + "paddingTop,paddingRight,paddingBottom,paddingLeft"; var prop_list = props.split(","); for(var i = 0, len = prop_list.length; i < len; i++) { var prop = prop_list[i]; ds[prop] = ss[prop]; } } function replaceOneObjectWithEmbed(object) { var html = object.innerHTML; var res = html.match(/(<embed.+?)>/im); if(!res) { return; } var embed_html_open_end = res[1]; var embed_html = embed_html_open_end + ' wmode="transparent">'; // ç´æ¥ object.innerHTML = embed_html ã¨ããªãã®ã¯ãIE ã§ã¨ã©ã¼ã«ãªãããã§ããã var span = document.createElement("span"); span.innerHTML = embed_html; copyDisplayProperties(object, span); var parent = object.parentNode; parent.replaceChild(span, object); } function bringAllObjectsBehind() { var nodes = document.getElementsByTagName("object"); for(var i = 0, len = nodes.length; i < len; i++) { var node = nodes[i]; replaceOneObjectWithEmbed(node); } } function makeEmbedWmodeTransparent(embed) { var wmode = embed.getAttribute("wmode"); // Do nothing in case that wmode has already been set to "transparent" if(wmode && wmode.match(/^transparent$/i)) { return; } var parent = embed.parentNode; var span = document.createElement("span"); embed = parent.replaceChild(span, embed); span.appendChild(embed); var html = span.innerHTML; var html2 = html.replace(/<EMBED /i, '<embed wmode="transparent" '); span.innerHTML = html2; } function bringAllEmbedsBehind() { var nodes = document.getElementsByTagName("embed"); for(var i = 0, len = nodes.length; i < len; i++) { var embed = nodes[i]; makeEmbedWmodeTransparent(embed); } } function bringAllFlashesBehind() { bringAllObjectsBehind(); bringAllEmbedsBehind(); } </script> </head> <body> <div id="div1"></div> abc <object class="blue_clock" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"> <param name="movie" value="blue_clock.swf" /> <embed class="blue_clock" src="blue_clock.swf" style="left:0px; top:0px;" /> </object> def <div id="div2"></div> <embed class="red_clock" src="red_clock.swf" /> <a href="#" id="link1" onclick="bringAllFlashesBehind();">bringAllFlashesBehind</a> </body> </html>