这个需求大概是这样子:

我做的一个聊天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?item.username === currentSession.username:false}"
@click="changeCurrentSession(item)">
<img class="avatar"
:src="item.userface">
<el-badge :is-dot="isDot[user.username+'#'+item.username]">
<p class="name">{{item.name}}</p>
</el-badge>
</li>
</GeminiScrollbar>

搜索框:这里使用带提示的输入框,

 @click="SearchCurrentSession(SearchHr)为查询方法。
  <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/?name=" + 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="user.userface"
:alt="user.name"></el-tooltip>
</el-menu-item>
<el-menu-item index="/chat" style="padding-left: 15px">
<i class="fa fa-weixin fa-2x"></i> </el-menu-item>
<el-menu-item index="/chat" style="padding-left: 15px">
<i class="fa fa-windows fa-2x"></i> </el-menu-item>
<el-menu-item index="/chat" style="padding-left: 15px">
<i class="fa fa-modx fa-2x"></i> </el-menu-item>
<el-menu-item index="/chat" style="padding-left: 15px">
<i class="fa fa-share-alt fa-2x"></i> </el-menu-item>
</el-menu> </div>
<div id="list"> <div style="height:100%;width:100%;overflow-x: hidden"> <ul style="padding-left: 0px; overflow-x: hidden;">
<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> <GeminiScrollbar
class="my-scroll-bars">
<li v-for="(item,index) in hrs" :id="index"
:key="index"
:class="{ active: currentSession?item.username === currentSession.username:false}"
@click="changeCurrentSession(item)">
<img class="avatar"
:src="item.userface">
<el-badge :is-dot="isDot[user.username+'#'+item.username]">
<p class="name">{{item.name}}</p>
</el-badge>
</li>
</GeminiScrollbar>
</ul>
</div> </div>
</div>
</template> <script>
import {mapState} from 'vuex'
export default {
name: 'list',
data() {
return {
isCollapse: true,
SearchHr: '',
hr: "",
restaurants: [],
user: JSON.parse(window.sessionStorage.getItem("user"))
}
},
computed: {
...mapState([
'hrs',
'isDot',
'currentSession'
])
},
methods: {
SearchCurrentSession() {
this.getRequest("/chat/?name=" + 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(); }
}); },
querySearch(queryString, cb) {
this.restaurants = this.loadAll();
let restaurants = [];
this.restaurants.forEach(value => {
let {name, username} = value;
let restaurant = {value: name, username: username}
restaurants.push(restaurant);
});
var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
// 调用 callback 返回建议列表的数据
cb(results);
},
createFilter(queryString) {
return (SearchHr) => {
return (SearchHr.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
};
},
loadAll() {
return this.hrs;
},
changeCurrentSession(currentSession) {
this.$store.commit('changeCurrentSession', currentSession)
},
handleSelect(item) {
console.log(item);
}
},
mounted() {
this.$store.dispatch('initData');
}
}
</script> <style lang="scss" scoped> .my-scroll-bars {
height: 610px;
} /* override gemini-scrollbar default styles */ /* vertical scrollbar track */
.gm-scrollbar.-vertical {
background-color: #f0f0f0
} /* horizontal scrollbar track */
.gm-scrollbar.-horizontal {
background-color: transparent;
} /* scrollbar thumb */
.gm-scrollbar .thumb {
background-color: rebeccapurple;
} .gm-scroll-view {
overflow-x: hidden;
} .gm-scrollbar .thumb:hover {
background-color: fuchsia;
} input-with-select {
margin-top: 50px;
padding-top: 20px; } .el-scrollbar__wrap {
width: 100%;
height: 100%;
overflow-x: hidden;
} .el-menu-item is-active {
padding-left: 10px; } .el-menu-vertical-demo {
background-color: #2e3238;
width: 80px;
height: 100%;
/*opacity:0.8;*/ } .leftlist {
background-color: transparent;
width: 90px;
height: 700px;
overflow-x: hidden;
} .avatar {
width: 45px;
height: 45px;
/*这个是图片和文字居中对齐*/
border-radius: 5px;
margin-top: 5px;
} .el-scrollbar__wrap {
background-color: #E4E7ED;
} #el-scrollbar {
background-color: #E4E7ED;
} #list {
background-color: #E4E7ED;
width: 100%;
overflow-x: hidden; li {
padding: 7px 15px;
border-bottom: 1px solid #E4E7ED;
cursor: pointer;
list-style: none;
color: #505458; &:hover {
background-color: rgba(0, 0, 0, 0.07);
}
} li.active {
/*注意这个是.不是冒号:*/
background-color: rgba(0, 0, 0, 0.1);
} .avatar {
border-radius: 2px;
width: 30px;
height: 30px;
vertical-align: middle;
} .name {
display: inline-block;
margin-left: 15px;
margin-top: 0px;
margin-bottom: 0px;
}
}
</style>

