放大镜制作
< div class = " box" id = " box" > < div class = " small" > < img src = " images/big.jpg" width = " 350" class = " aaa" alt = " " /> < div class = " mask" > </ div> </ div> < div class = " big" > < img src = " images/big.jpg" width = " 800" alt = " " /> </ div>
</ div>
* { margin : 0; padding : 0; } .box { width : 350px; height : 350px; margin : 100px; border : 5px solid black; position : relative; } .big { width : 400px; height : 400px; position : absolute; top : 0; left : 360px; border : 1px solid #ccc; overflow : hidden; display : none; } .mask { width : 175px; height : 175px; background : rgba ( 255, 255, 0, 0.4) ; position : absolute; top : 0px; left : 0px; cursor : move; display : none; } .small { position : relative; } .aaa { vertical-align : middle; } .big { position : relative; top : -350px; } .big img { position : absolute; }
< script> var box = document. getElementById ( "box" ) ; var smallBox = box. children[ 0 ] ; var mask = smallBox. children[ 1 ] ; var bigBox = box. children[ 1 ] ; var bigImg = bigBox. children[ 0 ] ; box. onmouseover = function ( ) { mask. style. display = "block" ; bigBox. style. display = "block" ; } ; box. onmouseout = function ( ) { mask. style. display = "none" ; bigBox. style. display = "none" ; } ; smallBox. onmousemove = function ( e) { var e = e || window. event; var x = e. clientX - box. offsetLeft - mask. offsetWidth / 2 ; var y = e. clientY - box. offsetTop - mask. offsetHeight / 2 ; x = x < 0 ? 0 : x; y = y < 0 ? 0 : y; var maxLeft = smallBox. offsetWidth - mask. offsetWidth; var maxTop = smallBox. offsetHeight - mask. offsetHeight; x = x > maxLeft ? maxLeft : x; y = y > maxTop ? maxTop : y; mask. style. left = x + "px" ; mask. style. top = y + "px" ; var biliX = mask. offsetWidth / ( bigBox. offsetWidth - 2 ) ; var biliY = mask. offsetHeight / ( bigBox. offsetHeight - 2 ) ; bigImg. style. left = - x / biliX + "px" ; bigImg. style. top = - y / biliY + "px" ; } ; < / script>
效果图为:
images