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

本文实例为大家分享了微信小程序底部弹出框封装的具体代码,供大家参考,具体内容如下

<!--index.wxml-->
<view>
  <button style="margin-top: 300px;" 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 {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: #000;
  opacity: 0.2;
  overflow: hidden;
  z-index: 1000;
  color: #fff;
}

/*对话框 */
.attr_box {
  background: #FFFFFF;
  opacity: 1;
  /* border-radius: 0px 0px 0px 0px; */
  /* height: 500rpx; */
  height: auto;
  width: 100%;
  overflow: hidden;
  position: fixed;
  bottom: 0;
  left: 0;
  z-index: 2000;
  background: #fff;
  /* background: rgba(66, 66, 66, .6); */
  padding-top: 40rpx;
  padding-bottom: 90rpx;
  box-sizing: border-box;

}

.dialog-box {
  width: 100%;
  height: 100%;
  /* background-color: pink; */
}

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

.dialog-title {
  width: 80%;
  height: 100%;
  font-size: 32rpx;
  font-family: PingFang SC;
  font-weight: bold;
  /* line-height: 40rpx; */
  color: rgba(0, 0, 0, .9);
  /* background-color: rgb(255, 254, 192); */

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

.close2ImgBox {
  width: 10%;
  height: 100%;
  display: flex;
  align-items: center;
}

.close2Img {
  width: 44rpx;
  height: 44rpx;
}

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

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

.select-item {
  width: 80%;
  height: 88rpx;
  line-height: 68rpx;
  background: #f6f5f8;
  opacity: 1;
  border-radius: 40rpx;
  text-align: center;
  font-size: 32rpx;
  font-family: PingFang SC;
  font-weight: 400;
  color: #151521;
  /* margin-right: 10rpx; */
  margin-bottom: 32rpx;
  box-sizing: border-box;
  display: flex;
  justify-content: center;
  align-items: center;
}

.selectedItem {
  background: #ff5050;
  color: #fff;
  border: 1px solid #ff5050;
}

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

.btn {
  width: 90% !important;
  height: 88rpx;
  background: #FF3B3B;
  opacity: 1;
  font-size: 32rpx;
  font-family: PingFang SC;
  font-weight: 500;
  color: #FFFFFF;
  opacity: 1;
  border-radius: 48rpx;
  border: none;
  outline: none;
  position: absolute;
  bottom: 50rpx;
  left: 50%;
  transform: translate(-50%, 0);
  display: flex;
  justify-content: center;
  align-items: center;
}
// // components/dialog/dialog.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {},

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

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

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

        //点击背景面任意一处时,弹出框隐藏
        hideModal: function (e) {
            //弹出框消失动画
            var animation = wx.createAnimation({
                duration: 10,
                timingFunction: "linear",
                delay: 0
            })
            //this.animation = animation
            animation.translateY(10).step()
            this.setData({
                animationData: animation.export(),
            })
            setTimeout(function () {
                animation.translateY(0).step()
                this.setData({
                    animationData: animation.export(),
                    showModalStatus: false
                })
            }.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: {
        ready: function () {

        },
    }
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 使用微信小程序开发弹出框应用实例详解

    view class="container" class="zn-uploadimg"> <button type="primary"bindtap="showok">消息提示框</button> <button type="primary"bindtap="modalcnt">模态弹窗</button> <button typ

  • 微信小程序实现弹出框提交信息

    本文实例为大家分享了微信小程序实现弹出框提交信息的具体代码,供大家参考,具体内容如下 <view class="navSm" bindtap="toolNo">       <image src="../../images/idx4.png" class="mavimg" mode="aspectFit"></image>       <view class=&qu

  • 微信小程序实现底部弹出框

    微信小程序的底部弹出框,供大家参考,具体内容如下 wxml <!-- 弹出框 start --> <view class="dialog_screen" bindtap="hideModal" wx:if="{{showModalStatus}}"></view> <view animation="{{animationData}}" class="dialog_attr_b

  • 微信小程序商品详情页底部弹出框

    电商项目中商品详情页,加入购物车或者下单时可以选择商品属性的弹出框,通过设置view的平移动画,达到从底部弹出的样式 1.js代码(一般情况下只调用显示对话框的函数,当点击对话框外部的时候,对话框可以消失) //显示对话框 showModal: function () { // 显示遮罩层 var animation = wx.createAnimation({ duration: 200, timingFunction: "linear", delay: 0 }) this.anim

  • JS中微信小程序自定义底部弹出框

    实现微信小程序底部弹出框效果,代码分为html,css和js两部分,具体代码详情大家参考下本文. html <view class="commodity_screen" bindtap="hideModal" wx:if="{{showModalStatus}}"></view> <view animation="{{animationData}}" class="commodity_a

  • 微信小程序自定义底部弹出框动画

    微信小程序之自定义底部弹出框动画,供大家参考,具体内容如下 最近做小程序时,会经常用到各种弹框.直接做显示和隐藏虽然也能达到效果,但是体验性太差,也比较简单粗暴.想要美美地玩,添加点动画还是非常有必要的.下面做一个底部上滑的弹框. wxml <view class="modals modals-bottom-dialog" hidden="{{hideModal}}"> <view class="modals-cancel" b

  • 微信小程序自定义可滚动的弹出框

    本文实例为大家分享了微信小程序自定义可滚动弹出框的具体代码,供大家参考,具体内容如下 最近在写一个装修的活动,规则是点击按钮弹出加上相应的动画. 首先我们需要一个按钮触发显示(如图,点击详细规则显示规则模态框,图二右边的滚动条在手机上不显示) 思路:小程序自己的模态框不能写样式,这是个比较尴尬的情况,这是一个比较小白的解决方案: 在前端写一个视窗,默认让它隐藏 我这边是用showModel来控制,默认给它false,当点击规则按钮是将showModel的值改为true,点击关闭按钮将showMo

  • 微信小程序商品详情页的底部弹出框效果

    电商项目中商品详情页,加入购物车或者下单时可以选择商品属性的弹出框,通过设置view的平移动画,达到从底部弹出的样式 1.js代码(一般情况下只调用显示对话框的函数,当点击对话框外部的时候,对话框可以消失) //显示对话框 showModal: function () { // 显示遮罩层 var animation = wx.createAnimation({ duration: 200, timingFunction: "linear", delay: 0 }) this.anim

  • 微信小程序自定义底部弹出框功能

    本文实例为大家分享了微信小程序自定义底部弹出框的具体代码,供大家参考,具体内容如下 实现这么一个功能,点击选项进行选择,效果是从底部弹出选项框(带滑出动画),选择了某项或者点击其他地方,隐藏(带滑出动画).效果图如下: 可适用于任何场景,如普通选项(如图)或者类似商城小程序选择商品属性的弹出框.只需要把内容替换自己需要的即可. 1. wxml代码 <view class="wrap"> <view bindtap="showModal"> &

  • 微信小程序自定义底部弹出框

    本文实例为大家分享了微信小程序底部弹出框展示的具体代码,供大家参考,具体内容如下 效果图: html <view class="commodity_screen" bindtap="hideModal" wx:if="{{showModalStatus}}"></view> <view animation="{{animationData}}" class="commodity_attr

随机推荐