svg鼠标响应事件的四种方法

查看完整版本 : svg鼠标响应事件的四种方法
鼠标响应事件的四种方法,以click事件为例。

Mouse Events - SMIL

xml 代码
  1. xml version="1.0" encoding="ISO-8859-1" standalone="no"?>  
  2. "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">  
  3. <svg xmlns="http://www.w3.org/2000/svg"  
  4. xmlns:xlink="http://www.w3.org/1999/xlink">  
  5. <rect x="5" y="5" width="40" height="40" fill="red">  
  6. <set attributeName="fill" to="blue" begin="click"/>  
  7. rect>  
  8. svg>  



实例演示:http://www.kevlindev.com/tutorials/basics/events/mouse/svg_smil/click.svg

Mouse Events - Attributes
xml 代码
  1.   
  2. xml version="1.0" encoding="ISO-8859-1" standalone="no"?>  
  3. "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" [   
  4. xmlns:a3 CDATA #IMPLIED   
  5. a3:scriptImplementation CDATA #IMPLIED>  
  6. a3:scriptImplementation CDATA #IMPLIED>  
  7. ]>  
  8. <svg xmlns="http://www.w3.org/2000/svg"  
  9. xmlns:xlink="http://www.w3.org/1999/xlink"  
  10. xmlns:a3="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"  
  11. a3:scriptImplementation="Adobe">  
  12. <script type="text/ecmascript" a3:scriptImplementation="Adobe"> 
  13. function changeColor(evt) {  
  14. var rect = evt.target;  
  15.  
  16. rect.setAttributeNS(null, "fill", "purple")  
  17. }  
  18. ]]>script>  
  19. <rect x="5" y="5" width="40" height="40" fill="red"  
  20. onclick="changeColor(evt)"/>  
  21. svg>  
  22.   
实例演示:

http://www.kevlindev.com/tutorials/basics/events/mouse/svg_js/onclick.svg

Mouse Events - JavaScript+SMIL

xml 代码
  1. xml version="1.0" encoding="ISO-8859-1" standalone="no"?>  
  2. "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" [   
  3. xmlns:a3 CDATA #IMPLIED   
  4. a3:scriptImplementation CDATA #IMPLIED>  
  5. a3:scriptImplementation CDATA #IMPLIED>  
  6. ]>  
  7. <svg onload="makeShape(evt)"  
  8. xmlns="http://www.w3.org/2000/svg"  
  9. xmlns:xlink="http://www.w3.org/1999/xlink"  
  10. xmlns:a3="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"  
  11. a3:scriptImplementation="Adobe">  
  12. <script type="text/ecmascript" a3:scriptImplementation="Adobe"> 
  13. var svgns = "http://www.w3.org/2000/svg";  
  14. function makeShape(evt) {  
  15. if ( window.svgDocument == null )  
  16. svgDocument = evt.target.ownerDocument;  
  17.  
  18. var rect = svgDocument.createElementNS(svgns, "rect");  
  19. rect.setAttributeNS(null, "x", "5");  
  20. rect.setAttributeNS(null, "y", "5");  
  21. rect.setAttributeNS(null, "width", "40");  
  22. rect.setAttributeNS(null, "height", "40");  
  23. rect.setAttributeNS(null, "fill", "green");  
  24.  
  25. var set = svgDocument.createElementNS(svgns, "set");  
  26. set.setAttributeNS(null, "attributeName", "fill");  
  27. set.setAttributeNS(null, "to", "blue");  
  28. set.setAttributeNS(null, "begin", "click");  
  29.  
  30. rect.appendChild(set);  
  31. svgDocument.documentElement.appendChild(rect);  
  32. }  
  33. ]]>script>  
  34. svg>  

实例演示:http://www.kevlindev.com/tutorials/basics/events/mouse/js_dom_smil/click_js_smil.svg

Mouse Events - EventListener

xml 代码
  1. xml version="1.0" encoding="ISO-8859-1" standalone="no"?>  
  2. "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" [   
  3. xmlns:a3 CDATA #IMPLIED   
  4. a3:scriptImplementation CDATA #IMPLIED>  
  5. a3:scriptImplementation CDATA #IMPLIED>  
  6. ]>  
  7. <svg onload="makeShape(evt)"  
  8. xmlns="http://www.w3.org/2000/svg"  
  9. xmlns:xlink="http://www.w3.org/1999/xlink"  
  10. xmlns:a3="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"  
  11. a3:scriptImplementation="Adobe">  
  12. <script type="text/ecmascript" a3:scriptImplementation="Adobe"> 
  13. var svgns = "http://www.w3.org/2000/svg";  
  14. function makeShape(evt) {  
  15. if ( window.svgDocument == null )  
  16. svgDocument = evt.target.ownerDocument;  
  17.  
  18. var rect = svgDocument.createElementNS(svgns, "rect");  
  19. rect.setAttributeNS(null, "x", 5);  
  20. rect.setAttributeNS(null, "y", 5);  
  21. rect.setAttributeNS(null, "width", 40);  
  22. rect.setAttributeNS(null, "height", 40);  
  23. rect.setAttributeNS(null, "fill", "green");  
  24.  
  25. rect.addEventListener("click", changeColor, false);  
  26.  
  27. svgDocument.documentElement.appendChild(rect);  
  28. }  
  29.  
  30. function changeColor(evt) {  
  31. var target = evt.target;  
  32. target.setAttributeNS(null, "fill", "purple");  
  33. }  
  34. ]]>script>  
  35. svg>  

实例演示:http://www.kevlindev.com/tutorials/basics/events/mouse/js_dom/click_js.svg
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想使用Vue 3来实现SVG图元的鼠标滚轮控制尺寸,并将初始尺寸设置为1,可以按照以下步骤进行操作: 1. 在Vue项目中安装`vue-svg-inline-loader`库,该库用于加载SVG文件并以内联方式显示。 ```bash npm install vue-svg-inline-loader ``` 2. 创建一个Vue组件,命名为`SvgElement.vue`,并在该组件中添加SVG图元。 ```html <template> <div> <svg ref="svgElement" v-html="svgCode" @wheel="handleWheel"></svg> </div> </template> <script> export default { data() { return { svgCode: '<circle cx="50" cy="50" r="50" fill="red"></circle>', size: 1 }; }, methods: { handleWheel(event) { event.preventDefault(); const delta = Math.sign(event.deltaY); if (delta > 0) { this.size += 0.1; } else { this.size -= 0.1; if (this.size < 0.1) { this.size = 0.1; } } this.updateSvgSize(); }, updateSvgSize() { const svgElement = this.$refs.svgElement; svgElement.style.width = `${this.size}rem`; svgElement.style.height = `${this.size}rem`; } }, mounted() { this.updateSvgSize(); } }; </script> <style scoped> svg { transition: width 0.2s, height 0.2s; } </style> ``` 3. 在你的Vue页面中使用`SvgElement`组件,并传递相应的SVG代码。 ```html <template> <div> <svg-element></svg-element> </div> </template> <script> import SvgElement from "@/components/SvgElement.vue"; export default { components: { SvgElement } }; </script> ``` 这样,当你在SVG元素上滚动鼠标滚轮时,尺寸会相应地增加或减小。初始尺寸为1,并且使用了Vue的响应式数据来实时更新SVG图元的大小。请确保已经安装了相关依赖并正确配置了Vue项目。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值