javascript addEventListener以在div中滚动

7gyucuyw  于 2023-01-11  发布在  Java
关注(0)|答案(2)|浏览(170)

我试图通过类或id来滚动div,但是我不能使它工作。console.log(滚动)没有激活控制台中的任何东西。有人能帮帮我吗?
我使用的JS是

document.getElementsByClassName('.site-content').addEventListener('scroll', () => {
  
  const scrolled = document.getElementsByClassName('.site-content').scrollY;
  
  console.log(scrolled);

});

我所在区域的CSS是

.site-content{
width:100vh;
  height:100vw;
  overflow-x: hidden;
  overflow-y: scroll;
  transform: rotate(-90deg) translateX(-100vh);
  transform-origin: top left; 
  position:absolute;
  scrollbar-width:none;
  -ms-overflow-style:none;
}
t8e9dugd

t8e9dugd1#

你有几个问题:
1.返回一个HTMLCollection(类似于数组),而不是单个元素,因此不能在其上使用addEventListener()。可以使用querySelector(),它将返回单个element。请注意,使用querSelector()是比.getElementsByClassName(class)[0]更优化的获取单个元素的方法。.getElementsByClassName()方法将在整个DOM树中搜索包含该类的元素,然后使用[0]获取搜索完成后找到的第一个元素(丢弃它找到的所有其他东西)。而querySelector()将在DOM中搜索您的元素,并在找到与类匹配的元素时立即停止搜索,它不会像.getElementsByClassName()那样继续寻找更多的元素。

  1. scrollY不是从.querySelector()返回的element的属性,而是可以使用.scrollTop属性。
    1.您的脚本在页面加载之前运行,这意味着您的脚本将无法找到您试图使用querySelector()获取的HTML元素。有几种方法可以解决这个问题,一种方法是将包含您代码的<script></script>标记移到</body>结束标记之前,以便它只在HTML元素加载后运行。
    1.最后,每次使用.querySelector()时都要查询DOM,其中一次是在添加事件侦听器时,然后是在每次发生scroll事件时(这是很多次!)。由于查询DOM是一个开销很大的操作,所以最好将其保持在最小值。您可以改为查询DOM一次来获取您的元素,并在scroll事件的回调中使用该元素引用。
    1.虽然这不是一个bug,但你可以通过添加{pasive: true}选项来提高触摸设备上的滚动性能。
    解决上述问题:
const scrollContent = document.querySelector('.site-content'); // store in a variable so we can reference the element in multiple locations
scrollContent.addEventListener('scroll', () => {
  const scrolled = scrollContent.scrollTop; // reuse `scrollContent` innstead of querying the DOM again
  console.log(scrolled);
}, {passive: true});
xfb7svmp

xfb7svmp2#

你必须选择HTMLCollection中的第一个元素,并且没有scrollY,可能是window.scrollY下面是一个如何获取滚动位置的示例,我添加了高度:300px到你的css,所以它滚动

document
  .getElementsByClassName("site-content")[0]
  .addEventListener("scroll", () => {
    const scrolled = document.getElementsByClassName("site-content")[0]
      .scrollTop;
    console.log(scrolled);
  });
.site-content {
  width: 100vh;
  height: 100vw;
  overflow-x: hidden;
  overflow-y: scroll;
  transform: rotate(-90deg) translateX(-100vh);
  transform-origin: top left;
  position: absolute;
  height: 300px;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
<div id="content" class="site-content">

  <div id="primary" class="content-area grid">
    <main id="main" class="site-main">

      <article id="post-7" class="post-7 page type-page status-publish hentry">
        <header class="entry-header">
        </header><!-- .entry-header -->

        <div class="entry-content">
          <div data-elementor-type="wp-page" data-elementor-id="7" class="elementor elementor-7" data-elementor-settings="[]">
            <div class="elementor-section-wrap">
              <section class="elementor-section elementor-top-section elementor-element elementor-element-f7eb723 elementor-section-full_width elementor-section-height-full section elementor-section-height-default elementor-section-items-middle" data-id="f7eb723" data-element_type="section">
                <div class="elementor-container elementor-column-gap-no">
                  <div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-f8514aa" data-id="f8514aa" data-element_type="column">
                    <div class="elementor-widget-wrap elementor-element-populated">
                      <div class="elementor-element elementor-element-2b50864 elementor-widget elementor-widget-image" data-id="2b50864" data-element_type="widget" data-widget_type="image.default">
                        <div class="elementor-widget-container">
                          <img src="https://jakobnatorp.com/wp-content/uploads/2021/10/LAVA-1-1024x512.png" class="attachment-large size-large" alt="" loading="lazy" srcset="https://jakobnatorp.com/wp-content/uploads/2021/10/LAVA-1-1024x512.png 1024w, https://jakobnatorp.com/wp-content/uploads/2021/10/LAVA-1-300x150.png 300w, https://jakobnatorp.com/wp-content/uploads/2021/10/LAVA-1-768x384.png 768w, https://jakobnatorp.com/wp-content/uploads/2021/10/LAVA-1-1536x768.png 1536w, https://jakobnatorp.com/wp-content/uploads/2021/10/LAVA-1-1000x500.png 1000w, https://jakobnatorp.com/wp-content/uploads/2021/10/LAVA-1.png 1920w" sizes="(max-width: 640px) 100vw, 640px" width="640" height="320">
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </section>
              <section class="elementor-section elementor-top-section elementor-element elementor-element-55da7b6 elementor-section-full_width elementor-section-height-full section elementor-section-height-default elementor-section-items-middle" data-id="55da7b6" data-element_type="section">
                <div class="elementor-container elementor-column-gap-no">
                  <div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-fd3a999" data-id="fd3a999" data-element_type="column">
                    <div class="elementor-widget-wrap elementor-element-populated">
                      <div class="elementor-element elementor-element-2d0d5b0 elementor-widget elementor-widget-image" data-id="2d0d5b0" data-element_type="widget" data-widget_type="image.default">
                        <div class="elementor-widget-container">
                          <img src="https://jakobnatorp.com/wp-content/uploads/2021/10/FLAAET-1-1024x512.png" class="attachment-large size-large" alt="" loading="lazy" srcset="https://jakobnatorp.com/wp-content/uploads/2021/10/FLAAET-1-1024x512.png 1024w, https://jakobnatorp.com/wp-content/uploads/2021/10/FLAAET-1-300x150.png 300w, https://jakobnatorp.com/wp-content/uploads/2021/10/FLAAET-1-768x384.png 768w, https://jakobnatorp.com/wp-content/uploads/2021/10/FLAAET-1-1536x768.png 1536w, https://jakobnatorp.com/wp-content/uploads/2021/10/FLAAET-1-1000x500.png 1000w, https://jakobnatorp.com/wp-content/uploads/2021/10/FLAAET-1.png 1920w" sizes="(max-width: 640px) 100vw, 640px" width="640" height="320">
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </section>
            </div>
          </div>
        </div><!-- .entry-content -->

      </article><!-- #post-7 -->

    </main><!-- #main -->
  </div><!-- #primary -->

</div>

相关问题