首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >卷轴的Javascript eventListener不工作

卷轴的Javascript eventListener不工作
EN

Stack Overflow用户
提问于 2022-08-11 07:50:43
回答 3查看 40关注 0票数 0

我在元素中添加了一个eventListener of‘滚动’,但它不起作用。

如果我在p标记上使用了,然后访问它,它就能正常工作。

问题出在哪里?

谢谢!

代码语言:javascript
复制
const myDiv = document.querySelector('#hScroll')
const customInfo = document.querySelector('#customInfo')

console.log(customInfo)

customInfo.addEventListener('scroll', () => {
    console.log('yup');
})
代码语言:javascript
复制
<div id="hScroll" class="container-fluid asd position-relative ps-3 pe-3 fadeEndB fadeStartB">
  <p id="customInfo" class="custom-info overflow-auto">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown
  </p>
</div>

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-08-11 13:53:07

问题是,我使用的是VUE js,我的标记被定义到带有#app id的div中,如果您这样做,VUE js会以某种方式使addEventListener失效。

溶液

我将我的javascript放入VUE挂载()中,它就能工作了。

票数 0
EN

Stack Overflow用户

发布于 2022-08-11 08:12:01

因为元素中没有滚动事件(即P)。

试试这段代码,你就会发现我的意思。

代码语言:javascript
复制
const myDiv = document.querySelector('#hScroll')
const customInfo = document.querySelector('#customInfo')

console.log(customInfo)

customInfo.addEventListener('scroll', () => {
    console.log('yup');
})
代码语言:javascript
复制
<p id="customInfo" class="custom-info overflow-auto" style="width:60px; height:60px; overflow-y:auto">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown
  </p>

票数 0
EN

Stack Overflow用户

发布于 2022-08-11 08:07:22

回调函数必须接受一个事件参数。

代码语言:javascript
复制
customInfo.addEventListener('scroll', (ev) => {
    console.log('yup');
})
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73317115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档