温馨提示×

微信小程序如何实现底部弹出框封装

发布时间:2022-07-21 13:44:30 阅读:187 作者:iii 栏目:开发技术

这篇“微信小程序如何实现底部弹出框封装”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“微信小程序如何实现底部弹出框封装”文章吧。

微信小程序如何实现底部弹出框封装

微信小程序如何实现底部弹出框封装

<!--index.wxml-->
<view>
  <button  catchtap="changeRange2">点击唤起弹窗222</button>

  <!-- 弹框 -->
  <dialogA id='dialog' catchtouchmove="preventTouchMove" bind:customEventHandler="customEvent"></dialogA>
</view>
{
  "usingComponents": {
    "dialogA":"/components/dialogA/dialog",
    "dialog":"/components/dialog/dialog"
  }
}
// index.js
// 获取应用实例
const app = getApp()

Page({

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function ({
        this.popup = this.selectComponent("#dialog"); //获取
    },
    
    
    // 调用子组件事件---弹窗2
    changeRange2(e) {
        var _this = this;
        _this.popup.changeRange(); //调用子组件内的函数

    },
    
})

微信小程序如何实现底部弹出框封装

<!--components/dialog/dialog.wxml-->

<view class="half-screen" catchtouchmove="preventTouchMove">
    <!--屏幕背景变暗的背景  -->
    <view class="background_screen" catchtap="hideModal" wx:if="{{showModalStatus}}"></view>
    <!--弹出框  -->
    <view animation="{{animationData}}" class="attr_box" wx:if="{{showModalStatus}}">
        <view class="dialog-box">
            <view class="dialog-head">
                <view class="dialog-title">商品类型</view>
                <view class="close2ImgBox">
                    <image src="/img/close2.png" class="close2Img"  catchtap="hideModal"></image>
                </view>
            </view>
            <view class='dialog-content'>
                <view class="select-box">
                    <view wx:for="{{tabData.val}}" wx:key="index" class="select-item {{index==tabData.toValIndex?'selectedItem':''}}" data-dialogid="{{index}}" catchtap="getValueTap">{{item}}</view>
                </view>
                <view class="btnBox">
                    <button class="btn" catchtap="hideModal">确认</button>
                </view>
            </view>

        </view>
    </view>
</view>
/* components/dialog/dialog.wxss */
/*模态框*/
/*使屏幕变暗  */
.background_screen {
  width100%;
  height100%;
  position: fixed;
  top0;
  left0;
  background#000;
  opacity0.2;
  overflow: hidden;
  z-index1000;
  color#fff;
}

/*对话框 */
.attr_box {
  background#FFFFFF;
  opacity1;
  /* border-radius: 0px 0px 0px 0px; */
  /* height: 500rpx; */
  height: auto;
  width100%;
  overflow: hidden;
  position: fixed;
  bottom0;
  left0;
  z-index2000;
  background#fff;
  /* background: rgba(66, 66, 66, .6); */
  padding-top40rpx;
  padding-bottom90rpx;
  box-sizing: border-box;

}


.dialog-box {
  width100%;
  height100%;
  /* background-color: pink; */
}

.dialog-head {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  height60rpx;
  /* background-color: rgb(215, 255, 192); */
}

.dialog-title {
  width80%;
  height100%;
  font-size32rpx;
  font-family: PingFang SC;
  font-weight: bold;
  /* line-height: 40rpx; */
  colorrgba(000, .9);
  /* background-color: rgb(255, 254, 192); */

  display: flex;
  align-items: center;
  justify-content: center;
}

.close2ImgBox {
  width10%;
  height100%;
  display: flex;
  align-items: center;
}

.close2Img {
  width44rpx;
  height44rpx;
}

.dialog-content {
  heightcalc(100% - 60rpx);
  /* background-color: rgb(192, 237, 255); */
  box-sizing: border-box;
  padding40rpx 0;
}

/* 主体内容 */
.select-box {
  /* background-color: rgb(240, 230, 146); */
  display: flex;
  flex-wrap: wrap;
  justify-content: start;
  box-sizing: border-box;
  padding10rpx;
  padding0 0 30rpx 0rpx;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.select-item {
  width80%;
  height88rpx;
  line-height68rpx;
  background#f6f5f8;
  opacity1;
  border-radius40rpx;
  text-align: center;
  font-size32rpx;
  font-family: PingFang SC;
  font-weight400;
  color#151521;
  /* margin-right: 10rpx; */
  margin-bottom32rpx;
  box-sizing: border-box;
  display: flex;
  justify-content: center;
  align-items: center;
}

.selectedItem {
  background#ff5050;
  color#fff;
  border1px solid #ff5050;
}

.btnBox {
  width100%;
  /* height: auto; */
  display: flex;
  justify-content: center;
  align-items: center;
}

.btn {
  width90% !important;
  height88rpx;
  background#FF3B3B;
  opacity1;
  font-size32rpx;
  font-family: PingFang SC;
  font-weight500;
  color#FFFFFF;
  opacity1;
  border-radius48rpx;
  border: none;
  outline: none;
  position: absolute;
  bottom50rpx;
  left50%;
  transformtranslate(-50%0);
  display: flex;
  justify-content: center;
  align-items: center;
}
// // components/dialog/dialog.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {},

    /**
     * 组件的初始数据
     */
    data: {
        //弹窗显示控制
        showModalStatusfalse,
        // isShowDialog: false, //是否显示提示控件组件

        // 点击添加的数据
        tabData: {
            // title: '拒绝发货',
            val: ['库存''跨境现货''爆款''新品'],
            toValIndexnull,
        }, //需要传递的值
    },
    /**
     * 组件的方法列表
     */
    methods: {
        //点击显示底部弹出
        changeRangefunction ({
            this.showModal();
            console.log('我是弹窗打开----');
        },

        //底部弹出框
        showModalfunction ({
            // 背景遮罩层
            var animation = wx.createAnimation({
                duration50,
                timingFunction"linear",
                delay0
            })
            //this.animation = animation
            animation.translateY(50).step()
            this.setData({
                animationData: animation.export(),
                showModalStatustrue
            })
            setTimeout(function ({
                animation.translateY(0).step()
                this.setData({
                    animationData: animation.export()
                })
            }.bind(this), 50)
        },

        //点击背景面任意一处时,弹出框隐藏
        hideModalfunction (e{
            //弹出框消失动画
            var animation = wx.createAnimation({
                duration10,
                timingFunction"linear",
                delay0
            })
            //this.animation = animation
            animation.translateY(10).step()
            this.setData({
                animationData: animation.export(),
            })
            setTimeout(function ({
                animation.translateY(0).step()
                this.setData({
                    animationData: animation.export(),
                    showModalStatusfalse
                })
            }.bind(this), 10)
        },

        // 选择选项-----弹出框选择添加类型
        getValueTap(e) {
            console.log(e);
            let dialogid = e.currentTarget.dataset.dialogid;
            console.log(dialogid);
            this.setData({
                ['tabData.toValIndex']: dialogid, //更新
            })
            // var toNum = this.data.tabData.index;
        },
    },
    // 生命周期
    lifetimes: {
        readyfunction ({

        },
    }
})

以上就是关于“微信小程序如何实现底部弹出框封装”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

温馨提示×

网络异常,请检查网络