vuejs數(shù)據(jù)超出單行顯示更多,點擊展開剩余數(shù)據(jù)實例
說下我在工作中遇到的這個需求
1:頁面上的菜單選項,選項內(nèi)容是后臺接口返回的數(shù)據(jù),現(xiàn)在的需求是當(dāng)選項的內(nèi)容超出一行,在這行的右面顯示更多,點擊更多,顯示剩下的選項的內(nèi)容
看下效果圖

這是展開的效果圖

下面先看下我的html部分代碼
<div :class="bussinessType?'show':'hidde'">
<dl>
<dt>業(yè)務(wù)類型:</dt>
<dd ref="bussinessTypeRef">
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name=""
@click="getchildMenu($event)" class="active">全部</a>
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" :name="item.code" v-for="item in projectType"
@click="getchildMenu($event)">{{item.name}}</a>
</dd>
<i class="unfold" @click="bussinessType = !bussinessType"
v-show="bussinessHeight>40">
{{ bussinessType ? '收起' : '更多'}}
<Icon :type="bussinessType?'chevron-down':'chevron-up'" ></Icon>
</i>
</dl>
</div>
說下原理show就是展開的時候使用的樣式名稱,hide是顯示一行是使用的樣式(主要就是控制容器高度)
.show{
height: auto;
border-bottom: 1px solid #ebebeb;
}
.hidde{
height: 40px;
overflow: hidden;
border-bottom: 1px solid #ebebeb;
}

這是我展示的菜單的部分,主要的思路是看這部分的高度是不是超出一行的高度,如果是超出一行的高度,則讓dl外的div默認使用hide的樣式
那么問題來了,怎么知道展示菜單的dd部分的高度超出一行了呢?
這就需要使用vuejs的watch來實現(xiàn)了
首先,watch監(jiān)聽ref是bussinessTypeRef的組件的高度(內(nèi)容多的話自然dd容易會被撐高),這時候與一行的高度(我這里設(shè)置的是40)作比較,如果超出,就讓更多的文字按鈕顯示出來。下面是監(jiān)聽dd內(nèi)容高度的watch方法
projectType: function () {
this.$nextTick(function(){
let cur = this.$refs['bussinessTypeRef'];
if(cur){
this.bussinessHeight = cur.clientHeight;
}
});
},
這時候更多文字按鈕顯示,我們就控制dl外層的div容器,讓該容器使用hide的樣式,點擊更多的時候,讓控制顯示更多的變量變?yōu)橄喾吹闹担@樣讓收起顯示出來,更多消失,同時讓外層的div容器使用show的樣式。這樣一來就實現(xiàn)了文字超出一行顯示更多,點擊收起的交互效果。
下面附上完整的代碼供參考
<div :class="bussinessType?'show':'hidde'">
<dl>
<dt>業(yè)務(wù)類型:</dt>
<dd ref="bussinessTypeRef">
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name=""
@click="getchildMenu($event)" class="active">全部</a>
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" :name="item.code" v-for="item in projectType"
@click="getchildMenu($event)">{{item.name}}</a>
</dd>
<i class="unfold" @click="bussinessType = !bussinessType"
v-show="bussinessHeight>40">
{{ bussinessType ? '收起' : '更多'}}
<Icon :type="bussinessType?'chevron-down':'chevron-up'" ></Icon>
</i>
</dl>
</div>
// 行業(yè)
businessType: function () {
this.$nextTick(function(){
let cur = this.$refs['industryRef'];
if(cur){
this.industryHeight = cur.clientHeight;
}
});
},
.show{
height: auto;
border-bottom: 1px solid #ebebeb;
}
.hidde{
height: 40px;
overflow: hidden;
border-bottom: 1px solid #ebebeb;
}
.list-filter {
position: relative;
margin-bottom: 20px;
font-size: 14px;
}
.list-filter dl {
overflow: hidden;
}
.list-filter dt {
float: left;
font-weight: 400;
height: 40px;
line-height: 40px;
}
.list-filter dd {
margin-left: 30px;
float: left;
width: 85%;
line-height: 40px;
}
.unfold{
font-size: 14px;
color: #00A971;
cursor: pointer;
font-style: normal;
vertical-align: middle;
display: inline-block;
height: 40px;
line-height: 40px;
}
.list-filter a {
color: #333;
display: inline-block;
margin-right: 20px;
padding: 0 5px;
text-decoration: none;
line-height: 2em;
z-index: 0;
}
以上所述是小編給大家介紹的vuejs數(shù)據(jù)超出單行顯示更多,點擊展開剩余數(shù)據(jù)詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
詳解vue-cli項目中怎么使用mock數(shù)據(jù)
這篇文章主要介紹了vue-cli項目中怎么使用mock數(shù)據(jù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
vue+element-ui+axios多文件上傳的實現(xiàn)并顯示整體進度
這篇文章主要介紹了vue+element-ui+axios多文件上傳的實現(xiàn)并顯示整體進度,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
vue.config.js配置proxy代理產(chǎn)生404錯誤的原因及解決
這篇文章主要介紹了vue.config.js配置proxy代理產(chǎn)生404錯誤的原因及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
vue3中實現(xiàn)拖拽排序代碼示例(vue-draggable-next的使用)
在Vue3中使用拖拽功能時應(yīng)選用vue-draggable-next插件,傳統(tǒng)的draggable插件不兼容Vue3,可能導(dǎo)致TypeError錯誤,安裝后,需在項目中引入并使用,具體步驟包括安裝插件、引入使用、查看效果和相關(guān)說明,需要的朋友可以參考下2024-09-09
vue啟動報錯‘vue-cli-service serve‘問題及解決
這篇文章主要介紹了vue啟動報錯‘vue-cli-service serve‘問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10

