博客列表 >放大镜效果2 (这个有点问题)

放大镜效果2 (这个有点问题)

default
default原创
2022年03月04日 17:52:30437浏览
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>点击方块 移动</title>
  8. <style type="text/css">
  9. *{
  10. margin: 0;
  11. padding: 0;
  12. }
  13. .div{
  14. width: 400px;
  15. height:400px;
  16. margin: 100px;
  17. border: 1px solid #333;
  18. position: relative;
  19. left: 0px;
  20. }
  21. .mask{
  22. width: 200px;
  23. height: 200px;
  24. border:1px solid yellow;
  25. background-color: yellow;
  26. position: absolute;
  27. top: 0;
  28. opacity: 0.5;
  29. display: none;
  30. cursor: move;
  31. }
  32. .big{
  33. width: 400px;
  34. height:400px;
  35. border: 1px solid #333;
  36. position: absolute;
  37. top: 0px;
  38. left: 500px;
  39. display: none;
  40. overflow: hidden;
  41. }
  42. img{
  43. width: 100%;
  44. }
  45. </style>
  46. </head>
  47. <body>
  48. <div id="">
  49. <div class="div">
  50. <img src="https://img14.360buyimg.com/n1/s450x450_jfs/t1/132382/31/23673/311148/6220170fEd0692b77/a49312c8dbbc0445.jpg" >
  51. <div class="mask">
  52. </div>
  53. </div>
  54. <div class="big">
  55. <img src="https://img14.360buyimg.com/n1/s450x450_jfs/t1/132382/31/23673/311148/6220170fEd0692b77/a49312c8dbbc0445.jpg" style="width: 800px; position: absolute;" >
  56. </div>
  57. </div>
  58. <script type="text/javascript">
  59. var mask = document.querySelector('.mask')
  60. var div = document.querySelector('.div')
  61. var big = document.querySelector('.big')
  62. var img = big.children[0]
  63. div.addEventListener('mouseover',function(){
  64. mask.style.display ='block'
  65. big.style.display = 'block'
  66. div.addEventListener('mousemove',function(e){
  67. // 鼠标的坐标
  68. var x =e.pageX - div.offsetLeft
  69. var y =e.pageY - div.offsetTop
  70. // 鼠标移动的距离
  71. var mouseMoveX = x- mask.offsetWidth/2
  72. var mouseMoveY = y- mask.offsetHeight/2
  73. // 盒子可以移动的距离
  74. var divMaxX = div.offsetWidth - mask.offsetWidth
  75. var divMaxY = div.offsetHeight - mask.offsetHeight
  76. var imgMax = img.offsetWidth - div.offsetWidth
  77. // console.log(imgMax)
  78. if(mouseMoveX <= 0 ){
  79. mouseMoveX =0
  80. }else if(mouseMoveX > divMaxX){
  81. mouseMoveX = divMaxX
  82. }
  83. if(mouseMoveY <= 0 ){
  84. mouseMoveY =0
  85. }else if(mouseMoveY > divMaxY){
  86. mouseMoveY = divMaxY
  87. }
  88. mask.style.left = mouseMoveX + 'px'
  89. mask.style.top = mouseMoveY + 'px'
  90. // 有个公式 移动的倍数 = 鼠标移动距离 * 大图片减去小图片的距离 / 盒子移动的最大距离
  91. // var flagX = mouseMoveX * imgMax / divMaxX
  92. // var flagY = mouseMoveY * imgMax / divMaxY
  93. var flag = img.offsetWidth/ div.offsetWidth
  94. console.log(div.offsetWidth)
  95. console.log(img.offsetWidth)
  96. img.style.left = - mouseMoveX *flag +'px'
  97. img.style.top = - mouseMoveY *flag +'px'
  98. })
  99. })
  100. div.addEventListener('mouseout',function(){
  101. mask.style.display = 'none'
  102. big.style.display = 'none'
  103. })
  104. </script>
  105. </body>
  106. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议