主要用来实现下图的效果,通过鼠标左键按住框选元素,将框选的元素返回 很少用js写前端了,最近项目需要,用的Js来实现功能,有不足的地方还请帮忙指出
function onSelectPluginClone() {
let x1, y1, x2, y2;
let $content = $("#content");
$content.on("mousedown", function (e) {
if (window.pageStatus !== "selectable") {
return;
}
x1 = e.clientX;
y1 = e.clientY + $("#rightspan")[0].scrollTop;
$content.append(`<div id="area" style="display: none;position: absolute;z-index: 9999999;border: 2px solid blue;"></div>`);
e.preventDefault();
});
$content.on("mousemove", function (e) {
if (window.pageStatus !== "selectable" || !x1 || !y1) {
return;
}
if (e.clientY >= window.screen.height - 50) {
$("#rightspan")[0].scrollTop += (e.clientY - window.screen.height + 75)
}
let width = Math.abs(e.clientX - x1),
height = Math.abs(e.clientY + $("#rightspan")[0].scrollTop - y1);
let top = Math.min(e.clientY + $("#rightspan")[0].scrollTop, y1),
left = Math.min(e.clientX, x1);
let $area = $("#area");
let parentTop = parseFloat($("#rightspan").css("top"));
let parentLeft = parseFloat($("#rightspan").css("left"));
top -= parentTop;
left -= parentLeft
$area.css("width", width + "px");
$area.css("height", height + "px");
$area.css("left", left + "px");
$area.css("top", top + "px");
$area.show();
});
$content.on("mouseup", function (e) {
if (window.pageStatus !== "selectable" || !x1 || !y1) {
return;
}
if (e.clientX > x1) {
x2 = e.clientX;
} else {
x2 = x1;
x1 = e.clientX;
}
if (e.clientY + $("#rightspan")[0].scrollTop > y1) {
y2 = e.clientY + $("#rightspan")[0].scrollTop;
} else {
y2 = y1;
y1 = e.clientY + $("#rightspan")[0].scrollTop;
}
handleMouseUp();
});
function handleMouseUp() {
let plugins = Array.from($content.find(".ui-plugin"));
let rightSpan = $("#rightspan")[0];
let selectedPlugins = null
selectedPlugins = plugins.filter(plugin => {
let left = offset(plugin, "left") - rightSpan.scrollLeft,
top = offset(plugin, "top");
let width = plugin.offsetWidth,
height = plugin.offsetHeight;
return left >= x1 && top >= y1 && (left + width) <= x2 && (top + height) <= y2;
});
x1 = 0;
y1 = 0;
x2 = 0;
y2 = 0;
$("#area").remove();
handleSelectedPlugins(selectedPlugins);
$(".content").removeClass("selectable");
$("#page-container").removeClass("top");
window.pageStatus = "edit";
}
function handleSelectedPlugins(plugins) {
if (plugins.length < 1) {
return
}
}
function offset(ele, leftOrTop) {
let offsetDir = "offset" + leftOrTop[0].toUpperCase() + leftOrTop.substring(1);
let realValue = ele[offsetDir];
let parent = ele.offsetParent;
while (parent !== null) {
realValue += parent[offsetDir];
parent = parent.offsetParent;
}
return realValue;
}
}
|