基于Vue3+TS简单设计一个查看文章时点击展开和点击收起的小功能

前言

这是一个展开和收起的小功能,可用于查看文章时点击展开和点击收起。

一、示例代码

(1)/src/views/Example/ExpandToggle/index.vue

<template>
  <div class="e-w">
    <div class="e-w-main" :class="{ 'e-w-expand': isExpand }" ref="ewRef">

      <div
        style="
          color: rgb(96, 109, 121);
          background-color: #f5ecd7;
          font-family: '楷体';
          padding: 22px;
        "
      >
        <b>什么是Vite,它与Vue CLI有什么区别,Volar又是啥?</b><br />

        <br />

        <b>Vite</b><br />
        Vite 是一个轻量级的、速度极快的构建工具,<br />
        对 Vue SFC 提供第一优先级支持。<br />
        作者是尤雨溪,<br />
        同时也是 Vue 的作者!<br />

        <br />

        <b>Vue CLI</b><br />
        Vue CLI 是官方提供的基于 Webpack 的 Vue 工具链,<br />
        它现在处于维护模式。<br />
        我们建议使用 Vite 开始新的项目,<br />
        除非你依赖特定的 Webpack 的特性。<br />
        在大多数情况下,<br />
        Vite 将提供更优秀的开发体验。<br />

        <br />

        <b>Volar</b><br />
        Volar 是 Vue 的 VS Code 插件,<br />
        也是 Vue 的官方 IDE/TS 支持工具,<br />
        除了集成 Vetur 的相关功能,<br />
        如高亮、语法提示等之外,<br />
        还包含一些独有功能。<br />
        也就是说,Volar 取代了我们之前为 Vue 2 提供的官方 VSCode 扩展 Vetur。如果你之前已经安装了 Vetur,请确保在 Vue 3 的项目中禁用它。<br />
      </div>

      <div class="view-more" v-if="!isExpand">
        <div class="view-more-box" @click="isExpand = !isExpand">
          <el-icon color="#409EFC">
            <ArrowDown />
          </el-icon>
        </div>
      </div>

      <div class="has-more" v-if="isExpand">
        <div class="has-more-box" @click="isExpand = !isExpand">
          <el-icon color="#409EFC">
            <ArrowUp />
          </el-icon>
        </div>
      </div>

    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { ArrowUp, ArrowDown } from "@element-plus/icons-vue";

const ewRef = ref(); // 实例
const isExpand = ref(false); // 是否展开

onMounted(() => {
  console.log('ewRef =>', ewRef)
  if (ewRef.value) {
    if (ewRef.value.scrollHeight > ewRef.value.clientHeight) {
      // ...
    }
  }
});
</script>
  
<style lang="less" scoped>
  .e-w {
    width: auto;
    padding: 100px;

    .e-w-main {
      height: 150px;
      overflow: hidden;
      position: relative;
      border: 1px solid #ddd;

      &.e-w-expand {
        height: auto;
        overflow: hidden;
      }

      // ---- ---- ---- ---- ^ 展开 样式 ---- ---- ---- ----
      .view-more {
        width: 100%;
        height: 22px;
        padding-top: 60px;
        background-image: linear-gradient(
          -180deg,
          rgba(255, 255, 255, 0) 0%,
          #ebebebf6 100%
        );
        position: absolute;
        bottom: 0;

        .view-more-box {
          width: 44px;
          height: 22px;
          background-color: rgba(255, 255, 255, 1);
          border-top-left-radius: 8px;
          border-top-right-radius: 8px;
          position: absolute;
          display: block;
          margin: auto;
          left: 0;
          right: 0;
          bottom: 0;
          cursor: pointer;

          .el-icon {
            position: absolute;
            display: block;
            margin: auto;
            left: 0;
            right: 0;
            bottom: 0;
          }
        }
      }
      // ---- ---- ---- ---- / 展开 样式 ---- ---- ---- ----

      // ---- ---- ---- ---- ^ 收起 样式 ---- ---- ---- ----
      .has-more {
        width: 100%;
        height: 22px;
        position: absolute;
        bottom: 0;

        .has-more-box {
          width: 44px;
          height: 22px;
          background-color: rgba(255, 255, 255, 1);
          border-top-left-radius: 8px;
          border-top-right-radius: 8px;
          position: absolute;
          display: block;
          margin: auto;
          left: 0;
          right: 0;
          bottom: 0;
          cursor: pointer;

          .el-icon {
            position: absolute;
            display: block;
            margin: auto;
            left: 0;
            right: 0;
            bottom: 0;
          }
        }
      }
      // ---- ---- ---- ---- / 收起 样式 ---- ---- ---- ----
    }
  }
</style>

二、运行效果

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值