zoukankan      html  css  js  c++  java
  • javascript 温故而知新 getBoundingClientRect

    getBoundingClientRect获取元素位置 

    getBoundingClientRect用于获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置。

    getBoundingClientRect()最先是IE的私有属性,现在已经是一个W3C标准。所以你不用当心浏览器兼容问题,不过还是有区别的:IE只返回top,lef,right,bottom四个值,不够可以通过以下方法来获取width,height的值:

        var ro = object.getBoundingClientRect();
        var Width = ro.right - ro.left;
        var Height = ro.bottom - ro.top;

    兼容所有浏览器写法:

        var ro = object.getBoundingClientRect();
        var Top = ro.top;
        var Bottom = ro.bottom;
        var Left = ro.left;
        var Right = ro.right;
        var Width = ro.width||Right - Left;
        var Height = ro.height||Bottom - Top;

    有了这个方法,获取页面元素的位置就简单多了:

        var X= this.getBoundingClientRect().left+document.documentElement.scrollLeft;
        var Y =this.getBoundingClientRect().top+document.documentElement.scrollTop;

  • 相关阅读:
    UnityGUI Keynote
    Unity3D Asset 导入&导出
    Unity3d平台信息设置
    Unity3D自带Demo AngryBots路径
    如何判定Unity已破解成功
    fbx模型
    Init & Deinit & ARC
    Subscript & Inheritance
    Properties & Method
    Enumeration & Class & Structure
  • 原文地址:https://www.cnblogs.com/CyLee/p/6729619.html
Copyright © 2011-2022 走看看