实现网页图片的放大镜效果

深藏阁楼爱情的钟 2023-07-10 14:17 96阅读 0赞

实现某东上面的商品放大镜效果
在这里插入图片描述


实现代码

  1. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style type="text/css"> *{ //清除浏览器原带的格式
  2. margin: 0;
  3. padding: 0;
  4. }
  5. #content{
  6. width: 800px;
  7. height: 800px;
  8. margin: 0 auto;
  9. border: 1px green solid;
  10. font-size: 12px;
  11. }
  12. #box{
  13. width: 200px;
  14. height: 60px;
  15. position: relative; /*整个大盒子,并设定相对定位好移动盒子内部的图片*/
  16. }
  17. #b4{
  18. width: 160px; /*注意b1,b2,b3,b4盒子的大小必须注意他们的比例*/
  19. height: 160px; /*b4大小是b1的4倍*/
  20. overflow: hidden;
  21. border: 1px black solid;
  22. display: none; /*开始时这个盒子是隐藏的*/
  23. position: relative; /*设定定位,利于b3移动*/
  24. }
  25. #b1{
  26. display: none; /*开始时这个盒子是隐藏的*/
  27. position: absolute; /*设定定位,利于b1移动*/
  28. width: 40px; /*设定盒子的大小*/
  29. height: 40px;
  30. background-color: rgba(150,150,150,0.3);
  31. cursor:move; /*改变鼠标光标样式*/
  32. }
  33. #b2{
  34. width: 100%; /*设定盒子的大小,为继承父标签大小,宽200px,高60px*/
  35. height:100%;
  36. }
  37. #b3{
  38. position:absolute; /*设定定位,利于b3移动*/
  39. width: 800px; /*大小为b2的4倍*/
  40. height: 240px;
  41. }
  42. </style> <script type="text/javascript" defer=""> window.addEventListener('load',function () {
  43. var box=document.querySelector('#box');
  44. var b4=document.querySelector('#b4'); box.addEventListener('mouseover',function () { //鼠标移动到box上面的时候b1和b4显示
  45. box.children[0].style.display='block';
  46. b4.style.display='block';
  47. }); box.addEventListener('mouseout',function () { //鼠标移出box的时候b1和b4隐藏
  48. box.children[0].style.display='none';
  49. b4.style.display='none';
  50. }); box.addEventListener('mousemove',function (e) {
  51. var x=e.pageX-box.offsetLeft-box.children[0].offsetWidth/2;
  52. var y=e.pageY-box.offsetTop-box.children[0].offsetHeight/2;
  53. /* *这里的x,y都是相对与box的left和top * x=鼠标到网页左边框距离 - box的左边框到网页左边框距离 - b1宽的一半 * y=鼠标到网页上边框距离 - box的上边框到网页上边框距离 - b1高的一半 * 那么为什么要减去b1的宽一半或者高一半呢? * 因为盒子开始定位的时候鼠标在盒子的左上角,而需要鼠标定位到盒子中间,所以需要这样 */
  54. var xBig=box.children[1].offsetWidth-box.children[0].offsetWidth; if(x<=0){
  55. x=0;
  56. }
  57. else if(x >= xBig){
  58. x=xBig;
  59. }
  60. if(y<=0){ //顶顶顶
  61. y=0;
  62. }
  63. else if(y >= box.children[1].offsetHeight-box.children[0].offsetHeight){
  64. y=box.children[1].offsetHeight-box.children[0].offsetHeight;
  65. }
  66. /* * xBig(鼠标在x方向的最大移动距离)=b2的宽度 - b1的宽度 * x值不能小于0,当x小于0的时候,令x=0 * x值不能大于xBig,当x大于xBig的时候,令x=xBig * y也是同理 */
  67. box.children[0].style.left=x+'px'; //给b1设定定位
  68. box.children[0].style.top=y+'px';
  69. var xMBig=b4.firstElementChild.offsetWidth-b4.offsetWidth;
  70. b4.firstElementChild.style.left=-x*xMBig/xBig+'px'; //给b3设定定位
  71. b4.firstElementChild.style.top=-y*xMBig/xBig+'px';
  72. /* * 因为b1,b2,b3,b4大小是相关的 * 所以b1在b2里面移动的x,y距离与b3在b4里面移动的距离有一定的倍数关系 * xMBig是b3在b4里面的最大移动距离 * 也要注意b3定位的ldft和top一定是负的 */
  73. });
  74. })
  75. </script>
  76. </head>
  77. <body>
  78. <div id="content">
  79. <div id="box">
  80. <div id="b1"></div>
  81. <img src="../images/image07.jpg" alt="" id="b2" title=""/>
  82. <div id="b4"><img src="../images/image07.jpg" id="b3" alt=""/></div>
  83. </div>
  84. </div>
  85. </body>
  86. </html>

效果图:

在这里插入图片描述
本文只用于个人学习和纪录

发表评论

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

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

相关阅读

    相关 原生js实现放大镜效果

    今天是中秋佳节,在这了我祝大家中秋快乐,心想事成,工作顺利哈! 放大镜效果在电商网站上较为常见的 一个效果,主要针对鼠标在图片选取部分放大查看。效果如图所示 ![...

    相关 jQuery实现放大镜效果

    1.1.1 摘要 相信大家都见过或使用过放大镜效果,甚至实现过该效果,它一般应用于放大查看商品图片,一些电商网站(例如:凡客,京东商城,阿里巴巴等)都有类似