uniapp 自定义展开/收起组件(适配H5、微信小程序等)

今天遇到啦个需求,内容展开收起功能,没有的用,那边手搓一个吧

因为项目使用到的也只有H5端和微信小程序端,所以如果其余端开发的话,需要看一下适当的调整,不出意外的话就直接用吧,废话不多说,上菜:

javascript 复制代码
<template>
    <view class="more">
        <view class="more_content" :style="{ maxHeight: getMaxH }">
            <view class="more_content_text" :style="{ fontSize: size + 'rpx' }">
                <slot>{{ text }}</slot>
            </view>
        </view>
        <view class="more_btn" :style="{ fontSize: size + 'rpx' }" v-if="domHeight.inner > domHeight.outer"
            @tap="isMore = !isMore">
            {{ isMore ? '收起' : '展开' }}
        </view>
    </view>
</template>

<script>
export default {
    /**
     * 1、设置maxH,row将会失效
    */
    props: {
        // 文本
        text: {
            type: String,
            default: ''
        },
        // 最大高度 rpx
        maxH: {
            type: Number,
            default: 0
        },
        // 字体大小 rpx
        size: {
            type: Number,
            default: 28
        },
        // 行数
        row: {
            type: Number,
            default: 2
        }
    },
    data() {
        return {
            isMore: false, // 是否显示更多
            domHeight: {
                outer: 0,
                inner: 0,
            }
        }
    },
    async mounted() {
        let outerDom = await this.$util.getDomInfo('.more_content', this);
        let innerDom = await this.$util.getDomInfo('.more_content_text', this);
        this.domHeight = {
            outer: outerDom.height,
            inner: innerDom.height
        }
    },
    computed: {
        getMaxH() {
            let h = this.row * this.size * 1.5;
            // #ifdef MP-WEIXIN
            h -= 8;
            // #endif
            let more = this.domHeight.inner / (uni.upx2px(100) / 100) + 'rpx'; // 展开高度
            let noMore = this.maxH ? (this.maxH + 'rpx') : (h + 'rpx'); // 收起高度
            return this.isMore ? more : noMore
        },
    }
}
</script>

<style lang="scss" scoped>
.more {
    position: relative;

    .more_btn {
        position: absolute;
        right: 0;
        bottom: 0;
        padding-left: 20rpx;
        line-height: 1.5;
        // background: red;
        background: linear-gradient(to right, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 20%, rgba(255, 255, 255, 1) 100%);
    }
}

.more_content {
    overflow: hidden;
    transition: .2s;
}

.more_content_text {
    word-wrap: break-word;
    word-break: break-all;
    white-space: pre-wrap;
    line-height: 1.5;
}
</style>
props 说明 默认值
text 需要显示的文本 -
maxH 设置最大高度(设置maxH后row属性便会失效) 0
size 内容字体大小(行高为1.5倍) 28
row 收起时显示行数(设置maxH后row属性便会失效) 2
[参数说明]

|---------|------|---|
| default | 默认插槽 | - |
[插槽]

可通过text属性传入文本,也可以通过默认插槽传入自定义内容,任君选择~

补充一下组件里面使用到的工具函数:

this.$util.getDomInfo

javascript 复制代码
	getDomInfo: function (id, that) {
		return new Promise((resolve, reject) => {
			if (!id) return reject('id/类名 不能为空')
			if (!that) return reject('this指向不能为空')
			const query = uni.createSelectorQuery().in(that);
			query.select(id).boundingClientRect(data => {
				// console.log("节点离页面顶部的距离为" + data.height);
				resolve(data || {})
			}).exec();
		})
	},
相关推荐
陈不知代码4 小时前
uniapp创建vue3+ts+pinia+sass项目
前端·uni-app·sass
说私域4 小时前
公域流量向私域流量转化策略研究——基于开源AI智能客服、AI智能名片与S2B2C商城小程序的融合应用
人工智能·小程序
半生过往4 小时前
微信小程序文件下载与预览功能实现详解
微信小程序·小程序·notepad++·压缩包下载解压
源码_V_saaskw4 小时前
JAVA图文短视频交友+自营商城系统源码支持小程序+Android+IOS+H5
java·微信小程序·小程序·uni-app·音视频·交友
weixin_lynhgworld7 小时前
淘宝扭蛋机小程序系统开发:重塑电商互动模式
大数据·小程序
996幸存者9 小时前
uni-app区域选择、支持静态、动态数据
微信小程序·uni-app
ᥬ 小月亮10 小时前
Uniapp编写微信小程序,绘制动态圆环进度条
微信小程序·小程序·uni-app
耶啵奶膘14 小时前
uniapp+vue3——通知栏标题纵向滚动切换
uni-app
The_era_achievs_hero17 小时前
UniappDay03
vue.js·微信小程序·uni-app
说私域20 小时前
技术赋能与营销创新:开源链动2+1模式AI智能名片S2B2C商城小程序的流量转化路径研究
人工智能·小程序·开源