Mouse monitoring to achieve picture animation
This is a small animation, monitoring the movement of the mouse, to achieve the image of the mobile visual effects. Although the function is not so powerful, but the scope of application is very broad, not grandiose and not monotonous.
Let's enjoy the style first.
Small Look
Code:
<!doctype html>"Utf-8"> <title> mouse Move pictures </title> <style>body {margin:0; Background-image:url (img/beijing.jpg); } #wrap {width: -%; height:800px; margin:30px Auto; position:relative; } #wrap img {position:absolute; } #wrap img:nth-of-type (1) {width:200px; height:300px; left:300px; top:30px; Z-index:0; } #wrap img:nth-of-type (2) {width:150px; height:200px; left:200px; top:300px; Z-index:1; } #wrap img:nth-of-type (3) {width:150px; height:190px; right:100px; top:50px; Z-index:2; } #wrap img:nth-of-type (4) {width:200px; height:200px; left:400px; top:30px; Z-index:3; } #wrap img:nth-of-type (5) {width:200px; height:200px; right:500px; top:70px; Z-index:4; } #wrap img:nth-of-type (6) {width:200px; height:200px; right:200px; top:200px; Z-index:5; } #wrap img:nth-of-type (7) {width:400px; height:400px; Right: *%; top:200px; Z-index:6; } </style> "Wrap"> "img/Image (2). PNG"/> "img/Image (3). PNG"/> "Img/dada_man_ray_skulptur_cadeau_01.gif"/> "img/Image (5). PNG"/> "img/Image (6). PNG"/> "img/capture_decran_2016_03_16_a_151542.jpg"/> "img/Image (8). PNG"/> </div> <script>varOwrap = document.getElementById ("Wrap"); varAimg = Owrap.getelementsbytagname ("img"); varIMax =7; //get the initial position of the picture for(vari =0; i < aimg.length; i++) {Aimg[i].startx= parseint (GetStyle (aimg[i),' Left'))} Owrap.onmousemove=function (EV) {EV= EV | | Window.Event; //gets the distance from the position of the mouse to the center point of the Div varYd = Ev.clientx-(Owrap.offsetleft + This. offsetwidth/5) for(vari =0; i < aimg.length; i++) { //get Z-index for each img varIzindex = GetStyle (Aimg[i],'ZIndex') //the larger the zindex, the smaller the relative distance of the movement. varIDISL =-parseint (Yd/imax * (imax-izindex)/ -) //The distance of the picture is equal to the original distance plus the calculated distanceAimg[i].style.left = aimg[i].startx + IDISL +'px'}} function GetStyle (obj, attr) {if(Obj.currentstyle) {
//ie Browser returnObj.currentstyle[attr]; } returngetComputedStyle (obj) [attr]; } </script> </body>
After copying the code, remember to change a piece, I set the monitor #wrap range of the mouse, just listen to the x-axis, you can study the addition of the y-axis of the monitoring movement.
finally
If there is a better way, we look forward to sharing, learning together and making progress together.
No exaggeration and no monotony--monitor mouse picture motion animation