在SVG元素上使用Javascript以创建鼠标事件
Using Javascript on SVG elements in order to create mouse events
我有一个SVG代码,看起来像这样:
<svg width="640" height="480" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">
<!-- Created with SVG-edit - http://svg-edit.googlecode.com/ -->
<g>
<title>Layer 1</title>
<rect id="svg_1" height="45" width="142" y="59.20001" x="59.39999" stroke-width="5" stroke="#000000" fill="#ffaaaa"/>
<rect id="svg_2" height="56" width="142" y="217.20001" x="97.39999" stroke-width="5" stroke="#000000" fill="#ff007f"/>
<rect id="svg_3" height="53" width="172" y="123.20001" x="360.39999" stroke-width="5" stroke="#000000" fill="#ff7f00"/>
</g>
</svg>
我想使用 Javascript 使所有单独的矩形在单击它们时略微移动。或者当我将光标悬停在它们上时显示某物的图片。我设法用CSS做到了,但我真的很有兴趣使用Javascript做这些事情,但我一无所知。谢谢!
这是一个普通的JavaScript起点。更新:整个代码中的注释解释了正在发生的事情。
var shapes = document.getElementsByTagName("rect"); // find all the shapes by making a collection of them
for (var i = 0; i < shapes.length; i += 1) { // tell the program what function (i.e. what handler)...
shapes[i].addEventListener('click', clickHandler); // ...to run every time each shape is clicked
}
function clickHandler(event) { // the function to run when any shape is clicked
// 'event' holds a lot of info, but critical here is 'which shape was clicked', i.e. event.target
var inc = 4, iter = 0; // set the anim'n speed and the count of anim'n steps
var jiggle = function() { // the function to perform on each animation cycle
iter += 1; // increment the anim'n step count
if (iter > 64) return; // when enough anim'n steps have run, do nothing more
event.target.x.baseVal.value += inc; // animate! : change the shape's left pos'n
if ((iter + 2) % 4 === 0) inc = -inc; // every 4 steps, change the move dir'n
requestAnimationFrame(jiggle); // at the end of every anim'n step, do it all again
};
requestAnimationFrame(jiggle); // get the first step of the anim'n going
};
<p>Click the shapes</p>
<svg width="640" height="480" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">
<!-- Created with SVG-edit - http://svg-edit.googlecode.com/ -->
<g>
<title>Layer 1</title>
<rect id="svg_1" height="45" width="142" y="59.20001" x="59.39999" stroke-width="5" stroke="#000000" fill="#ffaaaa"/>
<rect id="svg_2" height="56" width="142" y="217.20001" x="97.39999" stroke-width="5" stroke="#000000" fill="#ff007f"/>
<rect id="svg_3" height="53" width="172" y="123.20001" x="360.39999" stroke-width="5" stroke="#000000" fill="#ff7f00"/>
</g>
</svg>
相关文章:
- D3.js:如何在svg上移动鼠标时创建弹出事件
- 是否可以使用鼠标动态创建“html5画布”
- 可缩放容器鼠标上的可拖动元素在创建时从辅助对象上浮动
- 创建随机图像交换并禁止鼠标悬停事件
- 为Mozilla创建鼠标事件
- 正在创建一个事件以更改鼠标悬停时的图片
- 我可以在HTML5画布中创建图形形状对象吗?以及当用户将鼠标悬停在形状上时如何将光标显示为指针
- Jquery:当鼠标指针进入和离开元素时,创建一个弹出窗口
- 如何创建鼠标对 Div 的抵抗力,就像磁铁效应一样
- 为什么addEventListener只执行我的函数,而不为其创建鼠标悬停/鼠标打开事件
- 在SVG元素上使用Javascript以创建鼠标事件
- 我如何创建鼠标滚轮可缩放图像
- 如何创建鼠标悬停时显示消息的文本框
- 如何为HTML元素创建鼠标拖动滑块
- Javascript addEventListener -用于创建鼠标悬停效果
- 如何在html 5中创建鼠标悬停高亮框
- 为画布形状创建鼠标事件处理程序
- 使用Jquery创建鼠标悬停时滑动的标题
- Magento:如何在畅销书中创建鼠标悬停图像.phtml文件
- 如何从蓝色起源重新创建鼠标悬停效果