在微信小程序中自定义从底部弹出框的组件,然后在主页面中使用

component目录下:popup为组件
pages目录下:bottom为要加入组件的页面
(具体效果展示在底部

1.首先写组件popup
popup.wxml

<view class="half-screen">
  <!--屏幕背景变暗的背景  -->
  <view class="background_screen" bindtap="hideModal" wx:if="{{showModalStatus}}"></view>
  <!--弹出框  -->
  <view animation="{{animationData}}" class="attr_box" wx:if="{{showModalStatus}}">
    <image class="back" src="../../assets/back.png" bindtap="hideModal"></image>
    <view class="ctTitle">{{title}}</view>
    <view class="thresholdBtn">
      <button id="yes" class="confirm" size="mini" type="primary" bindtap="hideModal">确定</button>
    </view>
  </view>
</view>

popup.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 {
 
  
  height: 230px;
  width: 100%;
  overflow: hidden;
  position: fixed; 
  bottom: 0;
  left: 0;
  z-index: 2000;
  background: #fff;
  padding-top: 20px;
}
.attr_box .back {
  margin-top: -30px;
  height: 20px;
  width: 20px;
}
.attr_box .ctTitle{
  margin-top: -30px;
  margin-left: 120px; 
}


.thresholdBtn .confirm{
  margin-bottom: -180px;
  height: 30px;
  width: 80px;
  margin-left: 120px;
 
}

popup.js

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    title: {
      type: String,
      value: '标题'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    //弹窗显示控制 
    showModalStatus: false
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //点击显示底部弹出
    changeRange: function () {
      this.showModal();
    },

    //底部弹出框
    showModal: function () {
      // 背景遮罩层
      var animation = wx.createAnimation({
        duration: 200,
        timingFunction: "linear",
        delay: 0
      })
      //this.animation = animation
      animation.translateY(300).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: true
      })
      setTimeout(function () {
        animation.translateY(0).step()
        this.setData({
          animationData: animation.export()
        })
      }.bind(this), 200)
    },
   
    //点击背景面任意一处时,弹出框隐藏
    hideModal: function () {
      //弹出框消失动画
      var animation = wx.createAnimation({
        duration: 200,
        timingFunction: "linear",
        delay: 0
      })
      //this.animation = animation
      animation.translateY(300).step()
      this.setData({
        animationData: animation.export(),
      })
      setTimeout(function () {
        animation.translateY(0).step()
        this.setData({
          animationData: animation.export(),
          showModalStatus: false
        })
      }.bind(this), 200)
    },
  }
})

popup.json

{
  "component": true,
  "usingComponents": {}
}

2.然后是主页面
bottom.json

{
  "usingComponents": {
    "popup": "/components/popup/popup"
  }
}

bottom.wxml

<view bindtap="changeRange">请点击</view>
<popup id='popup' title='我是标题'></popup>

bottom.js

Page({
  onReady: function(){
    this.popup= this.selectComponent("#popup");
  },
  changeRange(){
    this.popup.changeRange();
  }
})

效果图
在这里插入图片描述

微信小程序使用 Vant UI 的弹出层和弹出,需要进行以下步骤: 1. 安装 Vant UI 库 使用 npm 安装 Vant UI 库: ``` npm i vant-weapp -S --production ``` 2. 引入弹出层和弹出组件 在需要使用弹出层和弹出页面,引入 Vant UI 的弹出层和弹出组件: ```javascript import { Dialog, Popup } from 'vant-weapp'; ``` 3. 在页面使用弹出层和弹出页面使用弹出层和弹出组件,例如: ```html <!-- 弹出 --> <van-dialog id="my-dialog" title="标题" show="{{ isShow }}"> <view>这是弹出的内容</view> </van-dialog> <!-- 弹出层 --> <van-popup id="my-popup" title="标题" show="{{ isShow }}"> <view>这是弹出层的内容</view> </van-popup> ``` 其,`isShow` 为控制弹出层和弹出显示的变量,需要在页面的 `data` 定义和初始化。 4. 控制弹出层和弹出的显示和隐藏 在页面,可以通过修改 `isShow` 变量的值,控制弹出层和弹出的显示和隐藏,例如: ```javascript // 显示弹出 this.setData({ isShow: true, }); // 隐藏弹出 this.setData({ isShow: false, }); ``` ```javascript // 显示弹出层 this.setData({ isShow: true, }); // 隐藏弹出层 this.setData({ isShow: false, }); ``` 以上就是在微信小程序使用 Vant UI 的弹出层和弹出的步骤和示例。注意,在微信小程序,需要使用 `id` 属性来绑定弹出层和弹出,同时也需要在页面的 `onLoad` 生命周期,调用 `this.selectComponent` 方法来获取弹出层和弹出的实例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值