|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <style type="text/css"> |
| 6 | + * { |
| 7 | + margin: 0; |
| 8 | + padding: 0; |
| 9 | + } |
| 10 | + .normal { |
| 11 | + width: 300px; |
| 12 | + height: 300px; |
| 13 | + position: relative; |
| 14 | + } |
| 15 | + .normal > img { |
| 16 | + width: 100%; |
| 17 | + height: 100%; |
| 18 | + } |
| 19 | + .lay { |
| 20 | + width: 150px; |
| 21 | + height: 150px; |
| 22 | + background: orange; |
| 23 | + opacity: 0.5; |
| 24 | + position: absolute; |
| 25 | + left: 0; |
| 26 | + top: 0; |
| 27 | + cursor: move; |
| 28 | + display: none; |
| 29 | + } |
| 30 | + .big { |
| 31 | + width: 300px; |
| 32 | + height: 300px; |
| 33 | + position: absolute; |
| 34 | + left: 350px; |
| 35 | + top: 0px; |
| 36 | + overflow: hidden; |
| 37 | + display: none; |
| 38 | + } |
| 39 | + .big > img { |
| 40 | + width: 600px; |
| 41 | + height: 600px; |
| 42 | + position: absolute; |
| 43 | + display: block; |
| 44 | + } |
| 45 | + </style> |
| 46 | + </head> |
| 47 | + <body> |
| 48 | + <section id="magnifier"></section> |
| 49 | + |
| 50 | + <script type="text/javascript"> |
| 51 | + var magnifier = { |
| 52 | + init(param) { |
| 53 | + const el = param.el; |
| 54 | + const src = param.src; |
| 55 | + if (!el || !src) return; |
| 56 | + this.createElement(el, src); |
| 57 | + }, |
| 58 | + createElement(el, src) { |
| 59 | + const _this = this; |
| 60 | + const normalDiv = document.createElement('div'); |
| 61 | + normalDiv.className = 'normal'; |
| 62 | + const normalImg = document.createElement('img'); |
| 63 | + normalImg.src = src.normal; |
| 64 | + const normalSpan = document.createElement('span'); |
| 65 | + normalSpan.className = 'lay'; |
| 66 | + normalDiv.appendChild(normalImg); |
| 67 | + normalDiv.appendChild(normalSpan); |
| 68 | + |
| 69 | + const bigDiv = document.createElement('div'); |
| 70 | + bigDiv.className = 'big'; |
| 71 | + const bigImg = document.createElement('img'); |
| 72 | + bigImg.src = src.big; |
| 73 | + bigDiv.appendChild(bigImg); |
| 74 | + |
| 75 | + el.appendChild(normalDiv); |
| 76 | + el.appendChild(bigDiv); |
| 77 | + |
| 78 | + normalDiv.addEventListener('mouseover', () => { |
| 79 | + // TODO: 放大镜、被放大的图片都显示,渲染为块级元素 |
| 80 | + normalSpan.style.display = 'block'; |
| 81 | + bigDiv.style.display = 'block'; |
| 82 | + }); |
| 83 | + normalDiv.addEventListener('mouseout', () => { |
| 84 | + // TODO: 放大镜、被放大的图片都隐藏,不渲染 |
| 85 | + normalSpan.style.display = 'none'; |
| 86 | + bigDiv.style.display = 'none'; |
| 87 | + }); |
| 88 | + |
| 89 | + normalDiv.addEventListener('mousemove', _moveHandler); |
| 90 | + |
| 91 | + function _moveHandler(event) { |
| 92 | + event = event || window.event; |
| 93 | + _this.moveHandler(event, normalDiv, normalSpan, bigImg); |
| 94 | + } |
| 95 | + }, |
| 96 | + moveHandler(event, normalDiv, normalSpan, bigImg) { |
| 97 | + // tip: 该函数处理放大镜、被放大的图片的显示区域,x、y分别为鼠标在该模块中的位置坐标 |
| 98 | + |
| 99 | + const scale = 2; |
| 100 | + let x = event.clientX - normalSpan.offsetWidth / 2; |
| 101 | + let y = event.clientY - normalSpan.offsetHeight / 2; |
| 102 | + // TODO: 判断临界值,重新设置放大镜、被放大的图片的位置 |
| 103 | + let xMin = 0; |
| 104 | + let yMin = 0; |
| 105 | + let xMax = normalDiv.offsetWidth - normalSpan.offsetWidth; |
| 106 | + let yMax = normalDiv.offsetWidth - normalSpan.offsetWidth; |
| 107 | + if (x < xMin) { |
| 108 | + x = xMin; |
| 109 | + } else if (x > xMax) { |
| 110 | + x = xMax; |
| 111 | + } |
| 112 | + if (y < yMin) { |
| 113 | + y = yMin; |
| 114 | + } else if (y > yMax) { |
| 115 | + y = yMax; |
| 116 | + } |
| 117 | + // console.log(x, y); |
| 118 | + // console.log(normalSpan); |
| 119 | + normalSpan.style.left = x + 'px'; |
| 120 | + normalSpan.style.top = y + 'px'; |
| 121 | + bigImg.style.left = -x * 2 + 'px'; |
| 122 | + bigImg.style.top = -y * 2 + 'px'; |
| 123 | + }, |
| 124 | + }; |
| 125 | + magnifier.init({ |
| 126 | + // TODO: 请获取id=magnifier的节点 |
| 127 | + |
| 128 | + el: document.getElementById('magnifier'), |
| 129 | + src: { |
| 130 | + normal: |
| 131 | + 'https://uploadfiles.nowcoder.com/images/20211201/920662346_1638327818015/FE8B1A979ADF6E3C2C114AF3F9CA693C', |
| 132 | + big: 'https://uploadfiles.nowcoder.com/images/20211201/920662346_1638327818015/FE8B1A979ADF6E3C2C114AF3F9CA693C', |
| 133 | + }, |
| 134 | + }); |
| 135 | + </script> |
| 136 | + </body> |
| 137 | +</html> |
0 commit comments