Vue列表如何實(shí)現(xiàn)滾動(dòng)到指定位置樣式改變效果
這個(gè)需求大概是這樣子:
我做的一個(gè)聊天Demo,在搜索框搜索用戶,可以滾動(dòng)到指定的用戶。然后成選中狀態(tài)。
這是目前狀態(tài),我搜索南宮仆射 ,想要下面的用戶列表直接滾動(dòng)到南宮仆射并改變CSS樣式。
查詢之后是這個(gè)子:
嗯,我的思路:
在搜索框搜索到用戶之后會(huì)返回一個(gè)用戶對(duì)象,之后會(huì)調(diào)用列表的點(diǎn)擊事件,改變CSS樣式及做一些聊天的邏輯。然后需要獲取到列表對(duì)應(yīng)的id值,直接使用 document.getElementById(it).scrollIntoView();
具體實(shí)現(xiàn):
列表:使用vue的v-for指令 ,這里的id值使用的是遍歷的索引值,外層是一個(gè)自定義滾動(dòng)條組件。樣式也是使用vue指令,一個(gè)語(yǔ)法糖。
<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>
搜索框:這里使用帶提示的輸入框,
<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="請(qǐng)輸入內(nèi)容" @select="handleSelect" > <el-button slot="append" icon="el-icon-search" @click="SearchCurrentSession(SearchHr)"></el-button> </el-autocomplete>
JS代碼:請(qǐng)求為get請(qǐng)求的axios封裝,hr為查詢返回的對(duì)象,hrs為所有的列表對(duì)象。
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) },
頁(yè)面全部代碼:
<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>手機(jī)號(hào)碼:{{user.phone}}</div> <div>電話號(hào)碼:{{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="請(qǐng)輸入內(nèi)容" @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; // 調(diào)用 callback 返回建議列表的數(shù)據(jù) 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; /*這個(gè)是圖片和文字居中對(duì)齊*/ 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 { /*注意這個(gè)是.不是冒號(hào):*/ 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>
總結(jié)
到此這篇關(guān)于Vue列表實(shí)現(xiàn)滾動(dòng)到指定位置樣式改變的文章就介紹到這了,更多相關(guān)Vue列表實(shí)現(xiàn)滾動(dòng)到指定位置樣式改變內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue中使用Cesium加載shp文件、wms服務(wù)、WMTS服務(wù)問(wèn)題
- Vue2中配置Cesium全過(guò)程
- cesium開發(fā)之如何在vue項(xiàng)目中使用cesium,使用離線地圖資源
- vue中安裝使用cesium方式(親測(cè)可用)
- 使用開源Cesium+Vue實(shí)現(xiàn)傾斜攝影三維展示功能
- vue2.0項(xiàng)目集成Cesium的實(shí)現(xiàn)方法
- vue實(shí)現(xiàn)滾動(dòng)條到頂部或者到指定位置
- vue keep-alive列表頁(yè)緩存 詳情頁(yè)返回上一頁(yè)不刷新,定位到之前位置
- vue?cesium加載點(diǎn)與定位到指定位置的實(shí)現(xiàn)方法
相關(guān)文章
vue項(xiàng)目如何使用three.js實(shí)現(xiàn)vr360度全景圖片預(yù)覽
這篇文章主要介紹了vue項(xiàng)目如何使用three.js實(shí)現(xiàn)vr360度全景圖片預(yù)覽,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03Vue使用v-viewer實(shí)現(xiàn)圖片預(yù)覽
這篇文章主要為大家詳細(xì)介紹了Vue使用v-viewer實(shí)現(xiàn)圖片預(yù)覽,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10vue?require.context()的用法實(shí)例詳解
require.context是webpack提供的一個(gè)api,通常用于批量注冊(cè)組件,下面這篇文章主要給大家介紹了關(guān)于vue?require.context()用法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04vue在mounted中window.onresize不生效問(wèn)題及解決
這篇文章主要介紹了vue中在mounted中window.onresize不生效問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04JavaScript實(shí)現(xiàn)簡(jiǎn)單的圖片切換功能(實(shí)例代碼)
這篇文章主要介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單的圖片切換功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-04-04如何利用vue3實(shí)現(xiàn)一個(gè)俄羅斯方塊
俄羅斯方塊這個(gè)游戲相信大家都玩過(guò),下面這篇文章主要給大家介紹了關(guān)于如何利用vue3實(shí)現(xiàn)一個(gè)俄羅斯方塊的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01