前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信小程序自定义弹窗组件 action-sheet

微信小程序自定义弹窗组件 action-sheet

作者头像
越陌度阡
发布2022-12-15 14:38:55
5380
发布2022-12-15 14:38:55
举报

最近公司在开发短剧小程序,短剧的剧集展示交互需要用到组件 action-sheet,小程序自带的有这个组件,但是这个组件支持的功能比较单一,相当于是一个选择表一样,不能自定义很多内容,只能自定义一个组件来使用。

 以下是这个组件的实现:

1. 组件的 index.js 代码;

代码语言:javascript
复制
// components/action-sheet/index.js

Component({

    data: {
        show: false,
        animation: "",
        maskAnimation: ''
    },

    properties: {
        actionShow: Boolean
    },

    observers: {
        'actionShow': function (newValue) {
            this.setData({
                show: newValue,
                animation: newValue === true ? 'show-action-sheet' : 'hide-action-sheet',
                maskAnimation: newValue === true ? 'show-mask-animation' : 'hide-mask-animation'
            })
        }
    },

    methods: {
        cancelAction() {
            this.setData({
                animation: 'hide-action-sheet',
                maskAnimation: 'hide-mask-animation'
            })
            setTimeout(() => {
                this.setData({
                    show: false
                })
            }, 300);
        }
    }
})

2. 组件的 index.wxml 代码;

代码语言:javascript
复制
<!-- components/action-sheet/index.wxss -->

<view class="action-sheet {{maskAnimation}}" hidden="{{!show}}" bindtap="cancelAction">
    <view class="container {{animation}}">
        <slot></slot>
    </view>
</view>

4. 组件的 index.wxss 代码;

代码语言:javascript
复制
/* components/action-sheet/index.wxss */

.action-sheet {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    opacity: 0;
    background-color: rgba(0, 0, 0, 0.3);
}

.container {
    position: absolute;
    left: 0;
    top: 100%;
    width: 100%;
    height: 70%;
    background-color: #fcf8f8;
    border-top-left-radius: 30rpx;
    border-top-right-radius: 30rpx;
}



.show-action-sheet {
    animation-name: show-animation;
    animation-duration: 0.25s;
    animation-timing-function: linear;
    animation-fill-mode: forwards;
}

.hide-action-sheet {
    animation-name: hide-animation;
    animation-duration: 0.25s;
    animation-timing-function: linear;
    animation-fill-mode: forwards;
}

.show-mask-animation {
    animation-name: show-mask;
    animation-duration: 0.25s;
    animation-timing-function: linear;
    animation-fill-mode: forwards;
}

.hide-mask-animation {
    animation-name: hide-mask;
    animation-duration: 0.25s;
    animation-timing-function: linear;
    animation-fill-mode: forwards;
}

@keyframes show-animation {
    from {
        transform: translateY(0);
    }

    to {
        transform: translateY(-100%);
    }
}

@keyframes hide-animation {
    from {
        transform: translateY(-100%);
    }

    to {
        transform: translateY(0);
    }
}

@keyframes show-mask {
    from {
        opacity: 0;
    }

    to {
        opacity: 1.0;
    }

}

@keyframes hide-mask {
    from {
        opacity: 1.0;
    }

    to {
        opacity: 0;
    }
}

5. 组件的 index.json 配置;

代码语言:javascript
复制
{
	"component": true,
	"usingComponents": {}
}

6. 组件的使用;

在要使用组件的json文件中引入自定义的 action-sheet 组件:

代码语言:javascript
复制
{  
    "usingComponents": {
        "x-action-sheet": "./components/action-sheet/index"
    },
    "navigationStyle" : "custom"
}

在wxml文件中使用组件:

代码语言:javascript
复制
<x-action-sheet actionShow="{{rechargeActionShow}}">
        
    <!--自定义的内容-->

</x-action-sheet>

rechargeActionShow 为控制 action-sheet 显示的变量。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-12-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档