同遮盖层划入一样,单纯的遮盖层划出的话算的上是非常简单了,但是在这里它却就不这么简单了,而且我前面还选了个比较麻烦的生成div,而不是显示存在的div,这里有那么几点需要特别注意:
1、遮盖层出现后,鼠标哪怕不动,也已经是在遮盖层上,已经不再给出的div区域了,所以注意监听的位置;
2、onmouseout和onmouseover都是瞬时触发的,这点很重要;
3、在实际应用中,已存在的div的显示比临时创建肯定要有效的多;
这样我还是上一下代码吧,其实之前的地方没怎么变,我只记录改变的地方,那就是onmouseout监听加在了哪呢?
var getonediv=function(){ var div=document.createelement(div); div.style.position=absolute; div.style.display=block; div.style.zindex=10; div.style.background=yellow; div.addeventlistener(mouseout,function(event){//我把它加在了这里,而这里监听的判断与之前的划入几乎如出一辙 var x=event.clientx; var y=event.clienty; left=x-test.offsetleft; top=y-test.offsettop; right=test.offsetleft+test.offsetwidth-x; bottom=test.offsettop+test.offsetheight-y; arr=[]; arr.push(top); arr.push(right); arr.push(bottom); arr.push(left); var least=findleast(arr); if(least==1){ } if(least==2){//还是距离和宽度的同时改变啊 div.style.left=test.offsetleft+px; div.style.top=test.offsettop+px; div.style.height=test.offsetheight+px; div.style.width=width+px; var changewidth2=setinterval(function(){ if(div.offsetleft>=test.offsetleft+test.offsetwidth){ clearinterval(changewidth2); check=true;//关键点 }else{ marginleft=marginleft+10; width=width-10; div.style.width=width+px; div.style.left=marginleft+px; } },30); } if(least==3){ } if(least==4){//向左划出,width作为全局变量,这次就是不断减小了 div.style.left=test.offsetleft+px; div.style.top=test.offsettop+px; div.style.height=test.offsetheight+px; div.style.width=width+px; var changewidth1=setinterval(function(){ if(div.offsetwidth<=0){ clearinterval(changewidth1); check=true;//这里也比较关键哦 }else{ width=width-10; div.style.width=width+px; } },30); } }) return div; }
就这样简单的都实现了划出划入的效果,简单的看的话确实已经有其形了,但是不得不说,这是个拙劣到了极点的实现,还有多少点是还没有加入的,还有多少情况是还没有考虑到的,另外,这代码的重复编写,优化优化,喏喏...