我正在尝试使用 SVG 为视频元素创建一个 UI。我正在寻找一个简单的解决方案来为控制栏设置动画,当鼠标在里面时我想从窗口底部升起(比如 YouTube)。
这是我想做的事情:
<svg xmlns="http://www.w3.org/2000/svg" height="100%" width="100%" viewBox="0 0 640 360" preserveAspectRatio="none" version="1.1">
<g id="control_bar" transform="translate(0,360)">
<animateTransform attributeName="transform" attributeType="XML" type="translate" begin="window.mouseover" from="0,360" to="0,328" dur="350ms" fill="freeze"/>
<animateTransform attributeName="transform" attributeType="XML" type="translate" begin="window.mouseout" from="0,328" to="0,360" dur="350ms" fill="freeze"/>
<rect x="0" y="0" width="640" height="32" fill="grey"/>
</g>
</svg>
不幸的是,window.mouseover 没有做任何事情。相反,我创建了一个透明矩形来覆盖整个窗口,并给它一个 id="screen" 并使用 begin="screen.mouseover" 等。果然当鼠标在窗口中时,控制栏的动画就像我想要的那样,不幸的是,屏幕阻止了它下面的所有元素获得自己的鼠标事件。
我正在寻找最简单的方法来实现这一点,最好只使用标记(SMIL),因为我想避免使用大量的 JavaScript 或库。
谢谢!
>>>编辑<<<澄清我所追求的是什么:
我想为 HTML5 <video> 元素创建一个自定义 SVG UI。我的第一种方法是使用 document.createElementNS 将 SVG 动态插入到 DOM 中,但这很快就变得一团糟。接下来我尝试了拉斐尔,但这只会让它稍微不那么混乱。我决定希望 UI 成为一个自包含的文档,因此我决定为 UI 创建一个 SVG 文档,然后创建一个 <object> 元素,该元素将加载它并覆盖在 <video> 元素的顶部。我的问题是,只要鼠标在 UI 窗口内,我就无法让控制栏设置动画然后保持原位。此外,从父文档与 UI 通信也变得很痛苦。
我目前的解决方案:
我在我的 HTML 文档中放置了一个 <video> 元素,如下所示:
<video width="640" src="myvideo.webm"/>
然后我使用以下JavaScipt(带有jquery):
$(function(){
$("video").each(function(){
var old_video = $(this);
var width = old_video.attr("width")
var height = Math.floor(width / (16 / 9))
var video = $("<object/>")
video.addClass("video")
video.css({
width: width,
height: height,
})
var src = old_video.attr("src")
video.attr("data", "video.xhtml#" + src)
old_video.replaceWith(video)
})
})
这会将视频元素替换为 <object>,其数据 uri 设置为:video.xhtml#myvideo.webm
那么video.xhtml的内容是这样的:
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<script src="jquery.js"/>
<script>
$(function(){
$(window).hover(function(){
$("#in")[0].beginElement()
}, function(){
$("#out")[0].beginElement()
})
var video = document.createElement("video")
video.setAttribute("src", location.hash.substr(1))
$("div#video").replaceWith(video)
})
</script>
<style>
svg {
position: absolute;
top: 0; left: 0;
}
video {
position: absolute;
top: 0; left: 0;
width: 100%;
height: 100%;
background: black;
}
</style>
</head>
<body>
<div id="video"/>
<svg xmlns="http://www.w3.org/2000/svg" height="100%" width="100%" viewBox="0 0 640 360" preserveAspectRatio="none" version="1.1">
<g id="bar" transform="translate(0,360)">
<animateTransform id="in" attributeName="transform" attributeType="XML" type="translate" begin="indefinite" from="0,360" to="0,328" dur="50ms" fill="freeze"/>
<animateTransform id="out" attributeName="transform" attributeType="XML" type="translate" begin="indefinite" from="0,328" to="0,360" dur="350ms" fill="freeze"/>
<rect x="0" y="0" width="640" height="32" fill="grey"/>
<rect onclick="$('video')[0].play()" x="0" y="0" width="64" height="32" fill="blue">
<set id="btn" attributeName="fill" to="red" begin="mousedown" end="mouseup;mouseout" dur="1s" fill="remove" />
</rect>
</g>
</svg>
</body>
</html>
此文档从哈希中获取视频 uri,并在 SVG UI 后面注入一个视频元素。由于它是一个 XHTML 文档(而不是 SVG),我现在可以使用 jquery 来处理鼠标事件,这并不理想,但它似乎可以工作。唯一的问题是我收到一个 JavaScript 错误:a.compareDocumentPosition 不是函数源文件:jquery.js 行: Firefox 中的 17。
这种方法有意义吗?有没有更好的办法?我也更喜欢仅使用 SMIL 而不是 JavaScript/jQuery 的动画解决方案。
谢谢!