javascript – 在svg中获取鼠标位置
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 在svg中获取鼠标位置,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2954字,纯文字阅读大概需要5分钟。
内容图文
![javascript – 在svg中获取鼠标位置](/upload/InfoBanner/zyjiaocheng/749/35d887d35ca9462ba5df8dc6ee50aefd.jpg)
我有一个像我这样的hv加载的svg(这是我的地图)
<div id="map1">
<object id="map" onm ousedown="on_mouse_move(onmousemove)" type="image/svg+xml" data="map.svg" style="width: 1400px; height: 700px; border:1px solid black; ">Your browser does not support SVG
</object>
</div>
当我点击并将鼠标移到我的地图上时,我想知道鼠标的位置.
当我点击并移动地图时,我想要移动一个圆圈.
到目前为止,这是我的代码
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="svg-pan-zoom.js"></script>
<script src="control-icons.js"></script>
<script src="raphael-min.js"></script>
<script>
// Don't use window.onLoad like this in production, because it can only listen to one function.
// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0
// Main function to retrieve mouse x-y pos.s
function on_mouse_move(evt) {
var
tempX = evt.clientX,
tempY = evt.clientY;
alert("hi")
}
window.onload = function () {
createDevice(tempX, tempY, "computer", "good");
createDevice(30, 30, "phone", "good");
createDevice(30, 10, "tablet", "bad");
};
//x and y are the coordinates for the posistion od the device.
//Make sure that the device is lowercase.
function createDevice(x,y,device,status) {
svgPanZoom.init('#Map', {
'zoomEnabled': true,
'controlIconsEnabled': true,
'setMinZoom': 100,
'setMaxZoom': 100,
'center': true,
'fit': true
});
// Creates canvas 320 × 200 at 10, 50
var paper = Raphael(x, y, 320, 200);
// Creates circle at x = 50, y = 40, with radius 10
var circle = paper.circle(50, 40, 10);
if (device == "computer") {
circle.attr("fill", "#0000FF");
}
if (device == "phone") {
circle.attr("fill", "#00FF00");
}
if (device == "tablet") {
circle.attr("fill", "#FF00FF");
}
if (status == "good") {
// Sets the stroke attribute of the circle to white
circle.attr("stroke", "green");
}
if (status == "bad") {
// Sets the stroke attribute of the circle to white
circle.attr("stroke", "orange");
}
if (status == "dead") {
// Sets the stroke attribute of the circle to white
circle.attr("stroke", "red");
}
}
</script>
</head>
<body>
<div id="map1">
<object id="map" onm ousedown="on_mouse_move(onmousemove)" type="image/svg+xml" data="map.svg" style="width: 1400px; height: 700px; border:1px solid black; ">Your browser does not support SVG</object>
</div>
</body>
</html
>
解决方法:
您应该在对象顶部使用div来捕获事件.在这里,看看这个小提琴,它不是你需要的正确,但显示如何做叠加.
HTML
<h1 id="coord"></h1>
<div id="map-container">
<object id="map-svg" type="image/svg+xml" data="http://upload.wikimedia.org/wikipedia/en/9/9a/Nickelodeon_logo.svg" style="width: 300px; height: 300px; border:1px solid black; ">Your browser does not support SVG</object>
<div id="map-catcher"></div>
</div>
JS
$(document).ready(function () {
$("#map-catcher").mousedown(function (e) {
$("#coord").text("x:"+e.offsetX+", y:"+e.offsetY);
});
});
CSS
#map-container{
position:relative;
width:300px;
height:300px;
}
#map-svg{
position:absolute;
left:0px;
top:0px;
}
#map-catcher{
position:absolute;
left:0px;
top:0px;
height:100%;
width:100%;
}
内容总结
以上是互联网集市为您收集整理的javascript – 在svg中获取鼠标位置全部内容,希望文章能够帮你解决javascript – 在svg中获取鼠标位置所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。