无法添加 CSS 动画来列出 touchstart 事件上的项目

发布于 2025-01-09 08:35:03 字数 2008 浏览 4 评论 0原文

当检测到触摸启动事件时,我会发生一些简单的 CSS 动画,但是在尝试将类添加到 div 元素时,不断出现“未定义不是对象”。请在移动设备上检查此项,因为它是通过触摸事件激活的。我想向 div 内的所有 li 项目添加一个具有新值的类。在添加类之前我尝试控制台记录每个列表项并在控制台中打印。这非常简单,但我不明白为什么只有在添加类时才会出现此错误。动画的 CSS 只需要出现在移动设备上,因此我在媒体查询中添加了这些新类。

这是工作网站

container.addEventListener('touchstart', handlePanStart, false);

function handlePanStart() {
  const target = document.querySelectorAll('.top')
  let yOffset = window.pageYOffset;
  console.log('start', 'ypos: ' + yOffset)

  target.forEach((item) => {
    console.log(item.classList)
    item.classlist.add('longer')

  })
}
ul {
  height: 50vh;
  flex-direction: column;
  transform: translateY(0);
}

.top {
  height: 5px;
  width: 95vw;
  transition: all 2s ease-out;
}

.top img {
  width: 100%;
  height: auto;
  object-fit: cover;
  border: 2px solid black;
  pointer-events: none;
}

.longer {
  height: 205px;
}
<ul>
  <li class="top"><img src="https://www.fillmurray.com/640/360"></li>
  <li class="top"><img src="https://www.fillmurray.com/640/360"></li>
  <li class="top"><img src="https://www.fillmurray.com/640/360"></li>
  <li class="top"><img src="https://www.fillmurray.com/640/360"></li>
  <li class="top"><img src="https://www.fillmurray.com/640/360"></li>
  <li class="top"><img src="https://www.fillmurray.com/640/360"></li>
  <li class="top"><img src="https://www.fillmurray.com/640/360"></li>
  <li class="top"><img src="https://www.fillmurray.com/640/360"></li>
  <li class="top"><img src="https://www.fillmurray.com/640/360"></li>
</ul>

I have some simple CSS animation to occur when a touch start event is detected however keep getting "undefined is not an object" when trying to add a class to a div element. Please check this on mobile as it's activated through a touch event. I want to add a class with new value to all the li items inside a div. I tried console logging each list item before adding the class and prints in the console fine. it's pretty straightforward but i cant figure out why I'm getting this error only when adding the class. The CSS for animations needs to only occur on mobile so i added those new class in the media queries.

Here's the working website.

container.addEventListener('touchstart', handlePanStart, false);

function handlePanStart() {
  const target = document.querySelectorAll('.top')
  let yOffset = window.pageYOffset;
  console.log('start', 'ypos: ' + yOffset)

  target.forEach((item) => {
    console.log(item.classList)
    item.classlist.add('longer')

  })
}
ul {
  height: 50vh;
  flex-direction: column;
  transform: translateY(0);
}

.top {
  height: 5px;
  width: 95vw;
  transition: all 2s ease-out;
}

.top img {
  width: 100%;
  height: auto;
  object-fit: cover;
  border: 2px solid black;
  pointer-events: none;
}

.longer {
  height: 205px;
}
<ul>
  <li class="top"><img src="https://www.fillmurray.com/640/360"></li>
  <li class="top"><img src="https://www.fillmurray.com/640/360"></li>
  <li class="top"><img src="https://www.fillmurray.com/640/360"></li>
  <li class="top"><img src="https://www.fillmurray.com/640/360"></li>
  <li class="top"><img src="https://www.fillmurray.com/640/360"></li>
  <li class="top"><img src="https://www.fillmurray.com/640/360"></li>
  <li class="top"><img src="https://www.fillmurray.com/640/360"></li>
  <li class="top"><img src="https://www.fillmurray.com/640/360"></li>
  <li class="top"><img src="https://www.fillmurray.com/640/360"></li>
</ul>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海未深 2025-01-16 08:35:03

classlist 更改为 classList (驼峰式大小写)。 classlist 在 DOM 中未定义。

Change classlist to classList (camel-case). classlist is undefined in the DOM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文