无法添加 CSS 动画来列出 touchstart 事件上的项目
当检测到触摸启动事件时,我会发生一些简单的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
classlist
更改为classList
(驼峰式大小写)。classlist
在 DOM 中未定义。Change
classlist
toclassList
(camel-case).classlist
is undefined in the DOM.