這裏是一個可能的解決方案:
在你的SVG結束(以這種方式,將在所有元素繪製)添加一個矩形這樣
<rect id="zoom_area" x=0 y=0 width=0 height=0 onmouseup="endDrag(evt)" style="fill: white; stroke:black; stroke-width:2px; opacity:0.5"/>
在您的網格中添加事件onmouseup="endDrag(evt)"
和onmousemove="moveMouse(evt)"
現在的javascript:
var zoom_box = {};
function startDrag(evt){
var offset = $("#bounds_grid").offset(); // Take the offset from the grid, change the ID as you need.
evt.stopPropagation();
zoom_box["start_x"] = evt.clientX-offset.left;
zoom_box["start_y"] = evt.clientY-offset.top;
zoom_box["boxing"] = true;
$('#zoom_area').attr("x",zoom_box["start_x"]);
$('#zoom_area').attr("y",zoom_box["start_y"]);
}
function endDrag(evt){
var offset = $("#bounds_grid").offset();
evt.stopPropagation();
zoom_box["end_x"] = evt.clientX-offset.left;
zoom_box["end_y"] = evt.clientY-offset.top;
zoom_box["boxing"] = false;
$('#zoom_area').attr("width",0);
$('#zoom_area').attr("height",0);
}
function moveMouse(evt){
var offset = $("#bounds_grid").offset();
evt.stopPropagation();
if(zoom_box["boxing"]){
zoom_box["end_x"] = evt.clientX-offset.left;
zoom_box["end_y"] = evt.clientY-offset.top;
$('#zoom_area').attr("width",(zoom_box["end_x"]-zoom_box["start_x"]));
$('#zoom_area').attr("height",(zoom_box["end_y"]-zoom_box["start_y"]));
}
}
要當心了失調:在這種方式,採取從文檔的頁邊距TE偏移