最近項目需要google earth有關的技術點,遇見了要在google earth上面添加浮動層的問題。怎么設置z-index也沒發使得元素在earth上面顯示,總是被壓下去。
補充:google map沒有此類問題,設z-index就可以控制浮動層的位置。
查了好多資料,才發現也有其他人遇見。好像是google earth固有的“缺陷”。
解決辦法:
利用動態生成iframe講圖片添加進去并設置z-index。iframe-shim-for-google-earth
經典代碼階段:
//本地的html按鈕 function createNativeHTMLButton(x, y, width, height,id) { // 創建浮動層 var button = document.createElement("div"); button.className="nxte"; button.id=id; var ahref = document.createElement('a'); ahref.href = '#'; button.appendChild(ahref); // create an IFRAME shim for the button var iframeShim = document.createElement('iframe'); iframeShim.frameBorder = 0; iframeShim.id=id+"Iframe"; iframeShim.scrolling = 'no'; iframeShim.src = (navigator.userAgent.indexOf('MSIE 6') >= 0) ? '' : 'javascript:void(0);'; // position the button and IFRAME shim var pluginRect = getElementRect(document.getElementById('earth')); button.style.position = iframeShim.style.position = 'absolute'; button.style.left = iframeShim.style.left = (pluginRect.left + x) + 'px'; button.style.top = iframeShim.style.top = (pluginRect.top + y) + 'px'; button.style.width = iframeShim.style.width = width + 'px'; button.style.height = iframeShim.style.height = height + 'px'; // set up z-orders button.style.zIndex = 10; iframeShim.style.zIndex = button.style.zIndex - 1; document.body.appendChild(button); document.body.appendChild(iframeShim); } /** * Helper function to get the rectangle for the given HTML element. */ function getElementRect(element) { var left = element.offsetLeft; var top = element.offsetTop; var p = element.offsetParent; while (p && p != document.body.parentNode) { if (isFinite(p.offsetLeft) && isFinite(p.offsetTop)) { left += p.offsetLeft; top += p.offsetTop; } p = p.offsetParent; } return { left: left, top: top, width: element.offsetWidth, height: element.offsetHeight }; }引用:createNativeHTMLButton(left, top, 14, 49,"nxte");
一個很好的demo: http://lineandpixel.com/blog/iframe-shim-for-google-earth
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
