bsr*_*bsr 14 jquery html5 svg drag-and-drop
我想跟随HTML5拖放教程这里.我无法dragstart
在rect
元素上注册该事件.如果我将事件更改为draggable
,mousedown
则调用handleDragStart
处理程序.请忽略代码中的其他空白注册.
JSFiddle 在这里
<!DOCTYPE html>
<html><head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<style type="text/css" media="screen">
svg rect { cursor: move; }
</style>
</head><body>
<h1>SVG/HTML 5 Example</h1>
<svg id="cvs">
<rect draggable="true" x="0" y="10" width="100" height="80" fill="#69c" />
<rect x="50" y="50" width="90" height="50" fill="#c66" />
</svg>
<script type="text/javascript" src="loc.js"></script>
</body></html>
Run Code Online (Sandbox Code Playgroud)
loc.js
$(document).ready(function() {
function handleDragStart(e) {
log("handleDragStart");
this.style.opacity = '0.4'; // this ==> e.target is the source node.
};
var registercb = function () {
$("#cvs > rect").each(function() {
$(this).attr('draggable', 'true');
});
$("#cvs > rect").bind('dragstart', handleDragStart);
$(window).mousedown(function (e) {
});
$(window).mousemove(function (e) {
});
$(window).mouseup(function (e) {
log("mouseup");
});
};
function log() {
if (window.console && window.console.log)
window.console.log('[XXX] ' + Array.prototype.join.call(arguments, ' '));
};
registercb();
});
Run Code Online (Sandbox Code Playgroud)
Alv*_*oro 11
我知道这是一个老问题,我从这个被标记为这个问题的副本的问题到达这里,并希望添加一个不需要jQuery或任何库的可能解决方案,并且适用于所有主流浏览器.它基于@AmirHossein Mehrvarzi推荐的本教程.
这个小解决方案不使用拖动事件,只是mousedown
,mouseup
和mousemove
.这是它的工作原理:
从上面问题中的代码:
var selectedElement = null;
var currentX = 0;
var currentY = 0;
$(document).ready(function() {
function handleDragStart(e) {
log("handleDragStart");
this.style.opacity = '0.4'; // this ==> e.target is the source node.
};
var registercb = function () {
$("#cvs > rect").mousedown(function (e) {
// save the original values when the user clicks on the element
currentX = e.clientX;
currentY = e.clientY;
selectedElement = e.target;
}).mousemove(function (e) {
// if there is an active element, move it around by updating its coordinates
if (selectedElement) {
var dx = parseInt(selectedElement.getAttribute("x")) + e.clientX - currentX;
var dy = parseInt(selectedElement.getAttribute("y")) + e.clientY - currentY;
currentX = e.clientX;
currentY = e.clientY;
selectedElement.setAttribute("x", dx);
selectedElement.setAttribute("y", dy);
}
}).mouseup(function (e) {
// deactivate element when the mouse is up
selectedElement = null;
});
};
function log() {
if (window.console && window.console.log)
window.console.log('[XXX] ' + Array.prototype.join.call(arguments, ' '));
};
registercb();
});
Run Code Online (Sandbox Code Playgroud)
rect { cursor: move; }
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h1>SVG/HTML 5 Example</h1>
<svg id="cvs">
<rect x="0" y="10" width="100" height="80" fill="#69c" />
<rect x="50" y="50" width="90" height="50" fill="#c66" />
</svg>
Run Code Online (Sandbox Code Playgroud)
你也可以在这个JSFiddle上看到它:http://jsfiddle.net/YNReB/61/
如果要添加删除功能,可以修改mouseup
函数以读取光标位置上的元素(with document.elementFromPoint(e.clientX, e.clientY)
),然后可以对原始元素和删除它的元素执行操作.
归档时间: |
|
查看次数: |
20696 次 |
最近记录: |