addEventListener scroll Uncaught TypeError: element.getBoundingClientRect is not a function

huangapple go评论80阅读模式
英文:

addEventListener scroll Uncaught TypeError: element.getBoundingClientRect is not a function

问题

I'm trying to set up my code so it can detect if a certain section of my HTML is at the top of the viewport. I'm getting the error:

Uncaught TypeError: element.getBoundingClientRect is not a function

const items = document.querySelectorAll('section');

function isOnScreen(element, buffer) {
  //buffer is optional and allows you to return true when  
  //the element is going to appear to the screen  
  buffer = typeof buffer === 'undefined' ? 0 : buffer;
  // Get element's position in the viewport
  const bounding = element.getBoundingClientRect();
  // Check if element is in the viewport 
  if (bounding.top >= buffer && bounding.left >= buffer &&
    bounding.right <=
    // fallback for browser compatibility 
    ((window.innerWidth || document.documentElement.clientWidth) - buffer) &&
    bounding.bottom <=
    ((window.innerHeight || document.documentElement.clientHeight) - buffer)) {
    return true
    console.log(element);
  } else {
    return false;
  };
};

document.addEventListener('scroll', function() {
  for (item in items) {
    isOnScreen(item);
  };
});

I'm new to all this stuff and don't know what I'm doing wrong. If anyone can point me in the right direction, I would greatly appreciate it.

Thank you,
Michael

英文:

I'm trying to set up my code so it can detect if a certain section of my HTML is at the top of the viewport. I'm getting the error:

Uncaught TypeError: element.getBoundingClientRect is not a function

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const items = document.querySelectorAll(&#39;section&#39;);

function isOnScreen(element, buffer) {
  //buffer is optional and allows you to return true when  
  //the element is going to appear to the screen  
  buffer = typeof buffer === &#39;undefined&#39; ? 0 : buffer;
  // Get element&#39;s position in the viewport
  const bounding = element.getBoundingClientRect();
  // Check if element is in the viewport 
  if (bounding.top &gt;= buffer &amp;&amp; bounding.left &gt;= buffer &amp;&amp;
    bounding.right &lt;=
    // fallback for browser compatibility 
    ((window.innerWidth || document.documentElement.clientWidth) - buffer) &amp;&amp;
    bounding.bottom &lt;=
    ((window.innerHeight || document.documentElement.clientHeight) - buffer)) {
    return true
    console.log(element);
  } else {
    return false;
  };
};

document.addEventListener(&#39;scroll&#39;, function() {
  for (item in items) {
    isOnScreen(item);
  };
});

<!-- language: lang-html -->

