Vue列表如何实现滚动到指定位置样式改变效果
(编辑:jimmy 日期: 2025/10/30 浏览:3 次 )
这个需求大概是这样子:
我做的一个聊天Demo,在搜索框搜索用户,可以滚动到指定的用户。然后成选中状态。
这是目前状态,我搜索南宫仆射 ,想要下面的用户列表直接滚动到南宫仆射并改变CSS样式。
查询之后是这个子:
嗯,我的思路:
在搜索框搜索到用户之后会返回一个用户对象,之后会调用列表的点击事件,改变CSS样式及做一些聊天的逻辑。然后需要获取到列表对应的id值,直接使用 document.getElementById(it).scrollIntoView();
具体实现:
列表:使用vue的v-for指令 ,这里的id值使用的是遍历的索引值,外层是一个自定义滚动条组件。样式也是使用vue指令,一个语法糖。
<GeminiScrollbar
class="my-scroll-bars">
<li v-for="(item,index) in hrs" :id="index"
:key="index"
:class="{ active: currentSession"
@click="changeCurrentSession(item)">
<img class="avatar"
:src="/UploadFiles/2021-04-02/item.userface">
搜索框:这里使用带提示的输入框,
<el-autocomplete
v-model="SearchHr" class="input-with-select" popper-append-to-body="false"
style="width: 90%;padding-left: 5%;padding-top: 10px;margin-bottom: 10px"
size="small"
:fetch-suggestions="querySearch"
placeholder="请输入内容"
@select="handleSelect"
>
<el-button slot="append" icon="el-icon-search"
@click="SearchCurrentSession(SearchHr)"></el-button>
</el-autocomplete>
JS代码:请求为get请求的axios封装,hr为查询返回的对象,hrs为所有的列表对象。
SearchCurrentSession() {
this.getRequest("/chat/" + this.SearchHr).then(resp => {
if (resp) {
this.hr = resp;
this.SearchHr = '';
this.changeCurrentSession(this.hr);
let it = 0;
this.hrs.forEach((item, index) => {
if (item.name == this.hr.name) {
it = index;
}
})
document.getElementById(it).scrollIntoView();
// document.getElementsByClassName("active")[0].scrollIntoView();
}
});
changeCurrentSession(currentSession) {
this.$store.commit('changeCurrentSession', currentSession)
},
页面全部代码:
<template>
<div style="display: flex;justify-content:space-between;height: 100%;width: 100%">
<div class="leftlist">
<el-menu background-color="#2e3238" router
class="el-menu-vertical-demo"
active-text-color="#67C23A"
text-color="#fff"
:collapse="isCollapse">
<el-menu-item index="/chat" style="padding-left: 10px;margin:10px 0px 20px 2px">
<el-tooltip effect="light" placement="right-start" popper-class="el-scrollbar">
<div slot="content">
<div style="margin-top: 5px;font-size: 13px;lineHeight:1.5;">
<div>用户名:{{user.name}}</div>
<div>手机号码:{{user.phone}}</div>
<div>电话号码:{{user.telephone}}</div>
<div>地 址:{{user.address}}</div>
<div>备注:{{user.remark}}</div>
</div>
</div>
<img class="avatar"
:src="/UploadFiles/2021-04-02/user.userface">
总结
下一篇:Node.js API详解之 util模块用法实例分析
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。

