vue实现超过两行显示展开收起的代码

 更新时间:2022年10月24日 10:33:25   作者:前端华仔  
这篇文章主要介绍了vue实现超过两行显示展开收起的代码,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

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 加载完执行
    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 计算当前文字比较列表文字
      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/公众号页面,有很多分类,每个分类下展示图片,默认超出两张 出现展开和收起功能。

说下思路

这类功能很常见,其实之前也写过;正常思路都是搞个类似单独的变量去控制分类下图片是展开/收起;但是代码很是累赘,于是就想说能不能用原生的代码去写。

接口返回的数据格式大致如下:

list = 
[
	{
		title: '标题一',
		id:'1',
		imgList:[
			'img1','img2',...
		]
	},
	{
		title: '标题一',
		id:'2',
		imgList:[
			'img1','img2',...
		]
	},
]

尝试

1.HTML上生成特定的class

使用接口的id,生成特定的类,这样在控制和找DOM就会更方便

代码里是分类下,图片超出4张才会出现展开/收起按钮

默认不展示收起按钮

<div v-for="item in list">
	<div>标题:{{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数组,DOM数组不能用forEach遍历,所以要转一下

// 展开/收起
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';
},

以上就是今天要讲的内容,本文仅仅简单介绍了平常工作中常见的需求,同一种需求不同状态下写,代码也会不一样,写文章也是为了更好的总结,从中慢慢积累经验。希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Vue 解决在element中使用$notify在提示信息中换行问题

    Vue 解决在element中使用$notify在提示信息中换行问题

    这篇文章主要介绍了Vue 解决在element中使用$notify在提示信息中换行问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • @vue/cli4.x版本的vue.config.js常用配置方式

    @vue/cli4.x版本的vue.config.js常用配置方式

    这篇文章主要介绍了@vue/cli4.x版本的vue.config.js常用配置方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • vue2 利用echarts 单独绘制省份的步骤

    vue2 利用echarts 单独绘制省份的步骤

    这篇文章主要介绍了vue2 利用echarts 单独绘制省份,首先引入所需要的第三方模块,通过示例代码给大家介绍的非常详细,文章末尾给大家补充介绍了vue2.x结合echarts2实现显示具体省份热力图的问题,需要的朋友可以参考下
    2022-01-01
  • vue实现可移动的悬浮按钮

    vue实现可移动的悬浮按钮

    这篇文章主要为大家详细介绍了vue实现可移动的悬浮按钮,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-03-03
  • VueCli4项目配置反向代理proxy的方法步骤

    VueCli4项目配置反向代理proxy的方法步骤

    这篇文章主要介绍了VueCli4项目配置反向代理proxy的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • vue diff算法全解析

    vue diff算法全解析

    这篇文章主要介绍了vue diff算法的使用,帮助大家更好的理解和学习使用vue,感兴趣的朋友可以了解下
    2021-04-04
  • 让Vue响应Map或Set的变化操作

    让Vue响应Map或Set的变化操作

    这篇文章主要介绍了让Vue响应Map或Set的变化操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • Vue项目打包成exe可执行文件的实现过程(无瑕疵,完美版)

    Vue项目打包成exe可执行文件的实现过程(无瑕疵,完美版)

    突然接到公司需求,说客户想让我们把项目直接打包,所以下面这篇文章主要给大家介绍了关于Vue项目打包成exe可执行文件的实现过程,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2022-11-11
  • vue3中使用editor.js的详细步骤记录

    vue3中使用editor.js的详细步骤记录

    富文本编辑器作为直接与用户交互的内容输入生产工具,对大家的项目来说非常重要,下面这篇文章主要给大家介绍了关于vue3中使用editor.js的详细步骤,需要的朋友可以参考下
    2024-01-01
  • vue-router2.0 组件之间传参及获取动态参数的方法

    vue-router2.0 组件之间传参及获取动态参数的方法

    下面小编就为大家带来一篇vue-router2.0 组件之间传参及获取动态参数的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11

最新评论