&lt;body&gt;
  &lt;!-- HTML Follows BEM naming conventions 
  IDs are only used for sections to connect menu achors to sections --&gt;
  &lt;header class=&quot;page__header&quot;&gt;
    &lt;nav class=&quot;navbar__menu&quot;&gt;
      &lt;!-- Navigation starts as empty UL that will be populated with JS --&gt;
      &lt;ul id=&quot;navbar__list&quot;&gt;&lt;/ul&gt;
    &lt;/nav&gt;
  &lt;/header&gt;
  &lt;main&gt;
    &lt;header class=&quot;main__hero&quot;&gt;
      &lt;h1&gt;Landing Page &lt;/h1&gt;
    &lt;/header&gt;
    &lt;!-- Each Section has an ID (used for the anchor) and 
    a data attribute that will populate the li node.
    Adding more sections will automatically populate nav.
    The first section is set to active class by default --&gt;
    &lt;section id=&quot;section1&quot; data-nav=&quot;Section 1&quot;&gt;
      &lt;div class=&quot;landing__container&quot;&gt;
        &lt;h2&gt;Section 1&lt;/h2&gt;
        &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum metus faucibus lectus pharetra dapibus. Suspendisse potenti. Aenean aliquam elementum mi, ac euismod augue. Donec eget lacinia ex. Phasellus imperdiet porta orci eget mollis.
          Sed convallis sollicitudin mauris ac tincidunt. Donec bibendum, nulla eget bibendum consectetur, sem nisi aliquam leo, ut pulvinar quam nunc eu augue. Pellentesque maximus imperdiet elit a pharetra. Duis lectus mi, aliquam in mi quis, aliquam
          porttitor lacus. Morbi a tincidunt felis. Sed leo nunc, pharetra et elementum non, faucibus vitae elit. Integer nec libero venenatis libero ultricies molestie semper in tellus. Sed congue et odio sed euismod.&lt;/p&gt;

        &lt;p&gt;Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
          tortor, eget elementum tortor mollis non.&lt;/p&gt;
      &lt;/div&gt;
    &lt;/section&gt;
    &lt;section id=&quot;section2&quot; data-nav=&quot;Section 2&quot;&gt;
      &lt;div class=&quot;landing__container&quot;&gt;
        &lt;h2&gt;Section 2&lt;/h2&gt;
        &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum metus faucibus lectus pharetra dapibus. Suspendisse potenti. Aenean aliquam elementum mi, ac euismod augue. Donec eget lacinia ex. Phasellus imperdiet porta orci eget mollis.
          Sed convallis sollicitudin mauris ac tincidunt. Donec bibendum, nulla eget bibendum consectetur, sem nisi aliquam leo, ut pulvinar quam nunc eu augue. Pellentesque maximus imperdiet elit a pharetra. Duis lectus mi, aliquam in mi quis, aliquam
          porttitor lacus. Morbi a tincidunt felis. Sed leo nunc, pharetra et elementum non, faucibus vitae elit. Integer nec libero venenatis libero ultricies molestie semper in tellus. Sed congue et odio sed euismod.&lt;/p&gt;

        &lt;p&gt;Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
          tortor, eget elementum tortor mollis non.&lt;/p&gt;
      &lt;/div&gt;
    &lt;/section&gt;
    &lt;section id=&quot;section3&quot; data-nav=&quot;Section 3&quot;&gt;
      &lt;div class=&quot;landing__container&quot;&gt;
        &lt;h2&gt;Section 3&lt;/h2&gt;
        &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum metus faucibus lectus pharetra dapibus. Suspendisse potenti. Aenean aliquam elementum mi, ac euismod augue. Donec eget lacinia ex. Phasellus imperdiet porta orci eget mollis.
          Sed convallis sollicitudin mauris ac tincidunt. Donec bibendum, nulla eget bibendum consectetur, sem nisi aliquam leo, ut pulvinar quam nunc eu augue. Pellentesque maximus imperdiet elit a pharetra. Duis lectus mi, aliquam in mi quis, aliquam
          porttitor lacus. Morbi a tincidunt felis. Sed leo nunc, pharetra et elementum non, faucibus vitae elit. Integer nec libero venenatis libero ultricies molestie semper in tellus. Sed congue et odio sed euismod.&lt;/p&gt;

        &lt;p&gt;Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
          tortor, eget elementum tortor mollis non.&lt;/p&gt;
      &lt;/div&gt;
    &lt;/section&gt;
  &lt;/main&gt;
  &lt;footer class=&quot;page__footer&quot;&gt;
    &lt;p&gt;&amp;copy Udacity&lt;/p&gt;
  &lt;/footer&gt;

&lt;/body&gt;

<!-- end snippet -->

I'm new to all this stuff and don't know what I'm doing wrong. If anyone can point me in the right direction I would greatly appreciate it.

Thank you,
Michael

答案1

得分: 2

以下是您要翻译的内容:

Tiny error :

for (item in items) : item 是一个 数字(在数组中的索引)。

for (item of items) : item 是数组中的实际元素。

const items = document.querySelectorAll('section');

function isOnScreen(element, buffer) {
  // buffer 是可选的,允许在元素即将出现在屏幕上时返回 true
  buffer = typeof buffer === 'undefined' ? 0 : buffer;
  // 获取元素在视口中的位置
  const bounding = element.getBoundingClientRect();
  // 检查元素是否在视口中
  if (bounding.top >= buffer && bounding.left >= buffer &&
    bounding.right <=
    // 浏览器兼容性的回退
    ((window.innerWidth || document.documentElement.clientWidth) - buffer) &&
    bounding.bottom <=
    ((window.innerHeight || document.documentElement.clientHeight) - buffer)) {
    return true
    console.log(element);
  } else {
    return false;
  };
};

