欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue實(shí)現(xiàn)超過兩行顯示展開收起的代碼

 更新時間:2022年10月24日 10:33:25   作者:前端華仔  
這篇文章主要介紹了vue實(shí)現(xiàn)超過兩行顯示展開收起的代碼,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

vue超過兩行顯示展開收起

基于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 ? "展開" : "收起" }}</span
? ? ? ? >
? ? ? </div>
? ? </div>
? </div>
</template>
<script>
export default {
? data () {
? ? return {
? ? ? textHeight: '',
? ? ? historyList: [
? ? ? ? {
? ? ? ? ? version: '7.1.4',
? ? ? ? ? title: '本次更新',
? ? ? ? ? content:
? ? ? ? ? ? '-聽模塊新增“文章難度分析”功能~,為你分析文章中詞匯、語速等難度,推薦;-聽模塊新增“文章難度分析”功能~,為你分析文章中詞匯、語速等難度,推薦',
? ? ? ? ? time: '2周前'
? ? ? ? },
? ? ? ? {
? ? ? ? ? version: '7.1.4',
? ? ? ? ? title: '本次更新',
? ? ? ? ? content:
? ? ? ? ? ? '-聽模塊新增“文章難度分析”功能~,為你分析文章中詞匯、語速等難度,推薦',
? ? ? ? ? time: '5周前'
? ? ? ? },
? ? ? ? {
? ? ? ? ? version: '7.1.4',
? ? ? ? ? title: '本次更新',
? ? ? ? ? content:
? ? ? ? ? ? '-聽模塊新增“文章難度分析”功能~,為你分析文章中詞匯、語速等難度,推薦;-聽模塊新增“文章難度分析”功能~,為你分析文章中詞匯、語速等難度,推薦',
? ? ? ? ? time: '6周前'
? ? ? ? },
? ? ? ? {
? ? ? ? ? version: '7.1.4',
? ? ? ? ? title: '本次更新',
? ? ? ? ? content:
? ? ? ? ? ? '-聽模塊新增“文章難度分析”功能~,為你分析文章中詞匯、語速等難度,推薦',
? ? ? ? ? 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: {
? ? // 計算文字 顯示展開 收起
? ? calculateText () {
? ? ? // 獲取一行文字的height 計算當(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多個展開收起功能

需求場景:移動端/h5/公眾號頁面,有很多分類,每個分類下展示圖片,默認(rèn)超出兩張 出現(xiàn)展開和收起功能。

說下思路

這類功能很常見,其實(shí)之前也寫過;正常思路都是搞個類似單獨(dú)的變量去控制分類下圖片是展開/收起;但是代碼很是累贅,于是就想說能不能用原生的代碼去寫。

接口返回的數(shù)據(jù)格式大致如下:

list = 
[
	{
		title: '標(biāo)題一',
		id:'1',
		imgList:[
			'img1','img2',...
		]
	},
	{
		title: '標(biāo)題一',
		id:'2',
		imgList:[
			'img1','img2',...
		]
	},
]

嘗試

1.HTML上生成特定的class

使用接口的id,生成特定的類,這樣在控制和找DOM就會更方便

代碼里是分類下,圖片超出4張才會出現(xiàn)展開/收起按鈕

默認(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>
	//被控制 展開/收起的圖片
	<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')">展開</div>
		<div :class="`close${item.id}`" @click="toChange(item,'none')" style="display:none;">收起</div>
	</div>
</div>

2.控制展開/收起

分別獲取到 mainDom,openDom ,closeDom

下面通過類名獲取到的是DOM數(shù)組,DOM數(shù)組不能用forEach遍歷,所以要轉(zhuǎn)一下

// 展開/收起
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)容,本文僅僅簡單介紹了平常工作中常見的需求,同一種需求不同狀態(tài)下寫,代碼也會不一樣,寫文章也是為了更好的總結(jié),從中慢慢積累經(jīng)驗(yàn)。希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue3實(shí)戰(zhàn)-子組件之間相互傳值問題

    vue3實(shí)戰(zhàn)-子組件之間相互傳值問題

    這篇文章主要介紹了vue3實(shí)戰(zhàn)-子組件之間相互傳值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Vue 使用計時器實(shí)現(xiàn)跑馬燈效果的實(shí)例代碼

    Vue 使用計時器實(shí)現(xiàn)跑馬燈效果的實(shí)例代碼

    這篇文章主要介紹了Vue 使用計時器實(shí)現(xiàn)跑馬燈效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-07-07
  • element?ui富文本編輯器的使用效果與步驟(quill-editor)

    element?ui富文本編輯器的使用效果與步驟(quill-editor)

    富文本編輯器在任何項(xiàng)目中都會用到,在Element中我們推薦vue-quill-editor組件,下面這篇文章主要給大家介紹了關(guān)于element?ui富文本編輯器的使用效果與步驟(quill-editor)的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • vue3.0中setup中異步轉(zhuǎn)同步的實(shí)現(xiàn)

    vue3.0中setup中異步轉(zhuǎn)同步的實(shí)現(xiàn)

    這篇文章主要介紹了vue3.0中setup中異步轉(zhuǎn)同步的實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • vue中導(dǎo)出Excel表格的實(shí)現(xiàn)代碼

    vue中導(dǎo)出Excel表格的實(shí)現(xiàn)代碼

    項(xiàng)目中我們可能會碰到導(dǎo)出Excel文件的需求,這篇文章主要介紹了vue中導(dǎo)出Excel表格的實(shí)現(xiàn)代碼,非常具有實(shí)用價值,需要的朋友可以參考下
    2018-10-10
  • 詳解用vue.js和laravel實(shí)現(xiàn)微信授權(quán)登陸

    詳解用vue.js和laravel實(shí)現(xiàn)微信授權(quán)登陸

    本篇文章主要介紹了詳解用vue.js和laravel實(shí)現(xiàn)微信授權(quán)登陸,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • 詳解Vue結(jié)合后臺的列表增刪改案例

    詳解Vue結(jié)合后臺的列表增刪改案例

    這篇文章主要介紹了詳解Vue結(jié)合后臺的增刪改案例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • 如何在Vue3中正確使用ElementPlus,親測有效,避坑

    如何在Vue3中正確使用ElementPlus,親測有效,避坑

    這篇文章主要介紹了如何在Vue3中正確使用ElementPlus,親測有效,避坑!具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Vue + Elementui實(shí)現(xiàn)多標(biāo)簽頁共存的方法

    Vue + Elementui實(shí)現(xiàn)多標(biāo)簽頁共存的方法

    這篇文章主要介紹了Vue + Elementui實(shí)現(xiàn)多標(biāo)簽頁共存的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-06-06
  • Vue中sync修飾符分析原理及用法示例

    Vue中sync修飾符分析原理及用法示例

    在vue中,子組件如果想修改父組件的變量,一般做法是通過綁定事件的方法,父組件向子組件傳遞修改變量的方法,子組件觸發(fā)修改變量的方法執(zhí)行,這種方式中規(guī)中矩;另一種方法是使用sync修飾符,此方法可以減少很多代碼量
    2022-08-08

最新評論