2
http://jsbin.com/AyaDOVO/3/edit啓用/禁用繪製對象的DIV
我一直在嘗試這種繪製格功能,並且我有麻煩隔離它,所以它是在點擊抽獎按鈕時,當啓動按鈕只叫和殘疾人點擊。
這是我的JQuery/Javascript。
$(document).ready(function() {
var drawable = false;
$('.div-tool').click(function() {
drawable = true;
});
$('.select-tool').click(function() {
drawable = false;
});
function draw_a_box(e) {
var pageX = e.pageX,
pageY = e.pageY,
dpLast = dp.find('.drawnBox.last'),
dpLast_data = dpLast.data();
$('.drawnBox').css({
'position': 'absolute',
'z-index': '1',
'border': '1px solid #222',
'background': '#e1e1e1',
'opacity': '0.5'
});
if (e.type === 'mousemove') {
// If ".drawnBox.last" doesn't exist, create it.
if (dpLast.length < 1) {
$('<div class="drawnBox last"></div>').appendTo(dp);
}
var drawCSS = {};
// If drawing is initiated.
if (draw) {
// Determine the direction.
// xLeft
if (dpLast_data.pageX > pageX) {
drawCSS.right = dp.width() - dpLast_data.pageX,
drawCSS.left = 'auto',
drawCSS.width = dpLast_data.pageX - pageX;
}
// xRight
else if (dpLast_data.pageX < pageX) {
drawCSS.left = dpLast_data.pageX,
drawCSS.right = 'auto',
drawCSS.width = pageX - dpLast_data.pageX;
}
// yUp
if (dpLast_data.pageY > pageY) {
drawCSS.bottom = dp.height() - dpLast_data.pageY,
drawCSS.top = 'auto',
drawCSS.height = dpLast_data.pageY - pageY;
}
// yDown
else if (dpLast_data.pageY < pageY) {
drawCSS.top = dpLast_data.pageY,
drawCSS.bottom = 'auto',
drawCSS.height = pageY - dpLast_data.pageY;
}
}
if (!draw && dpLast.length > 0) {
dpLast.css({
top: pageY,
left: pageX
});
}
if (draw) {
dpLast.css(drawCSS);
}
}
if (e.type === 'mousedown') {
e.preventDefault();
draw = true;
dpLast.data({ "pageX": pageX, "pageY": pageY });
}
else if (e.type === 'mouseup') {
draw = false;
dpLast.removeClass('last');
}
}
if (drawable) {
var dp = $('#drawingArea'),
draw = false,
enabled = true;
dp
.css({ position: 'relative' })
.on("mousemove mousedown mouseup", draw_a_box);
}
});
'drawable^= 1;'如何切換點擊事件? –