javascript - 自定义视频插件在 IE 浏览器中不起作用
问题描述
我为我的视频创建了一个插件,通过这个插件,我可以使用我的自定义格式的视频短代码我使用我在 jsfiddle 中看到的一个大播放按钮的代码。它在所有浏览器中都可以正常工作,但在 ie 中却不行。我试图替换 addeventlistener 和 setattribute 但它没有帮助。
var videoPlayButton,
videoWrapper = document.getElementsByClassName('video-wrapper')[0],
video = document.getElementsByTagName('video')[0],
videoMethods = {
renderVideoPlayButton: function() {
if (videoWrapper.contains(video)) {
this.formatVideoPlayButton()
video.classList.add('has-media-controls-hidden')
videoPlayButton = document.getElementsByClassName('video-overlay-play-button')[0]
/*videoPlayButton.addEventListener('click', this.hideVideoPlayButton)*/
if (videoPlayButton.addEventListener) {
videoPlayButton.addEventListener("click", this.hideVideoPlayButton, false);
}
else {
videoPlayButton.attachEvent("onclick", this.hideVideoPlayButton);
}
}
},
formatVideoPlayButton: function() {
videoWrapper.insertAdjacentHTML('beforeend', '\
<svg class="video-overlay-play-button" viewBox="0 0 200 200" alt="Play video">\
<circle cx="100" cy="100" r="90" fill="none" stroke-width="15" stroke="#fff"/>\
<polygon points="70, 55 70, 145 145, 100" fill="#fff"/>\
</svg>\
')
},
hideVideoPlayButton: function() {
video.play()
videoPlayButton.classList.add('is-hidden')
video.classList.remove('has-media-controls-hidden')
/*video.setAttribute('controls', 'controls')*/
video.attribute='controls'
}
}
videoMethods.renderVideoPlayButton()
这是CSS
.video-wrapper {
position: relative;
}
.video-wrapper > video {
width: 100%;
vertical-align: middle;
}
.video-wrapper > video.has-media-controls-hidden::-webkit-media-controls {
display: none;
}
.video-overlay-play-button {
box-sizing: border-box;
width: 100%;
height: 100%;
padding: 10px calc(50% - 50px);
position: absolute;
top: 0;
left: 0;
display: block;
opacity: 0.95;
cursor: pointer;
background-image: linear-gradient(transparent, #000);
transition: opacity 150ms;
}
.video-overlay-play-button:hover {
opacity: 1;
}
.video-overlay-play-button.is-hidden {
display: none;
}
解决方案
classList
由于 IE 不支持SVG 元素,它在 IE 中不起作用。您可以在此页面中找到兼容性信息。错误发生在这一行:videoPlayButton.classList.add('is-hidden')
。
你可以在你的代码中添加这个 polyfill,它会在 IE 中运行良好。
参考链接: 带有classList的代码在IE中不起作用?
推荐阅读
- javascript - 是否可以保存由 javascript 附加的单选按钮?
- zerobrane - Zerobrane:分形样本:zplane.lua 正在退出并出现错误
- javascript - NGRX | store.select(state => state.MyObject) | 返回未定义
- maven - 为什么 sonar-maven-plugin 不从 pom.xml 获取源目录
- python - urlopen.(url) 和 requests.get(url) 的区别
- r - 更改 R 中数据框列表中的列名子集
- symfony - 使用表单类型编辑对象
- c# - C# 控制台应用程序代码在等待后不执行
- r - 彩色绘图文本注释
- android - Play 商店安装的是即时应用,而非完整应用