Abd*_*auf 20 javascript raphael
我在我的网络应用程序中使用rapaeljs.我想设置对象的可丢弃边界.对象可以在可丢弃区域中移动.一旦物体进入可伸缩区域,对象就应该没有出路.
Pet*_*tai 63
拉斐尔已经建立了拖放支持.drag()
.在表单中使用它,element.drag(start, move, up);
其中3个参数是对您编写的函数的3个函数引用,分别处理mousedown,movement和mouseup事件.
注意如何this.ox
和this.oy
用于存储起始位置,dx
并dy
用于移动.
以下实现了对框的拖放.盒子总是可以移动,但一旦它在"监狱"盒子里,它就不能移回.当盒子进入监狱时,颜色立即改变,以通知用户功能已经改变.
这是通过实施Math.min()
和Math.max()
后箱的位置进行调整dx
,并dy
添加到当前位置:
var nowX, nowY, move = function (dx, dy) {
// move will be called with dx and dy
if (this.attr("y") > 60 || this.attr("x") > 60)
this.attr({x: this.ox + dx, y: this.oy + dy});
else {
nowX = Math.min(60, this.ox + dx);
nowY = Math.min(60, this.oy + dy);
nowX = Math.max(0, nowX);
nowY = Math.max(0, nowY);
this.attr({x: nowX, y: nowY });
if (this.attr("fill") != "#000") this.attr({fill: "#000"});
}
}
Run Code Online (Sandbox Code Playgroud)
您也可以这样做,以便一旦将其放入"监狱"框中,就不能再次拾取该框.这可以通过测试函数中的位置move()
或start()
函数中的使用来c.undrag(f)
完成stop()
.
window.onload = function() {
var nowX, nowY, R = Raphael("canvas", 500, 500),
c = R.rect(200, 200, 40, 40).attr({
fill: "hsb(.8, 1, 1)",
stroke: "none",
opacity: .5,
cursor: "move"
}),
j = R.rect(0,0,100,100),
// start, move, and up are the drag functions
start = function () {
// storing original coordinates
this.ox = this.attr("x");
this.oy = this.attr("y");
this.attr({opacity: 1});
if (this.attr("y") < 60 && this.attr("x") < 60)
this.attr({fill: "#000"});
},
move = function (dx, dy) {
// move will be called with dx and dy
if (this.attr("y") > 60 || this.attr("x") > 60)
this.attr({x: this.ox + dx, y: this.oy + dy});
else {
nowX = Math.min(60, this.ox + dx);
nowY = Math.min(60, this.oy + dy);
nowX = Math.max(0, nowX);
nowY = Math.max(0, nowY);
this.attr({x: nowX, y: nowY });
if (this.attr("fill") != "#000") this.attr({fill: "#000"});
}
},
up = function () {
// restoring state
this.attr({opacity: .5});
if (this.attr("y") < 60 && this.attr("x") < 60)
this.attr({fill: "#AEAEAE"});
};
// rstart and rmove are the resize functions;
c.drag(move, start, up);
};?
Run Code Online (Sandbox Code Playgroud)