Vue使用@scroll滚动监听事件没反应

最近在学习vue过程中,写了scroll监听方法,但是发现滚动没有反映。于是,我就网上一查,就出现很多说在生命周期中加document.addEventListener('scroll',function(){})。

这样使用document.addEventListener会破坏vue的统一性,然后自己又试其他方法。发现设置容器的高度。就可以监听到了,代码如下:

html

 

 <div class="eventBox"  @scroll="evenBox">  
<li v-for="item in items" :key="item.id" class="itemList">
    {{item.username}}
  </li>
</div>

 

 

css

html,body{height:100%}
.wrap{background-color:red;height: 100%;width:90%;
}
.eventBox{height:300px;overflow-y:scroll}
.itemList{height:200px;list-style: none; background-color:green}

这里的eventBox容器设置高度为300px,超出的就会出现滚动条。设置固定高度后,我们就能监听到scroll事件了。

 

posted @ 2022-05-02 09:17  前端噜噜  阅读(2645)  评论(0编辑  收藏  举报