js中的鼠标位置及元素宽度位置总结

作者 : 开心源码 本文共1687个字,预计阅读时间需要5分钟 发布时间: 2022-05-14 共215人阅读

不废话,直接上总结图!

鼠标位置总结

鼠标位置总结

y同各x

鼠标位置图例

元素宽度总结

元素宽度总结

heightwidth

各left总结各top总结

注意:offsetLeftoffsetTop依赖offsetParent元素

HTMLElement.offsetParent 是一个只读属性,返回一个指向最近的(指包含层级上的最近)包含该元素的定位元素或者者最近的 table,``td,``th,``body元素。当元素的 style.display 设置为 “none” 时,offsetParent 返回 nulloffsetParent 很有用,由于 offsetTopoffsetLeft 都是相对于其内边距边界的。

元素宽度距离图例

元素距离总结

元素距离可以使用getBoundingClientRect,返回元素的大小及其相对于视口(浏览器)的位置。

  • 标准盒子模型width===offsetWidth,height===offsetHeightbox模型width===width,height===height
  • left,top了解似css里面的left,top
  • right=left+width
  • bottom=top+height
    getBoundingClientRect

测试代码:

<div            style="                width: 100px;                height: 100px;                margin-top: 50px;                margin-left: 50px;                padding: 10px;                border: 5px solid #ccc;                background: #4abf14;                overflow: auto;            "            @click="test"        >            测试元素各种宽度 测试元素各种宽度 测试元素各种宽度 测试元素各种宽度            测试元素各种宽度        </div>//js      test(e) {            const target = e.target;            console.log("target", target);            console.log("clientX", e.clientX);            console.log("offsetX", e.offsetX);            console.log("x", e.x);            console.log("pageX", e.pageX);            console.log("screenX", e.screenX);            console.log("clientWidth", target.clientWidth);            console.log("clientLeft", target.clientLeft);            console.log("clientTop", target.clientTop);            console.log("offsetWidth", target.offsetWidth);            console.log("offsetLeft", target.offsetLeft);            console.log("offsetTop", target.offsetTop);            console.log("scrollWidth", target.scrollWidth);            console.log("scrollLeft", target.scrollLeft);            console.log("scrollTop", target.scrollTop);            console.log("getBoundingClientRect",target.getBoundingClientRect());            console.log("screen.width", screen.width);            console.log("availWidth", screen.availWidth);            console.log("availHeight", screen.availHeight);            console.log("window.innerWidth", window.innerWidth);            console.log("window.outerWidth", window.outerWidth);        },

参考文章

scrollWidth、clientWidth、offsetWidth的区别及总结

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » js中的鼠标位置及元素宽度位置总结

发表回复