vue實(shí)現(xiàn)超過(guò)兩行顯示展開(kāi)收起的代碼
vue超過(guò)兩行顯示展開(kāi)收起
基于vue-cli2,sass,vant(ui組件):https://youzan.github.io/vant/#/zh-CN/home
具體代碼如下:
<template>
? <div>
? ? <div class="group">
? ? ? <div class="text more" ref="more">
? ? ? ? 占位
? ? ? </div>
? ? ? <div class="list" v-for="(item, index) in historyList" :key="index">
? ? ? ? <van-row>
? ? ? ? ? <van-col span="12">{{ item.version }}</van-col>
? ? ? ? ? <van-col class="t_right l_text" span="12">{{ item.time }}</van-col>
? ? ? ? </van-row>
? ? ? ? <div class="l_title">{{ item.title }}</div>
? ? ? ? <div
? ? ? ? ? class="text"
? ? ? ? ? ref="textContainer"
? ? ? ? ? :class="{ retract: item.status }"
? ? ? ? ? :style="{ 'max-height': item.status ? textHeight : '' }"
? ? ? ? >
? ? ? ? ? {{ item.content }}
? ? ? ? </div>
? ? ? ? <span
? ? ? ? ? v-if="item.status !== null"
? ? ? ? ? class="link"
? ? ? ? ? @click="more(index)"
? ? ? ? ? >{{ item.status ? "展開(kāi)" : "收起" }}</span
? ? ? ? >
? ? ? </div>
? ? </div>
? </div>
</template><script>
export default {
? data () {
? ? return {
? ? ? textHeight: '',
? ? ? historyList: [
? ? ? ? {
? ? ? ? ? version: '7.1.4',
? ? ? ? ? title: '本次更新',
? ? ? ? ? content:
? ? ? ? ? ? '-聽(tīng)模塊新增“文章難度分析”功能~,為你分析文章中詞匯、語(yǔ)速等難度,推薦;-聽(tīng)模塊新增“文章難度分析”功能~,為你分析文章中詞匯、語(yǔ)速等難度,推薦',
? ? ? ? ? time: '2周前'
? ? ? ? },
? ? ? ? {
? ? ? ? ? version: '7.1.4',
? ? ? ? ? title: '本次更新',
? ? ? ? ? content:
? ? ? ? ? ? '-聽(tīng)模塊新增“文章難度分析”功能~,為你分析文章中詞匯、語(yǔ)速等難度,推薦',
? ? ? ? ? time: '5周前'
? ? ? ? },
? ? ? ? {
? ? ? ? ? version: '7.1.4',
? ? ? ? ? title: '本次更新',
? ? ? ? ? content:
? ? ? ? ? ? '-聽(tīng)模塊新增“文章難度分析”功能~,為你分析文章中詞匯、語(yǔ)速等難度,推薦;-聽(tīng)模塊新增“文章難度分析”功能~,為你分析文章中詞匯、語(yǔ)速等難度,推薦',
? ? ? ? ? time: '6周前'
? ? ? ? },
? ? ? ? {
? ? ? ? ? version: '7.1.4',
? ? ? ? ? title: '本次更新',
? ? ? ? ? content:
? ? ? ? ? ? '-聽(tīng)模塊新增“文章難度分析”功能~,為你分析文章中詞匯、語(yǔ)速等難度,推薦',
? ? ? ? ? time: '9周前'
? ? ? ? }
? ? ? ]
? ? }
? },
? mounted () {
? ? this.historyList.forEach((ele, index) => {
? ? ? this.$set(
? ? ? ? this.historyList,
? ? ? ? index,
? ? ? ? Object.assign({}, ele, { status: null })
? ? ? )
? ? })
? ? // DOM 加載完執(zhí)行
? ? this.$nextTick(() => {
? ? ? this.calculateText()
? ? ? //console.log(this.historyList)
? ? })
?
? ? window.onresize = () => {
? ? ? this.historyList.forEach((ele, index) => {
? ? ? ? this.$set(
? ? ? ? ? this.historyList,
? ? ? ? ? index,
? ? ? ? ? Object.assign({}, ele, { status: null })
? ? ? ? )
? ? ? })
? ? ? setTimeout(() => {
? ? ? ? this.calculateText()
? ? ? }, 0)
? ? }
? },
? methods: {
? ? // 計(jì)算文字 顯示展開(kāi) 收起
? ? calculateText () {
? ? ? // 獲取一行文字的height 計(jì)算當(dāng)前文字比較列表文字
? ? ? let oneHeight = this.$refs.more.scrollHeight
? ? ? let twoHeight = oneHeight * 2 || 40
? ? ? this.textHeight = `${twoHeight}px`
? ? ? let txtDom = this.$refs.textContainer
? ? ? for (let i = 0; i < txtDom.length; i++) {
? ? ? ? let curHeight = txtDom[i].offsetHeight
? ? ? ? if (curHeight > twoHeight) {
? ? ? ? ? this.$set(
? ? ? ? ? ? this.historyList,
? ? ? ? ? ? i,
? ? ? ? ? ? Object.assign({}, this.historyList[i], { status: true })
? ? ? ? ? )
? ? ? ? } else {
? ? ? ? ? this.$set(
? ? ? ? ? ? this.historyList,
? ? ? ? ? ? i,
? ? ? ? ? ? Object.assign({}, this.historyList[i], { status: null })
? ? ? ? ? )
? ? ? ? }
? ? ? }
? ? },
? ? more (index) {
? ? ? this.historyList[index].status = !this.historyList[index].status
? ? }
? }
}
</script><style lang="scss" scoped>
.group {
? .list {
? ? padding: 5px 0;
? ? border-bottom: 1px solid #eaeaea;
? }
? .text {
? ? position: relative;
? ? color: #000;
? ? font-size: 14px;
? }
? .more {
? ? visibility: hidden;
? }
? .link {
? ? font-size: 12px;
? ? color: #2d95fe;
? }
? .retract {
? ? position: relative;
? ? overflow: hidden;
? }
?
? .retract:after {
? ? content: "...";
? ? position: absolute;
? ? bottom: 0;
? ? right: 2px;
? ? width: 25px;
? ? padding-left: 25px;
? ? background: linear-gradient(to right, transparent, #fff 45%);
? }
}
</style>vue多個(gè)展開(kāi)收起功能
需求場(chǎng)景:移動(dòng)端/h5/公眾號(hào)頁(yè)面,有很多分類(lèi),每個(gè)分類(lèi)下展示圖片,默認(rèn)超出兩張 出現(xiàn)展開(kāi)和收起功能。

說(shuō)下思路
這類(lèi)功能很常見(jiàn),其實(shí)之前也寫(xiě)過(guò);正常思路都是搞個(gè)類(lèi)似單獨(dú)的變量去控制分類(lèi)下圖片是展開(kāi)/收起;但是代碼很是累贅,于是就想說(shuō)能不能用原生的代碼去寫(xiě)。
接口返回的數(shù)據(jù)格式大致如下:
list =
[
{
title: '標(biāo)題一',
id:'1',
imgList:[
'img1','img2',...
]
},
{
title: '標(biāo)題一',
id:'2',
imgList:[
'img1','img2',...
]
},
]
嘗試
1.HTML上生成特定的class
使用接口的id,生成特定的類(lèi),這樣在控制和找DOM就會(huì)更方便
代碼里是分類(lèi)下,圖片超出4張才會(huì)出現(xiàn)展開(kāi)/收起按鈕
默認(rèn)不展示收起按鈕
<div v-for="item in list">
<div>標(biāo)題:{{item.title}}</div>
<div :class="`main${item.id}`">
<img v-for="(url,index) in item.imgList.slice(0,4)" :src="url">
</div>
//被控制 展開(kāi)/收起的圖片
<div :class="`main${item.id}`">
<img v-for="(url,index) in item.imgList.slice(4,)" :src="url">
</div>
<div v-if="item.imgList.length>4">
<div :class="`open${item.id}`" @click="toChange(item,'block')">展開(kāi)</div>
<div :class="`close${item.id}`" @click="toChange(item,'none')" style="display:none;">收起</div>
</div>
</div>
2.控制展開(kāi)/收起
分別獲取到 mainDom,openDom ,closeDom
下面通過(guò)類(lèi)名獲取到的是DOM數(shù)組,DOM數(shù)組不能用forEach遍歷,所以要轉(zhuǎn)一下
// 展開(kāi)/收起
toChange(item,str){
let mainDom = document.getElementsByClassName(`nr${item.id}`);
let openDom = document.getElementById(`open${item.id}`);
let closeDom = document.getElementById(`close${item.id}`);
mainDom = [...mainDom];
mainDom.forEach(item=>{
item.style.display = str;
})
closeDom.style.display = str;
openDom.style.display = str==='block' ? 'none':'block';
},
以上就是今天要講的內(nèi)容,本文僅僅簡(jiǎn)單介紹了平常工作中常見(jiàn)的需求,同一種需求不同狀態(tài)下寫(xiě),代碼也會(huì)不一樣,寫(xiě)文章也是為了更好的總結(jié),從中慢慢積累經(jīng)驗(yàn)。希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vant-list組件觸發(fā)多次onload事件導(dǎo)致數(shù)據(jù)亂序的解決方案
這篇文章主要介紹了vant-list組件觸發(fā)多次onload事件導(dǎo)致數(shù)據(jù)亂序的解決方案2023-01-01
vue中el-autocomplete支持分頁(yè)上拉加載功能
最近在項(xiàng)目中使用了ElementUI的el-autocomplete,下面這篇文章主要介紹了vue中el-autocomplete支持分頁(yè)上拉加載功能的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
VUE 更好的 ajax 上傳處理 axios.js實(shí)現(xiàn)代碼
本篇文章主要介紹了VUE 更好的 ajax 上傳處理 axios.js實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
vue使用數(shù)組splice方法失效,且總刪除最后一項(xiàng)的問(wèn)題及解決
這篇文章主要介紹了vue使用數(shù)組splice方法失效,且總刪除最后一項(xiàng)的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
基于Vue3+WebSocket實(shí)現(xiàn)實(shí)時(shí)文件傳輸監(jiān)控系統(tǒng)
WebSocket是一種在客戶(hù)端和服務(wù)器之間進(jìn)行雙向通信的網(wǎng)絡(luò)協(xié)議,它通過(guò)建立持久性的、全雙工的連接,允許服務(wù)器主動(dòng)向客戶(hù)端發(fā)送數(shù)據(jù),本文小編給大家介紹了基于Vue3+WebSocket實(shí)現(xiàn)實(shí)時(shí)文件傳輸監(jiān)控系統(tǒng),需要的朋友可以參考下2025-03-03
Vue-cli創(chuàng)建項(xiàng)目從單頁(yè)面到多頁(yè)面的方法
本篇文章主要介紹了Vue-cli創(chuàng)建項(xiàng)目從單頁(yè)面到多頁(yè)面的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
Vue中的父子組件通訊以及使用sync同步父子組件數(shù)據(jù)
這篇文章主要介紹了Vue中的父子組件通訊以及使用sync同步父子組件數(shù)據(jù),對(duì)vue感興趣的同學(xué),可以參考下2021-04-04

