放大镜案例:附图

﹏ヽ暗。殇╰゛Y 2022-04-23 15:16 343阅读 0赞

鼠标随着小方框的移动在右边大图显示出来。
在这里插入图片描述

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>$美少女战士$</title>
  6. <style>
  7. .smallBox {
  8. position: relative;
  9. width: 400px;
  10. height: 250px;
  11. margin-left: 100px;
  12. margin-top: 100px;
  13. float: left;
  14. }
  15. .smallBox img {
  16. width: 400px;
  17. }
  18. .bigBox {
  19. position: relative;
  20. width: 800px;
  21. height: 500px;
  22. margin-left: 50px;
  23. margin-top: 100px;
  24. float: left;
  25. border: 1px solid #ccc ;
  26. overflow: hidden;
  27. /* display: none;*/
  28. }
  29. .move {
  30. position: absolute;
  31. left:0;
  32. top:0;
  33. width: 120px;
  34. height: 120px;
  35. background-color: rgba(234,23,56,0.4);
  36. /*display: none;*/
  37. }
  38. #bigPic {
  39. position: absolute;
  40. left: 0;
  41. top:0;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <!--小盒子-->
  47. <div class="smallBox">
  48. <img src="2-small.jpg" alt="" id="smallPic">
  49. <div class="move"></div>
  50. </div>
  51. <div class="bigBox">
  52. <img src="2-big.jpg" alt="" id="bigPic">
  53. </div>
  54. <script src="获取元素.js"></script>
  55. <script>
  56. /*
  57. offsetleft offsetTop
  58. 有绝对定位属性的: 他获取的是距离父元素左上角的距离值
  59. 没有绝对定位属性 他获取的是距离body 左上角的距离值
  60. * offsetWidth
  61. * offsetHeight --->盒模型总高
  62. *
  63. * */
  64. // window.onload: 页面加载完成
  65. // 一,window.onload的用法:
  66. // 因为页面中的代码一般情况下按照,从上到下,从左到右的顺序执行。
  67. // 所以当js代码需要获取页面中的元素时,如果script标签在元素的前面,需要加window.onload;如果script放在了元素后面,就不需要加 window.onload。
  68. // ---------------------
  69. window.onload = function () {
  70. var smallBox = my$('.smallBox') //小盒子
  71. var move = my$('.move') //运动元素
  72. var bigBox = my$('.bigBox') //大盒子
  73. var bigPic = my$('#bigPic') //大照片
  74. /*
  75. * 1:阴影块随着鼠标动---》获取鼠标位置
  76. *
  77. * */
  78. //鼠标移入显示
  79. /*smallBox.onmouseover = function(){
  80. //让move和大盒子显示
  81. move.style.display = 'block'
  82. bigBox.style.display = 'block'
  83. }
  84. //鼠标移开消失
  85. smallBox.onmouseout = function(){
  86. move.style.display = 'none'
  87. bigBox.style.display = 'none'
  88. }
  89. */
  90. //鼠标在小照片盒子上运动
  91. smallBox.onmousemove = function (e) {
  92. //将鼠标变为移动样式
  93. this.style.cursor = 'move'
  94. console.log(e.clientX)
  95. //先判断此时偏移量到底满足条件与否 如果不满足不赋值
  96. //只需要判断超出边界的情况 水平运动 最小值 0
  97. // 最大值 smallBox.offsetWidth-move.offsetWidth
  98. //因为鼠标在move中间 所以小move/2
  99. var newLeft = e.clientX-smallBox.offsetLeft-move.offsetWidth/2;
  100. var newTop = e.clientY-smallBox.offsetTop-move.offsetHeight/2;
  101. if(newLeft < 0){
  102. newLeft = 0;
  103. }
  104. if(newLeft > smallBox.offsetWidth-move.offsetWidth ){
  105. newLeft = smallBox.offsetWidth-move.offsetWidth
  106. }
  107. if(newTop < 0){
  108. newTop = 0;
  109. }
  110. if(newTop > smallBox.offsetHeight-move.offsetHeight ){
  111. newTop = smallBox.offsetHeight-move.offsetHeight
  112. }
  113. move.style.left = newLeft +'px'
  114. move.style.top = newTop +'px'
  115. /******计算比例
  116. * 比例=小图宽度/大图宽度 pass
  117. * 小盒子/大盒子 pass
  118. *
  119. * (3)宽比例 move的宽度-小盒子的宽度 /大盒子的宽度-大照片的宽度
  120. * (4)高比例move的高度-小盒子的高度 /大盒子的高度-大照片的高度
  121. *
  122. * ******/
  123. var widthRate = (my$('.move').offsetWidth -smallBox.offsetWidth)/
  124. (bigBox.offsetWidth-bigPic.offsetWidth) ;
  125. var heightRate = (my$('.move').offsetHeight -smallBox.offsetHeight)/
  126. (bigBox.offsetHeight-bigPic.offsetHeight) ;
  127. bigPic.style.left = -parseInt(move.style.left) / widthRate +'px'
  128. bigPic.style.top = -parseInt(move.style.top) / heightRate +'px'
  129. }
  130. }
  131. </script>
  132. </body>
  133. </html>

发表评论

表情:
评论列表 (有 0 条评论,343人围观)

还没有评论,来说两句吧...

相关阅读

    相关 vue放大镜

    最近有一个需求是要像淘宝商品详情页那样,分享一下。小白第一次分享,各位大神莫见笑。 Build Setup 使用步骤 安装 install npm i