首页 > 其他分享 >vue使用纯CSS实现多行文本的“展开”与“收起”功能

vue使用纯CSS实现多行文本的“展开”与“收起”功能

时间:2023-02-28 12:14:07浏览次数:56  
标签:多行 vue checked text 100% height content CSS more

// 创建组件
<template>
  <!--
    文档搜索列表展开/收起组件
    实现了文档搜索列表页 文档超过5行会显示展开/收起的功能
  -->
  <div class="mj-text-hide">
    <input :id="`group-members_exp${index}`" class="group-members_exp" type="checkbox"/>
    <div class="more-text" line-clamp="5" style="background: #fff;width: 100%;">
      <label style="margin-top: 0;" class="more-btn cur" :for="`group-members_exp${index}`">
        <span class="text"></span>
        <i class="el-icon-arrow-down"></i>
      </label>
      <div style="font-size: 13px;display: inline;" v-html="value" @click="handlePreview($event,item)"></div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'MjTextHide',
  props: {
    value: { // 文字
      type: String,
      default: ''
    },
    item: {
      type: Object,
      required: true
    },
    index: {
      type: Number,
      default: 0
    }
  },
  methods: {
    /**
     * 点击高亮词定位
     * @param e
     * @param item
     */
    handlePreview(e, item) {
      this.$emit("handlePreview", e, item);
    }
  }
}
</script>

<style scoped lang="scss">
.mj-text-hide {
  display: flex;
  width: 100%;

  [line-clamp="5"] {
    max-height: 100px;
  }

  .more-text {
    overflow: hidden;
    line-height: 1.5;
    max-height: 105px;
    position: relative;

    &::before {
      content: '';
      float: right;
      width: 0;
      height: calc(100% - 20px);
      /*100%减去一个按钮的高度即可*/
      background: #FFFFFF;
    }

    &::after {
      content: '';
      width: 100%;
      height: 100%;
      position: absolute;
      background: #FFFFFF;
    }

    .more-btn {
      float: right;
      clear: both;
      color: #c8c8c8;
      font-size: 13px;
      cursor: pointer;

      &::before {
        content: '…';
        margin-right: 15px;
        color: #c8c8c8;
        font-size: 13px;
        transform: translateX(-100%);
      }

      .text::after {
        content: '展开';
      }
    }
  }

  .group-members_exp {
    display: none;

    &:checked + .more-text {
      -webkit-line-clamp: 999;
      max-height: none;
    }

    &:checked + .more-text .more-btn .text::after {
      content: '收起';
    }

    &:checked + .more-text .more-btn .el-icon-arrow-down {
      transform: rotate(180deg);
    }

    &:checked + .more-text .more-btn::before {
      display: none;
    }

    &:checked + .more-text::after {
      display: none;
    }
  }
}
</style>

使用

<div class="item-content">
    <TextCom :value="item.content" :index="index" :item="item"
        @handlePreview="handlePreview"></TextCom>
</div>    

参考:

https://juejin.cn/post/6905368091165720589

https://juejin.cn/post/6963904955262435336#heading-7

https://juejin.cn/post/7013261397383446542

标签:多行,vue,checked,text,100%,height,content,CSS,more
From: https://www.cnblogs.com/Console-LIJIE/p/17163555.html

相关文章

  • 通过使用online表单的获取使用,了解vue.js数组的常用操作
        在开发项目中,经常会遇到对数组的操作,比如对数组的数据进行删减或增加,同时也会对每个数组里的数据进行删减,下面就举个例子说明一下。   直接给一段代码getO......
  • vue中的几个高级概念
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0NEJeurD-1677552905427)(https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/bf111c39bc87422......
  • 滴滴前端常考vue面试题
    Computed和Methods的区别可以将同一函数定义为一个method或者一个计算属性。对于最终的结果,两种方式是相同的不同点:computed:计算属性是基于它们的依赖进行缓存......
  • 美团前端常见vue面试题(必备)
    v-model是如何实现的,语法糖实际是什么?(1)作用在表单元素上动态绑定了input的value指向了messgae变量,并且在触发input事件的时候去动态把message设置为目标值:<in......
  • vue源码分析-响应式系统(三)
    上一节,我们深入分析了以data,computed为数据创建响应式系统的过程,并对其中依赖收集和派发更新的过程进行了详细的分析。然而在使用和分析过程中依然存在或多或少的问题,这......
  • 字节前端一面常见vue面试题(必备)
    Vue为什么没有类似于React中shouldComponentUpdate的生命周期考点:Vue的变化侦测原理前置知识:依赖收集、虚拟DOM、响应式系统根本原因是Vue与React的变化侦测方式......
  • VUE2 键盘事件
    常规用法<inputtype="text"@keyup="showInfo"></input>methods:{showInfo(e){console.log(e.target.value)//这种情况会是输入框中实时输入什么,就......
  • VUE2 滚动事件
    滚动条的滚动事件<ul@scroll="gundong"><li>1</li><li>2</li><li>3</li></ul>鼠标滚轮的滚动事件<ul@wheel="gundong"><li>1</li><li>2<......
  • vue3 门户网站搭建5-图标
    奈何element自带的图标太少,不够用,故打算使用 vite-plugin-svg-icons组件来封装svg-icon。ps:ui框架选用的 element-ui,为了能跟vue3更好的结合,还得装个 elemen......
  • 百度前端一面高频vue面试题汇总
    什么是递归组件?举个例子说明下?分析递归组件我们用的比较少,但是在Tree、Menu这类组件中会被用到。体验组件通过组件名称引用它自己,这种情况就是递归组件<template><......