Vue列表实现滚动到指定位置样式改变的更多相关文章

  1. Vue如何引入jquery实现平滑滚动到指定位置效果

    在以往的做法里首选jquery的animate实现,但是Vue里并没有这个方法.如何在Vue项目中实现点击导航平滑滚动到指定位置,为了这效果我是快要崩溃了,上网查阅了很久发现并没有真正意义上解决这个问 ...

  2. js滚动到指定位置

    序言:在网络上百度,关键字:“js div滚动到指定位置”,结果基本上大同小异!各种大神都给我们总结出来了四种滚动到指定位置的办法,可惜再下愚钝,每个都不会用,所以写了一个超级简单的方法来使初学者一看 ...

  3. 自定义ScrollViewer的Touch事件--触摸上下移动ScrollViewer滚动到指定位置

    double mPointY;//触摸点的Y坐标 double mOffsetY;//滚动条当前位置 bool mIsTouch = false;//是否触摸 //触摸事件 private void ...

  4. 通过scrollTop,使子元素滚动至指定位置

    想实现这样的一个功能,点击子元素,让元素滚动至指定位置,怎么实现呢? 在代码实现之前,先了解下相关关键点. 1.scrollHeight 属性 通过 scrollHeight 属性可获得子元素的滚动高 ...

  5. ios开发之--令UITableView滚动到指定位置

    这个应用场景还是挺多的,代码如下: //获取到需要跳转位置的行数 NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow: inSect ...

  6. selenium webdriver——JS滚动到指定位置

    1.DOM滚动方法 1.scrollIntoView(alignWithTop)  滚动浏览器窗口或容器元素,以便在当前视窗的可见范围看见当前元素.如果alignWithTop为true,或者省略它, ...

  7. 利用jquery制作滚动到指定位置触发动画

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>利用 ...

  8. jquery滚动到指定位置

    利用jquery实现页面可视区滚动到指定位置.直接上代码 //滚动到指定位置 function scrollTo(element,speed) { if(!speed){ speed = 300; } ...

  9. js 获取滚动位置,滚动到指定位置,平滑滚动

    1.获取当前滚动条位置信息 var top = dom.scrollTop; // 获取y轴上的滚动位置 var left = dom.scrollLeft; // 获取x轴上的滚动位置 2.滚动到指 ...

随机推荐

  1. 数据结构和算法(Golang实现)(8.2)基础知识-分治法和递归

    分治法和递归 在计算机科学中,分治法是一种很重要的算法. 字面上的解释是分而治之,就是把一个复杂的问题分成两个或更多的相同或相似的子问题. 直到最后子问题可以简单的直接求解,原问题的解即子问题的解的合 ...

  2. AJ学IOS 之CoreLocation地理编码小Demo输入城市得到经纬度

    AJ分享,必须精品 一:效果 输入地名,可以得到相应的经纬度,知识为了学习写的小Demo 二:实现步骤 一 :首先获取用户输入的位置. 二 :创建地理编码对象. 三 :利用地理编码对象编码,根据传入的 ...

  3. Connections in Galaxy War ZOJ - 3261 (并查集)

    点权并查集的反向离线操作 题目大意:有n个stars,每一个都一定的“颜值”.然后stars与stars之间可以相连,query c表示再与c相连的stars中,颜值比c高的,stars的标号,如果有 ...

  4. vue2.x学习笔记(八)

    接着前面的内容:https://www.cnblogs.com/yanggb/p/12577433.html. 列表渲染 vue提供了一个[v-for]指令用于列表渲染(循环). 用[v-for]指令 ...

  5. Springboot:修改默认端口以及Logo(三)

    端口修改 在application.yml文件中增加端口的配置: server: port: 8081 Logo修改 Logo生成网址: https://www.bootschool.net/asci ...

  6. SpringMVC数据传递及乱码问题

    基础环境搭建请参考SringMVC入门程序 一.SpringMVC数据处理 1:resful 路径传值 http://localhost/get/1/2 /* http://localhost/get ...

  7. php正则验证手机、邮箱

    //验证电话private function reg_phone($phone){        if (preg_match("/^13[0-9]{1}[0-9]{8}$|15[0189] ...

  8. 2019-2020-1 20199329《Linux内核原理与分析》第十三周作业

    <Linux内核原理与分析>第十三周作业 一.本周内容概述 通过重现缓冲区溢出攻击来理解漏洞 二.本周学习内容 1.实验简介 注意:实验中命令在 xfce 终端中输入,前面有 $ 的内容为 ...

  9. [Qt]执行cmd命令

    要加 /c 参数 QProcess p; p.start("cmd", QStringList()<<"/c"<<"ping ...

  10. 【集群实战】NFS服务常见故障排查和解决方法

    NFS,全名叫Network File System,中文叫网络文件系统,是Linux.UNIX系统的分布式文件系统的一个组成部分,可实现在不同网络上共享远程文件系统. NFS由Sun公司开发,目前已 ...