document.addEventListener('scroll', function() {
  for (item of items) {
    isOnScreen(item);
  };
});
<body>
  <!-- HTML 遵循 BEM 命名约定
  ID 仅用于将菜单锚点连接到部分的部分 -->
  <header class="page__header">
    <nav class="navbar__menu">
      <!-- 导航从空的 UL 开始,将由 JS 填充 -->
      <ul id="navbar__list"></ul>
    </nav>
  </header>
  <main>
    <header class="main__hero">
      <h1>Landing Page </h1>
    </header>
    <!-- 每个部分都有一个 ID(用于锚点)和
    将填充 li 节点的数据属性。
    添加更多部分将自动填充导航。
    默认情况下,第一个部分设置为活动类 -->
    <section id="section1" data-nav="Section 1">
      <div class="landing__container">
        <h2>Section 1</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum metus faucibus lectus pharetra dapibus. Suspendisse potenti. Aenean aliquam elementum mi, ac euismod augue. Donec eget lacinia ex. Phasellus imperdiet porta orci eget mollis.
          Sed convallis sollicitudin mauris ac tincidunt. Donec bibendum, nulla eget bibendum consectetur, sem nisi aliquam leo, ut pulvinar quam nunc eu augue. Pellentesque maximus imperdiet elit a pharetra. Duis lectus mi, aliquam in mi quis, aliquam
          porttitor lacus. Morbi a tincidunt felis. Sed leo nunc, pharetra et elementum non, faucibus vitae elit. Integer nec libero venenatis libero ultricies molestie semper in tellus. Sed congue et odio sed euismod.</p>

        <p>Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
          tortor, eget elementum tortor mollis non.</p>
      </div>
    </section>
    <section id="section2" data-nav="Section 2">
      <div class="landing__container">
        <h2>Section 2</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum metus faucibus lectus pharetra dapibus. Suspendisse potenti. Aenean aliquam elementum mi, ac euismod augue. Donec eget lacinia ex. Phasellus imperdiet porta orci eget mollis.
          Sed convallis sollicitudin mauris ac tincidunt. Donec bibendum, nulla eget bibendum consectetur, sem nisi aliquam leo, ut pulvinar quam nunc eu augue. Pellentesque maximus imperdiet elit a pharetra. Duis lectus mi, aliquam in mi quis, aliquam
          porttitor lacus. Morbi a tincidunt felis. Sed leo nunc, pharetra et elementum non, faucibus vitae elit. Integer nec libero venenatis libero ultricies molestie semper in tellus. Sed congue et odio sed euismod.</p>

        <p>Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
          tortor, eget elementum tortor mollis non.</p>
      </div>
    </section>
    <section id="section3" data-nav="Section 3">
      <div class="landing__container">
        <h2>Section 3</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum metus faucibus lectus pharetra dapibus. Suspendisse potenti. Aenean aliquam elementum mi, ac euismod augue. Donec eget lacinia ex. Phasellus imperdiet porta orci eget mollis.
          Sed convallis sollicitudin mauris ac tincidunt. Donec bibendum, nulla eget bibendum consectetur, sem nisi aliquam leo, ut pulvinar quam nunc eu augue. Pellentesque maximus imperdiet elit a pharetra. Duis lectus mi, aliquam in mi quis, aliquam
          porttitor lacus. Morbi a tincidunt felis. Sed leo nunc, pharetra et elementum non, faucibus vitae elit. Integer nec libero venenatis libero ultricies molestie semper in tellus. Sed congue et odio sed euismod.</p>

        <p>Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
          tortor, eget elementum tortor mollis non.</p>
      </div>
    </section>
  </main>
  <footer class="page__footer">
    <p>&copy Udacity</p>
  </footer>
</body>
英文:

Tiny error :

for (item in items) : item is a number (the index in the array).

for (item of items) : item is the actual element in the array.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const items = document.querySelectorAll(&#39;section&#39;);
function isOnScreen(element, buffer) {
//buffer is optional and allows you to return true when  
//the element is going to appear to the screen  
buffer = typeof buffer === &#39;undefined&#39; ? 0 : buffer;
// Get element&#39;s position in the viewport
const bounding = element.getBoundingClientRect();
// Check if element is in the viewport 
if (bounding.top &gt;= buffer &amp;&amp; bounding.left &gt;= buffer &amp;&amp;
bounding.right &lt;=
// fallback for browser compatibility 
((window.innerWidth || document.documentElement.clientWidth) - buffer) &amp;&amp;
bounding.bottom &lt;=
((window.innerHeight || document.documentElement.clientHeight) - buffer)) {
return true
console.log(element);
} else {
return false;
};
};
document.addEventListener(&#39;scroll&#39;, function() {
for (item of items) {
isOnScreen(item);
};
});

<!-- language: lang-html -->

