在做彈出層、下拉選單時,常會遇到 div 明明設了很高的 z-index,卻還是被 iframe 蓋住的惱人問題。以下整理兩種典型情況與解法。
類似結構:
<div></div>
<iframe src="https://caibaojian.com"></iframe>
當 div 使用透明背景(無論是 opacity 還是 rgba 格式)時,除了 Chrome,其他瀏覽器都會出現 div 被 iframe 遮住的情況。
解決方法:讓 div 改用不透明背景,或使用透明的背景圖片。
在 IE 中,若 iframe 內嵌入了 video 影片播放,會導致 div 的 z-index 失效——無論設多大都會被 iframe 蓋住。
解決方法:在 iframe 位址加上參數 wmode=opaque。例如原網址為 https://caibaojian.com,則改為 https://caibaojian.com?wmode=opaque。
順帶一提,Flash 也有類似的 z-index 失效問題,解法相同——加上 wmode 參數:
<param name="wmode" value="transparent">
<EMBED src="FlashAnimation.swf" type="application/x-shockwave-flash" wMode="Transparent">
<iframe border="3"></iframe>,設定邊緣寬度。<iframe frameborder="0"></iframe>,是否顯示三維邊框(0=否,1=是)。<iframe height="31" width="88"></iframe>,設定寬高。<iframe scrolling="no"></iframe>,是否顯示滾動條(yes/no/auto)。<iframe src="girl.gif"></iframe>,指定載入的檔案。以上就是這篇文章的全部內容,希望對大家的學習或工作帶來幫助。