生活随笔
收集整理的這篇文章主要介紹了
放大镜制作(1)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
放大鏡制作
<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
總結
以上是生活随笔為你收集整理的放大镜制作(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。