我完成了没有jquery-ui的拖动事件。然后我必须使区域元素可以移动。它就像jquery-ui中的包含属性。如何制作受限区域。
(function ($) {
var elPosStartCounsel = 0;
var elPosCurrentCounsel = 0;
var elPosStartX = 0;
var elPosCurrentX = 0;
$.fn.drags = function (opt) {
opt = $.extend({
handle: "",
cursor: "pointer",
}, opt);
if (opt.handle === "") {
var $el = this;
} else {
var $el = this.find(opt.handle);
}
return $el.css('cursor', opt.cursor).on("mousedown", function (e) {
elPosStartCounsel = $(this).offset().top + $(this).offset().left;
elPosStartX = $(this).offset().top + $(this).offset().left;
if (opt.handle === "") {
var $drag = $(this).addClass('draggable');
} else {
var $drag = $(this).addClass('active-handle').parent().addClass('draggable');
}
var z_idx = $drag.css('z-index'),
drg_h = $drag.outerHeight(),
drg_w = $drag.outerWidth(),
pos_y = $drag.offset().top + drg_h - e.pageY,
pos_x = $drag.offset().left + drg_w - e.pageX;
$drag.css('z-index', 1000).parents().on("mousemove", function (e) {
$('.draggable').offset({
top: e.pageY + pos_y - drg_h,
left: e.pageX + pos_x - drg_w
}).on("mouseup", function () {
elPosCurrentCounsel = $(this).offset().top + $(this).offset().left;
elPosCurrentX = $(this).offset().top + $(this).offset().left;
$(this).removeClass('draggable').css('z-index', z_idx);
});
});
e.preventDefault(); // disable selection
}).on("mouseup", function (e) {
elPosCurrentCounsel = $(this).offset().top + $(this).offset().left;
elPosCurrentX = $(this).offset().top + $(this).offset().left;
if (opt.handle === "") {
$(this).removeClass('draggable');
} else {
$(this).removeClass('active-handle').parent().removeClass('draggable');
}
})
}
$('#linktalk').drags();
$('.counsel_btn').drags();
这是我的来源。