To determine the direction of mouse movement, mainly according to the mouse to move into the slope K, when-k<k1<k, from left and right to move in, x>x0 to the left to move in, x<x0 to move in, otherwise from up and down, y>y0 from the bottom of the move in, y>y0 from the top.
if (-k<k1 && k1<k) { if(Pointx > x0) { = "right"; } Else { = "Left" }}else{ if(Pointy > y0) { = "Bottom"; } Else { = "Top"; }}
Html:
<id= "box"></div>
Js:
varT = $ ("#box"). Offset (). Top,//TopL = $ ("#box"). Offset (). Left,// LeftW = $ ("#box"). Width (),//widthH = $ ("#box"). Height (),//Heightx0 = l + W/2, y0= t + H/2, y1= T,x1 =L, Y2= t + h,x2 = l +W, K= (y2-y0)/(X2-x0), k1,k2,dir,pointx,pointy,outx,outy; Console.log (k); $("#box"). Append (' <div class= ' mask ></div> ') $("#box"). MouseEnter (function(e) {Pointx=E.pagex; Pointy=E.pagey; K1= (pointy-y0)/(Pointx-x0); Console.log (K1); if(-k<k1 && k1<k) { if(Pointx >x0) {dir= "Right"; }Else{dir= "Left" } }Else{ if(Pointy >y0) {dir= "Bottom"; }Else{dir= "Top"; } } Switch(dir) { Case"Top": Console.log ("Top"); $( This). Find (". Mask"). css ({left:0,top:-h+ "px")}); Break; Case"Right": Console.log ("Right"); $( This). Find (". Mask"). css ({top:0,left:w+ "px")}); Break; Case"Bottom": Console.log ("Bottom"); $( This). Find (". Mask"). css ({left:0,top:h+ "px")}); Break; Case"Left": Console.log ("Left"); $( This). Find (". Mask"). css ({top:0,left:-w+ "px")}); Break; default: Break; } $( This). Find (". Mask"). Stop (false,true). Animate ({top:0,left:0},1000); }). MouseLeave (function(e) {OutX=E.pagex; Outy=E.pagey; K2= (outy-y0)/(OutX-x0); if(-k<k2 && k2<k) { if(OutX >x0) {dir= "Right"; }Else{dir= "Left" } }Else{ if(Outy >y0) {dir= "Bottom"; }Else{dir= "Top"; } } Switch(dir) { Case"Top": Console.log ("Top"); $( This). Find (". Mask"). Animate ({top:-h+ "px", left:0},1000); Break; Case"Right": Console.log ("Right"); $( This). Find (". Mask"). Animate ({top:0,left:w+ "px"},1000); Break; Case"Bottom": Console.log ("Bottom"); $( This). Find (". Mask"). Animate ({top:h+ "px", left:0},1000); Break; Case"Left": Console.log ("Left"); $( This). Find (". Mask"). Animate ({top:0,left:-w+ "px"},1000); Break; default: Break; } });
Browse effects
A stop event is used on top, and stop is the method used in jquery to control the effect of page animations. Ends the animation effect on the current page immediately after running.
Stop adds 2 parameters to the new jquery:
The first parameter means whether to empty the animation sequence, that is, stop is the animation effect of the current element or stop the accompanying animation effect, generally false, skip the current animation effect, perform the next animation effect;
The second parameter is whether the current animation effect is executed to the end, meaning that the animation effect is just executed when the current animation is stopped, and this is true when you want the effect after the animation is executed. Otherwise, the animation will stop when the stop is executed.
Mask layer that follows the mouse movement