vue中如何動(dòng)態(tài)獲取剩余區(qū)域的滾動(dòng)高度
vue動(dòng)態(tài)獲取剩余區(qū)域的滾動(dòng)高度
需求
通過(guò)獲取屏幕的高度,減去【固定】高度,剩下的高度為【內(nèi)容】滾動(dòng)高度。
我們通過(guò)下面代碼獲取屏幕高度:
document.documentElement.clientHeight || document.body.clientHeight;
所謂的固定高度,應(yīng)該是一直停留在屏幕可見(jiàn)范圍中,不根據(jù)內(nèi)容進(jìn)行滾動(dòng)。
如果這個(gè)固定高度是一些靜態(tài)資源時(shí),我們只需通過(guò)設(shè)置 ref 獲取元素的高度。
<div class="index-banner-img"> ? <img :src="bannerImg" ref="imgSize" alt=""> </div>
let imgHeight = this.$refs['imgSize'].height
那么這樣就很容易的獲取到內(nèi)容的滾動(dòng)高度,代碼如下:
getScollerHeight() {
? setTimeout(() => {
? ? let clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
? ? let imgHeight = this.$refs['imgSize'].height
? ? this.scrollHeight = (clientHeight - imgHeight) + 'px'
? }, 100)
}
mounted() {
? this.getScollerHeight()
},vue獲取元素高度總是不準(zhǔn)確問(wèn)題
打算用一個(gè)websocket寫(xiě)一個(gè)簡(jiǎn)易的聊天系統(tǒng)。
后端代碼很容易就寫(xiě)好,但是前端是真的難搞,遇到一個(gè)很?chē)?yán)重的問(wèn)題:
當(dāng)發(fā)送一條消息或者是收到一條消息,消息展示界面不能滑到最下面,展示最新消息,于是,經(jīng)過(guò)一段時(shí)間的修改,發(fā)送新消息時(shí),滾動(dòng)條雖然能下滑,但是滑不到最底部,于是我添加了一個(gè)按鈕,使用按鈕,將滾動(dòng)條滑到最底部是可行的。又使用debug調(diào)試,發(fā)現(xiàn):vue會(huì)先執(zhí)行你的其它方法,再渲染頁(yè)面,導(dǎo)致總是只能滑到上一條消息展示的高度。
于是我再百度,發(fā)現(xiàn):重置數(shù)據(jù)后,獲取dom元素高時(shí),dom元素還未渲染完畢,(可能這就是為什么只能滑到上一條消息展示的地方)
解決辦法
this.$nextTick()函數(shù) :在下次DOM更新循環(huán)結(jié)束之后執(zhí)行延遲回調(diào)
this.$nextTick(()=>{?
? ? ?this.goBottom(); // 滾動(dòng)條滑到底部的方法
? ?})補(bǔ)充: 使用vue獲取一個(gè)指定的元素的高度,可以使用vue的ref
當(dāng)ref加在普通的元素上,使用this.ref.name獲取到的是dom元素
下面是聊天的html代碼
<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <title>微聊</title>
? ? <script src="../static/js/vue.js"></script>
? ? <style>
? ? ? ? .cheet-box{
? ? ? ? ? ? width: 592px;
? ? ? ? ? ? height: 160px;
? ? ? ? }
? ? ? ? .box{
? ? ? ? ? ? /*margin: 0 auto;*/
? ? ? ? ? ? /*overflow:auto;*/
? ? ? ? ? ? overflow-y: auto;
? ? ? ? ? ? overflow-x: hidden;
? ? ? ? ? ? font-family: "微軟雅黑 Light";
? ? ? ? ? ? width: 600px;
? ? ? ? ? ? height: 300px;
? ? ? ? ? ? background-color: #ecece9;
? ? ? ? ? ? border: none;
? ? ? ? ? ? box-shadow: aliceblue;
? ? ? ? ? ? margin-bottom: 20px;
? ? ? ? ? ? padding: 50px;
? ? ? ? }
? ? ? ? .to,.me{
? ? ? ? ? ? word-wrap:break-word;
? ? ? ? ? ? display: block;
? ? ? ? ? ? width: 50%;
? ? ? ? ? ? padding: 26px;
? ? ? ? ? ? border-radius: 10px;
? ? ? ? ? ? background-color: #fff;
? ? ? ? ? ? margin: 5px 0 10px 0;
? ? ? ? }
? ? ? ? .system-log{
? ? ? ? ? ? padding: 5px 0;
? ? ? ? ? ? color: darkgrey;
? ? ? ? ? ? text-align: center;
? ? ? ? }
? ? ? ? .to{
? ? ? ? ? ? float: left;
? ? ? ? }
? ? ? ? .me{
? ? ? ? ? ? float: right;
? ? ? ? }
? ? </style>
</head>
<body onbeforeunload="checkLeave()">
? ? <div id="app">
? ? ? ? <div class="box" id="box" ref="getHeight">
? ? ? ? ? ? <div v-for="item in messageArray">
<!-- ? ? ? ? ? ? ? ?<div class="system-log">連接成功...</div>-->
? ? ? ? ? ? ? ? <div class="to" v-if="item.username != message.username" v-text="item.text">
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? <div class="me" v-else ?v-text="item.text">
? ? ? ? ? ? ? ? ? ? aaaaaa
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? </div>
? ? ? ? </div>
? ? ? ? <div>
? ? ? ? ? ? <input type="text" v-model="message.username">
? ? ? ? </div>
? ? ? ? <div>
? ? ? ? ? ? <textarea type="text" v-model="message.text" class="cheet-box"></textarea>
? ? ? ? ? ? <input @click="sendMessage()" type="button" value="發(fā)送"/>
? ? ? ? ? ? <input @click="goBottom()" type="button" value="底部"/>
? ? ? ? </div>
? ? </div>
? ? <script>
? ? ? ? function checkLeave(){
? ? ? ? ? ? sessionStorage.setItem('key','hello');
? ? ? ? ? ? localStorage.setItem('2','3')
? ? ? ? }
? ? ? ? var websocket ;
? ? ? ? var vm = new Vue({
? ? ? ? ? ?el:'#app',
? ? ? ? ? ? created(){
? ? ? ? ? ? ? ? this.initWebSocket();
? ? ? ? ? ? },
? ? ? ? ? ? data:{
? ? ? ? ? ? ? ? ? ? message:{
? ? ? ? ? ? ? ? ? ? ? ? username:'',
? ? ? ? ? ? ? ? ? ? ? ? text:'',
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? messageArray:[
? ? ? ? ? ? ? ? ],
? ? ? ? ? ? },
? ? ? ? ? ? methods:{
? ? ? ? ? ? ? ?initWebSocket(){
? ? ? ? ? ? ? ? ? ?if (typeof (WebSocket)=="undefined"){
? ? ? ? ? ? ? ? ? ? ? ?alert('瀏覽器不支持WebSocket')
? ? ? ? ? ? ? ? ? ?}else {
? ? ? ? ? ? ? ? ? ? ? ?console.log('瀏覽器支持websocket')
? ? ? ? ? ? ? ? ? ? ? ?websocket = new WebSocket("ws://localhost:8080/ws/asset");
? ? ? ? ? ? ? ? ? ? ? ?//連接打開(kāi)事件
? ? ? ? ? ? ? ? ? ? ? ?websocket.onopen = function() {
? ? ? ? ? ? ? ? ? ? ? ? ? ?console.log("Socket 已打開(kāi)");
? ? ? ? ? ? ? ? ? ? ? ? ? ?var obj = {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?text:'',
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?username: '',
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?log:'連接成功!'
? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ? ? ?websocket.send(JSON.stringify(obj));
? ? ? ? ? ? ? ? ? ? ? ?};
? ? ? ? ? ? ? ? ? ? ? ?//收到消息事件
? ? ? ? ? ? ? ? ? ? ? ?websocket.onmessage = function(msg) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?vm.pushArray(msg.data)
? ? ? ? ? ? ? ? ? ? ? ?};
? ? ? ? ? ? ? ? ? ? ? ? ? ?//連接關(guān)閉事件
? ? ? ? ? ? ? ? ? ? ? ?websocket.onclose = function() {
? ? ? ? ? ? ? ? ? ? ? ? ? ?console.log("Socket已關(guān)閉");
? ? ? ? ? ? ? ? ? ? ? ?};
? ? ? ? ? ? ? ? ? ? ? ?//發(fā)生了錯(cuò)誤事件
? ? ? ? ? ? ? ? ? ? ? ?websocket.onerror = function() {
? ? ? ? ? ? ? ? ? ? ? ? ? ?alert("Socket發(fā)生了錯(cuò)誤");
? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ?//窗口關(guān)閉時(shí),關(guān)閉連接
? ? ? ? ? ? ? ? ? ? ? ?window.unload=function() {
? ? ? ? ? ? ? ? ? ? ? ? ? ?websocket.close();
? ? ? ? ? ? ? ? ? ? ? ?};
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?},
? ? ? ? ? ? ? ? sendMessage(){
? ? ? ? ? ? ? ? ? ? websocket.send(JSON.stringify(this.message));
? ? ? ? ? ? ? ? ? ? this.message.text = ''
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? pushArray(msg){
? ? ? ? ? ? ? ? ? ? let message = JSON.parse(msg);
? ? ? ? ? ? ? ? ? ? console.log(message)
? ? ? ? ? ? ? ? ? ? if (message.username!='' && message.text!=''){
? ? ? ? ? ? ? ? ? ? ? ? this.messageArray.push(message)
? ? ? ? ? ? ? ? ? ? ? ? this.$nextTick(()=>{
? ? ? ? ? ? ? ? ? ? ? ? ? ? this.goBottom();
? ? ? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? goBottom(){
? ? ? ? ? ? ? ? ? ? let box = document.getElementById('box');
? ? ? ? ? ? ? ? ? ? box.getBoundingClientRect().height
? ? ? ? ? ? ? ? ? ? box.scrollTo(0,box.scrollHeight-box.clientHeight)
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? })
? ? </script>
</body>
</html>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue實(shí)現(xiàn)電梯樣式錨點(diǎn)導(dǎo)航效果流程詳解
這篇文章主要介紹了Vue實(shí)現(xiàn)電梯樣式錨點(diǎn)導(dǎo)航效果流程,這種導(dǎo)航效果廣泛應(yīng)用于商城點(diǎn)餐購(gòu)物情景,文中通過(guò)示例代碼介紹的很詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-05-05
如何在Vue單頁(yè)面中進(jìn)行業(yè)務(wù)數(shù)據(jù)的上報(bào)
為什么要在標(biāo)題里加上一個(gè)業(yè)務(wù)數(shù)據(jù)的上報(bào)呢,因?yàn)樵谠蹅兦岸隧?xiàng)目中,可上報(bào)的數(shù)據(jù)維度太多,比如還有性能數(shù)據(jù)、頁(yè)面錯(cuò)誤數(shù)據(jù)、console捕獲等。這里我們只講解業(yè)務(wù)數(shù)據(jù)的埋點(diǎn)。2021-05-05
Bootstrap+Vue滑動(dòng)監(jiān)聽(tīng)Scrollspy實(shí)現(xiàn)餐廳餐品展示
本文主要介紹了Bootstrap+Vue滑動(dòng)監(jiān)聽(tīng)Scrollspy實(shí)現(xiàn)餐廳餐品展示,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Vue實(shí)現(xiàn)購(gòu)物車(chē)詳情頁(yè)面的方法
這篇文章主要介紹了Vue實(shí)戰(zhàn)之購(gòu)物車(chē)詳情頁(yè)面的實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
vue+iview 實(shí)現(xiàn)可編輯表格的示例代碼
這篇文章主要介紹了vue+iview 實(shí)現(xiàn)可編輯表格的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
vue使用高德地圖實(shí)現(xiàn)實(shí)時(shí)定位天氣預(yù)報(bào)功能
這篇文章主要介紹了vue使用高德地圖實(shí)現(xiàn)實(shí)時(shí)天氣預(yù)報(bào)功能,根據(jù)定位功能,使用高德地圖實(shí)現(xiàn)定位當(dāng)前城市的天氣預(yù)報(bào)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
vue異步axios獲取的數(shù)據(jù)渲染到頁(yè)面的方法
今天小編就為大家分享一篇vue異步axios獲取的數(shù)據(jù)渲染到頁(yè)面的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
vue watch監(jiān)控對(duì)象的簡(jiǎn)單方法示例
這篇文章主要給大家介紹了關(guān)于vue watch監(jiān)控對(duì)象的簡(jiǎn)單方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
如何使用VuePress搭建一個(gè)類(lèi)型element ui文檔
這篇文章主要介紹了如何使用VuePress搭建一個(gè)類(lèi)型element ui文檔,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02

