div选中效果及鼠标指针变形

这个功能,很多框架,如mxGraph都有,但是他们的集成太复杂,我自己做了一个效果。

先上效果图


第一个的效果。

我看到mxGraph是用的svg实现的,我现在需要用div实现。但是参考了一点mxGraph的思路。

1、选中目标时,添加几个小的div,我分别加的是四个边,四个角,四个中点,一共是12个div

//四条边
			active.append(createMyElement(width + borderWidth,borderWidth,- borderWidth/2,- borderWidth/2,"activeBorder"));
			active.append(createMyElement(width + borderWidth,borderWidth,- borderWidth/2,height- borderWidth/2,"activeBorder"));
			active.append(createMyElement(borderWidth,height + borderWidth,- borderWidth/2,- borderWidth/2,"activeBorder"));
			active.append(createMyElement(borderWidth,height + borderWidth,width - borderWidth/2,-borderWidth/2,"activeBorder"));
			
			
			//四个顶点
			active.append(createMyElement(pointWidth,pointWidth,- pointWidth/2,- pointWidth/2,"activeBorder"));
			active.append(createMyElement(pointWidth,pointWidth,- pointWidth/2,height- pointWidth/2,"activeBorder"));
			active.append(createMyElement(pointWidth,pointWidth,width - pointWidth/2,- pointWidth/2,"activeBorder"));
			active.append(createMyElement(pointWidth,pointWidth,width - pointWidth/2,height - pointWidth/2,"activeBorder"));
			
			//四个中点
			active.append(createMyElement(pointWidth,pointWidth,width/2 - pointWidth/2,- pointWidth/2,"activeBorder"));
			active.append(createMyElement(pointWidth,pointWidth,width/2 - pointWidth/2, height - pointWidth/2,"activeBorder"));
			active.append(createMyElement(pointWidth,pointWidth,- pointWidth/2,height/2 - pointWidth/2,"activeBorder"));
			active.append(createMyElement(pointWidth,pointWidth,width - pointWidth/2, height/2 - pointWidth/2,"activeBorder"));
其中,createMyElement()方法:

function createMyElement(width,height,left,top ,className){
			var element = document.createElement("div");
			element.style.width = width + "px";
			element.style.height= height+"px";
			element.style.top = top + "px";
			element.style.left = left + "px";
			if(className){
				element.className = className;
			}
			return element;
		}


2、鼠标移到对应的点上,鼠标的指针会进行变化

.active .activeBorder:nth-child(5){cursor:nw-resize;}
			.active .activeBorder:nth-child(6){cursor:sw-resize;}
			.active .activeBorder:nth-child(7){cursor:sw-resize;}
			.active .activeBorder:nth-child(8){cursor:nw-resize;}
			.active .activeBorder:nth-child(9){cursor:n-resize;}
			.active .activeBorder:nth-child(10){cursor:n-resize;}
			.active .activeBorder:nth-child(11){cursor:w-resize;}
			.active .activeBorder:nth-child(12){cursor:w-resize;}

核心就是这样。

源代码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值