|
|

楼主 |
发表于 2022-4-29 12:21
|
显示全部楼层
代码:
- <style>
- .mnBox { left: -214px; width: 1024px; height: 600px; background: #ccc; box-sizing: border-box; padding: 20px; cursor: pointer; position: relative; }
- .line38 { height: 100%; width: 1px; position: absolute; left: 50%; top: 0; background: red;}
- .mouse-tips { color: olive; }
- </style>
- <div class="mnBox">
- <div class="line38"></div>
- <div class="mouse-tips">单击检测鼠标位置</div>
- </div>
- <script>
- let main = document.querySelector(".mnBox");
- let tips = document.querySelector(".mouse-tips");
- main.onclick = function(e) {
- e = e || event;
- let x = e.clientX - this.offsetLeft;
- let y = this.clientWidth;
- if(x > y / 2) {
- tips.innerText = "右边";
- } else {
- tips.innerText = "左边";
- }
- }
- </script>
复制代码
|
|