阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

网页视频遮住顶层 DIV 的解决办法

213次阅读
没有评论

共计 4258 个字符,预计需要花费 11 分钟才能阅读完成。

在一个客户的案例中,遇到一个比较奇怪的问题,网页中插入优酷视频后,滚动时会遮住最顶层的 div,如下图所示:

网页视频遮住顶层 DIV 的解决办法

只在 IE8 及以下版本的 IE 浏览器才出现这个问题,修改 z-index 属性没办法解决。于是搜索了一下,发现设置一下 wmode 的值为 opaque 就可以了。最终的代码样例如下:

1
2
3
4
5
6
7
8
<object width="720" height="452" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" align="middle">
    <param name="src" value="http://player.youku.com/player.php/sid/XNzUwOTQwMDI0/v.swf" />
    <param name="allowfullscreen" value="true" />
    <param name="quality" value="high" />
    <param name="allowscriptaccess" value="always" />
    <param name="wmode" value="opaque" />
    <embed wmode="opaque" width="720" height="452" type="application/x-shockwave-flash" src="http://player.youku.com/player.php/sid/XNzUwOTQwMDI0/v.swf" allowfullscreen="true" quality="high" allowscriptaccess="always" align="middle" />
</object>

注意看代码第 6 行,就是新增的 wmode 属性。也许你会说,每个插入的视频都这样添加一行,实在是麻烦,好吧,如过你用的是 WordPress,只需要将下面的代码添加到主题的 functions.php 就可以啦:

1
2
3
4
5
6
7
89
10
11
12
13
14
/**
 * WordPress 快速替换文章 / 评论的某些文字内容
 * http://www.wpdaxue.com/replace-text-of-content-or-comment.html
 */
function wpdaxue_replace_text($text){
	$replace = array(
		'<embed' => '<param name="wmode"value="opaque"/><embed wmode="opaque"'
		);
	$text = str_replace(array_keys($replace), $replace, $text);
	return $text;
}
add_filter('the_content', 'wpdaxue_replace_text'); // 正文
add_filter('the_excerpt', 'wpdaxue_replace_text'); // 摘要
add_filter('comment_text', 'wpdaxue_replace_text'); // 评论

关于 wmode 属性的更多内容,请继续往下看:

以下内容转载自:http://www.zhihu.com/question/20144006

在做 web 开发中可能会遇到 flash 遮挡页面中元素的情况,无论怎么设置 flash 容器和层的深度 (z-index) 也无济于事,现有的解决方案是在插入 flash 的 embed 或 object 标签中加入”wmode”属性并设置为 wmode=“transparent”或”opaque”,但 wmode 属性到底是什么意义,为什么可以解决这个问题呢?

window mode(wmode)

wmode 即窗口模式总共有三种,看看当年 Macromedia 官方的说法:

  • Window: Use the Window value to play a Flash Player movie in its own rectangular window on a web page. This is the default value for wmode and it works the way the classic Flash Player works. This normally provides the fastest animation performance.
  • Opaque: By using the Opaque value you can use JavaScript to move or resize movies that don’t need a transparent background. Opaque mode makes the movie hide everything behind it on the page. Additionally, opaque mode moves elements behind Flash movies (for example, with dynamic HTML) to prevent them from showing through.
  • Transparent: Transparent mode allows the background of the HTML page, or the DHTML layer underneath the Flash movie or layer, to show through all the transparent portions of the movie. This allows you to overlap the movie with other elements of the HTML page. Animation performance might be slower when you use this value.

window 模式

默认情况下的显示模式,在这种模式下 flash player 有自己的窗口句柄,这就意味着 flash 影片是存在于 Windows 中的一个显示实例,并且是在浏览器核心显示窗口之上的,所以 flash 只是貌似显示在浏览器中,但这也是 flash 最快最有效率的渲染模式。由于他是独立于浏览器的 HTML 渲染表面,这就导致默认显示方式下 flash 总是会遮住位置与他重合的所有 DHTML 层。

但是大多数苹果电脑浏览器会允许 DHTML 层显示在 flash 之上,但当 flash 影片播放时会出现比较诡异的现象,比如 DHTML 层像被 flash 刮掉一块一样显示异常。

Opaque 模式

这是一种无窗口模式,在这种情况下 flash player 没有自己的窗口句柄,这就需要浏览器需要告诉 flash player 在浏览器的渲染表面绘制的时间和位置。这时 flash 影片就不会在高于浏览器 HTML 渲染表面而是与其他元素一样在同一个页面上, 因此你就可以使用 z -index 值来控制 DHTML 元素是遮盖 flash 或者被遮盖。

Transparent 模式

透明模式,在这种模式下 flash player 会将 stage 的背景色 alpha 值将为 0 并且只会绘制 stage 上真实可见的对象,同样你也可以使用 z -index 来控制 flash 影片的深度值,但是与 Opaque 模式不同的是这样做会降低 flash 影片的回放效果,而且在 9.0.115 之前的 flash player 版本设置 wmode=”opaque”或”transparent”会导致全屏模式失效。

以上内容转自蓝色理想

结论:

添加一个 wmode=”transparent”属性或者 wmode=”opaque”属性 即可
推荐使用 wmode=”opaque”因为 wmode=”transparent”会影响 flash 的性能

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-22发表,共计4258字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中