`

javascript - trick to get and set height and width

阅读更多

 

when looking at properties that could have an auto value, it was mentioned that height and width are two properties that frequently have these values.

 

 

the offsetHeight and offsetWidth properties provide just that: A fairly reliable place to access the correct height and width of an element. The numbers returned by the two properties include the padding of the element as well.

 

this is all well, but there is one gotcha, though that if hte element is hidden, the offsetWidth an offsetHeight will be 0; so we hae to take a woraround to temporarily make the element visible, but in a hidden state.

 

achieve this by setting the display property to block (making it visible and able to be measured) and immediately set its visibility to hidden (making it invisible to the user) and finally set its position to absolute (taking it out of the flow of the document, stopping it from pushing any other elements out of the way)

 

below shows the code sample how to do that. 

 

 

 

<html>
<head>
<title>Height/Width Test</title>
<script type="text/javascript">
  (function () {
    this.height = function (elem) {
      return this.offsetHeight ||
      swap(elem, props, function () {
        return elem.offsetHeight;
      });
    };
    this.width = function (elem) {
      return this.offsetWidth ||
        swap(elem, props, function () {
          return elem.offsetWidth;
      });
    };
    var props = {
      position: "absolute",
      visibility: "hidden",
      display: "block"
    };
    // the wap function will temporary set the 
    // e.g. positoin to be "absolute"
    //      visiblity to be "hidden"
    // after retrieve the value, store the old value back 
    function swap(elem, options, callback) {
      var old = {}, ret;
      // Remember the old values, and insert the new ones
      for (var name in options) {
        old[name] = elem.style[name];
        elem.style[name] = options[name];
      }
      ret = callback();
      // Revert the old values
      for (var name in options) {
        elem.style[name] = old[name];
      }
      return ret;
    }
  })();
  window.onload = function () {
    var block = document.getElementById("block");
    var none = document.getElementById("none");
    // Both will alert out the same values
    alert(width(block) + " " + height(block));
    alert(width(none) + " " + height(none));
  };
</script>
</head>
<body>
<div id="block">Test</div>
<div id="none" style="display:none;">Test</div>
</body>
</html>
 
分享到:
评论

相关推荐

    Pandas-and-NumPy-Tips-Trick-and-Techniques-master.zip

    Pandas-and-NumPy-Tips-Trick-and-Techniques-master

    S-a-D-trick.rar_Join In

    在VB6(Visual Basic 6)中,"S-a-D-trick.rar_Join In" 这个主题涉及到的是文件分割与合并的技术。在处理大文件时,为了方便传输或者存储,我们可能会选择将其分割成多个小文件,而在需要使用整个文件时,则需要将...

    python-trick

    Python-trick,上传的事pdf文档

    在规定的时间内,使用鼠标控制帽子接住落下的保龄球,躲避炸弹。_Hat-Trick.zip

    在规定的时间内,使用鼠标控制帽子接住落下的保龄球,躲避炸弹。_Hat-Trick

    HLP-Trick-crx插件

    【HLP-Trick-crx插件】是一款专为解决特定网页限制而设计的浏览器扩展程序,主要用于恢复用户在浏览网页时被禁用的复制、粘贴功能,以及上下文菜单和本地高亮显示功能。这款插件特别适用于那些因为版权保护或者安全...

    深度学习领域CNN橄榄球比赛NFL目标检测(带数据集)-cnn-baseline-more-tta-trick

    语言:python 内容包括:源码、数据集、数据集描述、论文 目的:使用CNN算法在橄榄球比赛中目标检测。 带数据集很好运行,主页有搭建环境过程。主页有更多源码。 数据集描述如下: 在这场比赛中,你的任务是预测球员...

    gtg-grind-trick-generator

    gtg-grind-trick-generator PWA Web应用程序(Node.js,JS,HTML,CSS) Chrome,Safari,Firefox,Edge(Android,iOS,MacOS,Windows) 离线工作Android应用程式使用Google Workbox,Webpack制作

    py-trick-book:关于 Python 的高级提示和技巧

    Python 提示和技巧 ... git clone https://github.com/plasmashadow/py-trick-book.git 我假设您安装了 ipython 导航到目录并执行 ipython notebook 笔记: 退出间谍活动并自己阅读 或者 保持冷静,加入草帽海贼团

    tips-n-trick

    尺寸可以通过width和height属性预先设定,以提高页面加载速度。 表单是HTML中的重要部分,、、、和等元素用于收集用户输入。了解如何使用type属性(如text、email、password等)和action、method属性(指定提交表单...

    [Head.First.JavaScript].Michael.Morrison...

    specialist, you’ll be able to take a cluttered room of JavaScript data and impose your will on it with a flurry of virtual labels and storage bins. storing data Your scripts can store data 34 Scripts...

    vdr-hattrick-开源

    1. 下载最新版本的源代码,例如压缩包内的hattrick-0.1.6。 2. 在VDR环境中编译和安装插件。这可能需要熟悉Linux环境和基本的编译命令。 3. 配置VDR以启用Hattrick插件,并设置与Hattrick账户的连接信息。 4. 更新...

    21-card-trick:在 React 中完成的 21 张卡片技巧

    React 21 卡技巧一个演示卡片技巧的React应用程序。动机该项目旨在学习如何使用 React 钩子和进行嵌套的 api 调用。 该项目不再进行。怎么玩记住 21 张卡片中的 1 张后,单击完成。 选择您的卡片所在的 3 堆中的哪一...

    CMCC-Trick:招惹CMCC-* WLAN

    Trick-CMCC 利用CMCC公共热点的小漏洞免费上网~~ :) sudo ./conn.sh Notice: 目前只知道我工CMCC有这特色, 其他地区尚不明确 Notice: 脚本适用于使用NetWorkManager网络sds管理工具的系统 Notice: 不必惊讶原理, ...

    计算机网络第六版答案

    28. Trudy can pretend to be Bob to Alice (and vice-versa) and partially or completely modify the message(s) being sent from Bob to Alice. For example, she can easily change the phrase “Alice, I owe ...

    HLP-绝招「HLP-Trick」-crx插件

    该扩展程序重新启用网页上的复制/粘贴功能,上下文菜单和本机突出显示功能。 支持语言:English (United States)

    NIO trick and trap NIO网络

    ### NIO Trick and Trap:构建高性能Java NIO网络框架 #### 概述 NIO(New I/O),作为Java平台的一项重要技术革新,为开发者提供了更高效的数据处理方式。相较于传统的IO模型,NIO通过非阻塞式I/O操作、多路复用...

    Bishop Pattern Recognition and Machine Learning

    **Bishop's "Pattern Recognition and Machine Learning"** is a comprehensive and authoritative text that serves as both an introduction to the field of machine learning and a reference for advanced ...

    英文原版-Instant Puppet 3 starter 1st Edition

    Most importantly, it teaches you the trick to get better results every time, by thinking about and expressing your desired outcome in a deterministic fashion.“Instant Puppet 3 Starter” provides you...

    Trick

    "Trick"这一主题似乎与一套特别的字体资源相关,其中包括多种不同风格的图像文件(.gif)和TrueType字体文件(.TTF)。让我们深入探讨一下这个话题。 首先,.gif 文件是一种常见的图像格式,支持透明度和动画,常...

    Go.in.Practice.1633430073

    Built with simplicity, concurrency, and modern applications in mind, Go provides the core tool set for rapidly building web, cloud, and systems applications. If you know a language like Java or C#, ...

Global site tag (gtag.js) - Google Analytics