&lt;body&gt;
  &lt;!-- HTML Follows BEM naming conventions 
  IDs are only used for sections to connect menu achors to sections --&gt;
  &lt;header class=&quot;page__header&quot;&gt;
    &lt;nav class=&quot;navbar__menu&quot;&gt;
      &lt;!-- Navigation starts as empty UL that will be populated with JS --&gt;
      &lt;ul id=&quot;navbar__list&quot;&gt;&lt;/ul&gt;
    &lt;/nav&gt;
  &lt;/header&gt;
  &lt;main&gt;
    &lt;header class=&quot;main__hero&quot;&gt;
      &lt;h1&gt;Landing Page &lt;/h1&gt;
    &lt;/header&gt;
    &lt;!-- Each Section has an ID (used for the anchor) and 
    a data attribute that will populate the li node.
    Adding more sections will automatically populate nav.
    The first section is set to active class by default --&gt;
    &lt;section id=&quot;section1&quot; data-nav=&quot;Section 1&quot;&gt;
      &lt;div class=&quot;landing__container&quot;&gt;
        &lt;h2&gt;Section 1&lt;/h2&gt;
        &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum metus faucibus lectus pharetra dapibus. Suspendisse potenti. Aenean aliquam elementum mi, ac euismod augue. Donec eget lacinia ex. Phasellus imperdiet porta orci eget mollis.
          Sed convallis sollicitudin mauris ac tincidunt. Donec bibendum, nulla eget bibendum consectetur, sem nisi aliquam leo, ut pulvinar quam nunc eu augue. Pellentesque maximus imperdiet elit a pharetra. Duis lectus mi, aliquam in mi quis, aliquam
          porttitor lacus. Morbi a tincidunt felis. Sed leo nunc, pharetra et elementum non, faucibus vitae elit. Integer nec libero venenatis libero ultricies molestie semper in tellus. Sed congue et odio sed euismod.&lt;/p&gt;

        &lt;p&gt;Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
          tortor, eget elementum tortor mollis non.&lt;/p&gt;
      &lt;/div&gt;
    &lt;/section&gt;
    &lt;section id=&quot;section2&quot; data-nav=&quot;Section 2&quot;&gt;
      &lt;div class=&quot;landing__container&quot;&gt;
        &lt;h2&gt;Section 2&lt;/h2&gt;
        &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum metus faucibus lectus pharetra dapibus. Suspendisse potenti. Aenean aliquam elementum mi, ac euismod augue. Donec eget lacinia ex. Phasellus imperdiet porta orci eget mollis.
          Sed convallis sollicitudin mauris ac tincidunt. Donec bibendum, nulla eget bibendum consectetur, sem nisi aliquam leo, ut pulvinar quam nunc eu augue. Pellentesque maximus imperdiet elit a pharetra. Duis lectus mi, aliquam in mi quis, aliquam
          porttitor lacus. Morbi a tincidunt felis. Sed leo nunc, pharetra et elementum non, faucibus vitae elit. Integer nec libero venenatis libero ultricies molestie semper in tellus. Sed congue et odio sed euismod.&lt;/p&gt;

        &lt;p&gt;Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
          tortor, eget elementum tortor mollis non.&lt;/p&gt;
      &lt;/div&gt;
    &lt;/section&gt;
    &lt;section id=&quot;section3&quot; data-nav=&quot;Section 3&quot;&gt;
      &lt;div class=&quot;landing__container&quot;&gt;
        &lt;h2&gt;Section 3&lt;/h2&gt;
        &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum metus faucibus lectus pharetra dapibus. Suspendisse potenti. Aenean aliquam elementum mi, ac euismod augue. Donec eget lacinia ex. Phasellus imperdiet porta orci eget mollis.
          Sed convallis sollicitudin mauris ac tincidunt. Donec bibendum, nulla eget bibendum consectetur, sem nisi aliquam leo, ut pulvinar quam nunc eu augue. Pellentesque maximus imperdiet elit a pharetra. Duis lectus mi, aliquam in mi quis, aliquam
          porttitor lacus. Morbi a tincidunt felis. Sed leo nunc, pharetra et elementum non, faucibus vitae elit. Integer nec libero venenatis libero ultricies molestie semper in tellus. Sed congue et odio sed euismod.&lt;/p&gt;

        &lt;p&gt;Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
          tortor, eget elementum tortor mollis non.&lt;/p&gt;
      &lt;/div&gt;
    &lt;/section&gt;
  &lt;/main&gt;
  &lt;footer class=&quot;page__footer&quot;&gt;
    &lt;p&gt;&amp;copy Udacity&lt;/p&gt;
  &lt;/footer&gt;

&lt;/body&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 23:32:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定