鱼C论坛

 找回密码
 立即注册
查看: 2145|回复: 1

[已解决]getBoundingClientRect返回值全为0

[复制链接]
发表于 2022-5-11 23:03:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 李万金 于 2022-5-11 23:08 编辑

<li>的getBoundingClientRect返回值全为0(打印在console界面),但它明显不在左上角。
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <title>Follow Along Nav</title>
  6.   <style>
  7.     html {
  8.       box-sizing: border-box;
  9.       font-family: "Arial Rounded MT Bold", "Helvetica Rounded", Arial, sans-serif;
  10.     }

  11.     *,
  12.     *:before,
  13.     *:after {
  14.       box-sizing: inherit;
  15.     }

  16.     body {
  17.       margin: 0;
  18.       min-height: 100vh;
  19.       background:
  20.         linear-gradient(45deg, hsla(340, 100%, 55%, 1) 0%, hsla(340, 100%, 55%, 0) 70%),
  21.         linear-gradient(135deg, hsla(225, 95%, 50%, 1) 10%, hsla(225, 95%, 50%, 0) 80%),
  22.         linear-gradient(225deg, hsla(140, 90%, 50%, 1) 10%, hsla(140, 90%, 50%, 0) 80%),
  23.         linear-gradient(315deg, hsla(35, 95%, 55%, 1) 100%, hsla(35, 95%, 55%, 0) 70%);
  24.     }

  25.     h2 {
  26.       margin-top: 0;
  27.       padding-top: .8em;
  28.     }

  29.     nav {
  30.       position: relative;
  31.       perspective: 600px;
  32.     }

  33.     .cool>li>a {
  34.       color: yellow;
  35.       text-decoration: none;
  36.       font-size: 20px;
  37.       background: rgba(0, 0, 0, 0.2);
  38.       padding: 10px 20px;
  39.       display: inline-block;
  40.       margin: 20px;
  41.       border-radius: 5px;
  42.     }

  43.     nav ul {
  44.       list-style: none;
  45.       margin: 0;
  46.       padding: 0;
  47.       display: flex;
  48.       justify-content: center;
  49.     }

  50.     .cool>li {
  51.       position: relative;
  52.       display: flex;
  53.       justify-content: center;
  54.     }

  55.     .dropdown {
  56.       opacity: 0;
  57.       position: absolute;
  58.       overflow: hidden;
  59.       padding: 20px;
  60.       top: -20px;
  61.       border-radius: 2px;
  62.       transition: all 0.5s;
  63.       transform: translateY(100px);
  64.       will-change: opacity;
  65.       display: none;
  66.     }

  67.     .trigger-enter .dropdown {
  68.       display: block;
  69.     }

  70.     .trigger-enter-active .dropdown {
  71.       opacity: 1;
  72.     }

  73.     .dropdownBackground {
  74.       width: 100px;
  75.       height: 100px;
  76.       position: absolute;
  77.       background: #fff;
  78.       border-radius: 4px;
  79.       box-shadow: 0 50px 100px rgba(50, 50, 93, .1), 0 15px 35px rgba(50, 50, 93, .15), 0 5px 15px rgba(0, 0, 0, .1);
  80.       transition: all 0.3s, opacity 0.1s, transform 0.2s;
  81.       transform-origin: 50% 0;
  82.       display: flex;
  83.       justify-content: center;
  84.       opacity: 0;
  85.     }

  86.     .dropdownBackground.open {
  87.       opacity: 1;
  88.     }

  89.     .arrow {
  90.       position: absolute;
  91.       width: 20px;
  92.       height: 20px;
  93.       display: block;
  94.       background: white;
  95.       transform: translateY(-50%) rotate(45deg);
  96.     }

  97.     .bio {
  98.       min-width: 500px;
  99.       display: flex;
  100.       justify-content: center;
  101.       align-items: center;
  102.       line-height: 1.7;
  103.     }

  104.     .bio img {
  105.       float: left;
  106.       margin-right: 20px;
  107.     }

  108.     .courses {
  109.       min-width: 300px;
  110.     }

  111.     .courses li {
  112.       padding: 10px 0;
  113.       display: block;
  114.       border-bottom: 1px solid rgba(0, 0, 0, 0.2);
  115.     }

  116.     .dropdown a {
  117.       text-decoration: none;
  118.       color: #ffc600;
  119.     }

  120.     a.button {
  121.       background: black;
  122.       display: block;
  123.       padding: 10px;
  124.       color: white;
  125.       margin-bottom: 10px;
  126.     }

  127.     /* Matches Twitter, TWITTER, twitter, tWitter, TWiTTeR... */
  128.     .button[href*=twitter] {
  129.       background: #019FE9;
  130.     }

  131.     .button[href*=facebook] {
  132.       background: #3B5998;
  133.     }

  134.     .button[href*=courses] {
  135.       background: #ffc600;
  136.     }
  137.   </style>
  138. </head>
  139. <body>
  140.   <h2>Cool</h2>
  141.   <nav class="top">
  142.     <div class="dropdownBackground">
  143.       <span class="arrow"></span>
  144.     </div>

  145.     <ul class="cool">
  146.       <li>
  147.         <a href="#">About Me</a>
  148.         <div class="dropdown dropdown1">
  149.           <div class="bio">
  150.             <img src="https://logo.clearbit.com/wesbos.com">
  151.             <p>Wes Bos sure does love web development. He teaches things like JavaScript, CSS and BBQ. Wait. BBQ isn't part of web development. It should be though!</p>
  152.           </div>
  153.         </div>
  154.       </li>
  155.       <li>
  156.         <a href="#">Courses</a>
  157.         <ul class="dropdown courses"  >   
  158.           <li class="why">
  159.             <span class="code">RFB</span>
  160.             <a href="https://ReactForBeginners.com">React For Beginners</a>
  161.           </li>
  162.           <li>
  163.             <span class="code">ES6</span>
  164.             <a href="https://ES6.io">ES6 For Everyone</a>
  165.           </li>
  166.           <li>
  167.             <span class="code">NODE</span>
  168.             <a href="https://LearnNode.com">Learn Node</a>
  169.           </li>
  170.           <li>
  171.             <span class="code">STPU</span>
  172.             <a href="https://SublimeTextBook.com">Sublime Text Power User</a>
  173.           </li>
  174.           <li>
  175.             <span class="code">WTF</span>
  176.             <a href="http://Flexbox.io">What The Flexbox?!</a>
  177.           </li>
  178.           <li>
  179.             <span class="code">GRID</span>
  180.             <a href="https://CSSGrid.io">CSS Grid</a>
  181.           </li>
  182.           <li>
  183.             <span class="code">LRX</span>
  184.             <a href="http://LearnRedux.com">Learn Redux</a>
  185.           </li>
  186.           <li>
  187.             <span class="code">CLPU</span>
  188.             <a href="http://CommandLinePowerUser.com">Command Line Power User</a>
  189.           </li>
  190.           <li>
  191.             <span class="code">MMD</span>
  192.             <a href="http://MasteringMarkdown.com">Mastering Markdown</a>
  193.           </li>
  194.         </ul>
  195.       </li>
  196.       <li>
  197.         <a href="#">Other Links</a>
  198.         <ul class="dropdown dropdown3">
  199.           <li><a class="button" href="http://twitter.com/wesbos">Twitter</a></li>
  200.           <li><a class="button" href="http://facebook.com/wesbos.developer">Facebook</a></li>
  201.           <li><a class="button" href="http://wesbos.com">Blog</a></li>
  202.           <li><a class="button" href="http://wesbos.com/courses">Course Catalog</a></li>
  203.         </ul>
  204.       </li>
  205.     </ul>
  206.   </nav>



  207. <script>
  208.   const whyLabel = document.querySelector('.why')
  209.   positionOfWhyLabel = whyLabel.getBoundingClientRect();
  210.   console.log(positionOfWhyLabel);
  211. </script>

  212. </body>
  213. </html>
复制代码
最佳答案
2022-5-25 21:04:10
你的<li>元素都显示不出来,当然为0.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-25 21:04:10 | 显示全部楼层    本楼为最佳答案   
你的<li>元素都显示不出来,当然为0.
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 18:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表