首页 > 编程语言 >微信小程序:弹框

微信小程序:弹框

时间:2024-01-29 15:45:21浏览次数:23  
标签:微信 程序 isShow height width 弹出 background 20rpx 弹框

一、自定义弹框组件

1、组件定义

在components目录下创建dialog文件夹,然后选择文件夹,右键,点击"新建Component",就会创建四个文件。如下所示:

wxml

<!--components/dialog/index.wxml-->
<view wx:if="{{isShow}}" class="tip-area">
  <view bindtap="hideClick" class="dialog-content">
    
    <view class="tip-content">
        <view class="title">
            <text>{{title}}</text>
        </view>
        <text  decode="true" style="margin-left:20rpx;">{{contentStr}}</text>
    </view>
    <view class="btn-content">
      {{btnStr}}
    </view>
  </view>
</view>

js

// components/dialog/index.js
Component({
    properties: {
      isShow: {//false:弹出框消息  true:弹出框显示
        value: false,
        type: Boolean
      },
      contentStr:{//弹出框需要显示的内容
        value: '内容',
        type: String,
      },
      title:{//弹出框需要显示的内容
        value: '标题',
        type: String,
      },
      btnStr:{//按钮文本(可进行自行扩展两个按钮)
        value: '确定',
        type: String,
      }
    },
    data: {
      timeId: null,//弹出框消息倒计时标识
    },
    observers: {//监听isShow属性的变化,来进行判断是否显示弹出框
      "isShow": function (value) {
        clearTimeout(this.data.timeId);
        if (value) {
          this.data.timeId = setTimeout(() => {
            this.setData({
              isShow: false,
            });
          }, 20000);
        }
      }
    },
    methods: {
      hideClick() {//点击弹出框的任意位置进行点击事件的传递,从而修改isShow的值,来控制弹出框消失
        this.triggerEvent('clickDialog', {
          isShow: false
        });
        clearTimeout(this.data.timeId);
      }
    },
  })

wxss

/* components/dialog/index.wxss */
.tip-area {
    position: absolute;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, .5);
  }
  
  .dialog-content{
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 80vw;
    height: auto;
    display: flex;
    flex-direction: column;
  }
  .dialog-content .title{
      width: 100%;
      text-align: center;
      line-height: 70rpx;
      height: 70rpx;
      /* background-color: green; */
      font-size: 36rpx;
      margin-bottom: 20rpx;
  }
  .tip-content {
      padding:40rpx;
    background: white;
    width: 80vw;
    height: 44vw;
    border-radius: 20rpx 20rpx 0 0;
    box-sizing: border-box;
    /* line-height: 34vw; */
    /* text-align: center; */
    font-size: 28rpx;
    color: black;
    border-bottom: .5rpx solid #e8e8e9;
    padding-top: 40rpx;
  }
  
  .btn-content{
    width: 100%;
    height: 10vw;
    border-radius: 0 0 20rpx 20rpx;
    background: white;
    text-align: center;
    line-height: 10vw;
    font-size: 32rpx;
  }

2、组件的使用

(1)、页面的json文件中引入

{
    "usingComponents": {
        "dialog": "/components/dialog/index"
    }
}

(2)、点击事件

<view class="rules_desc"  bindtap="showDialog">
                <text>规则说明</text>
            </view>

(3)、使用组件

<dialog bind:clickDialog="clickDialog" isShow="{{isShow}}" contentStr="&ensp;&ensp;积分以自然年为计算单位,三年为一个有效期,实行滚动清零,每年12月底执行。即您在第一年获得的积分,只要在第三年年底之前使用都有效。" btnStr="确定" title="积分规则说明"></dialog>

(4)、js中

Page({
  data: {
    isShow: false,//true:显示弹出框  false:隐藏弹出框
  },
  showDialog(e){//显示弹出框
    this.setData({
      isShow: true,
    })
  },
  clickDialog(e){//接受弹出框点击反馈监听事件,来进行隐藏弹出框
    this.setData({
      isShow: e.detail.isShow,
    })
  },
})

效果如下:

二、页面中写弹框组件

以下是弹框显示二维码的代码

wxml

<view wx:if="{{isShow}}" class="tip-area">
  <view class="dialog-content">
    <view class="tip-content">
        <view class="title">
            <text>二维码</text>
        </view>
        <view class="qrcode">
            <canvas id='qrcode' type="2d" style='width:300rpx;height:300rpx;' ></canvas>
        </view>
    </view>
    <view class="btn-content" bindtap="hideClick">
      取消
    </view>
  </view>
</view>

less

.tip-area {
    position: absolute;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, .5);
    .dialog-content{
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        width: 80vw;
        height: auto;
        display: flex;
        flex-direction: column;
        .tip-content {
            padding:40rpx;
            background: white;
            width: 80vw;
            height: 70vw;
            border-radius: 20rpx 20rpx 0 0;
            box-sizing: border-box;
            /* line-height: 34vw; */
            /* text-align: center; */
            font-size: 28rpx;
            color: black;
            border-bottom: .5rpx solid #e8e8e9;
            padding-top: 40rpx;
            .title{
                width: 100%;
                text-align: center;
                line-height: 70rpx;
                height: 70rpx;
                /* background-color: green; */
                font-size: 36rpx;
                margin-bottom: 20rpx;
            }
            .qrcode{
                width: 100%;
                display: flex;
                justify-content: center;
                align-items: center;
                margin-top: 60rpx;
                margin-bottom: 60rpx;
            }
        }
        .btn-content{
            width: 100%;
            height: 10vw;
            border-radius: 0 0 20rpx 20rpx;
            background: white;
            text-align: center;
            line-height: 10vw;
            font-size: 32rpx;
          }
      }
  }
  

js

data: {
    isShow: false,
    qrCodeLink: null,
    qrcodePath: null,
  },
  showQrcode(e){
    this.setData({
      isShow: true
    })
    console.log(e)
    const {code} = e.currentTarget.dataset;
    this.setData({
      qrCodeLink: code
    })
    this.createQrcode(code)
    
  },
  hideClick(){
    this.setData({
      isShow: false,
      qrCodeLink: null,
      qrcodePath: null
    })
  },
  // 生成二维码
createQrcode(code) {
  var that = this;
  const query = wx.createSelectorQuery()
  query.select('#qrcode')
    .fields({
      node: true,
      size: true
    })
    .exec((res) => {
      console.log(res)
      var canvas = res[0].node
 
      // 调用方法drawQrcode生成二维码
      QRCode({
        canvas: canvas,
        canvasId: 'qrcode',
        // width:that.createRpx2px(300),
        // width: that.createRpx2px(300),
        // padding: 10,
        // background: '#ffffff',
        // foreground: '#000000',
        text: code
      })
 
      // 获取临时路径(得到之后,想干嘛就干嘛了)
      wx.canvasToTempFilePath({
        canvasId: 'qrcode',
        canvas: canvas,
        x: 0,
        y: 0,
        success(res) {
          // console.log('二维码临时路径:', res.tempFilePath)
          that.setData({
            qrcodePath: res.tempFilePath,
            // isShow: true
          })
          
          // console.log('二维码临时路径:', that.data.qrcodePath)
        },
        fail(res) {
          console.error(res)
        }
      })
    })
  },

效果如下:

 

标签:微信,程序,isShow,height,width,弹出,background,20rpx,弹框
From: https://www.cnblogs.com/zwh0910/p/17582085.html

相关文章

  • 程序员 | 开发必备12个高效AI编程助手神器
    ⛳前言根据StackOverflow公布的开发者问卷调查报告,70%的受访者今年已在使用AI工具编程。AI浪潮下在程序员领域,未来,一定不是AI取代你,而掌握AI的人将取代你,这里整理12个高效AI助手,建议大家收藏使用~......
  • 通过LINUX驱动控制FPGA端PWM外设(LED) 通过应用程序命令传参随意修改寄存器的值(PWM波频
    用法:先下发下面的命令让kernel信息打印到串口:echo7417>/proc/sys/kernel/printk然后增加程序可执行性:chmod777pwmdriver_app  先执行./pwmdriver_app/dev/pwm400000200然后执行./pwm_driver_app/dev/pwm400000200,可以发现LED[1]......
  • 局域网电脑自动监控软件,微信泄密|QQ泄密|邮件泄密通通一网打尽
    本文一共:1035字|预计阅读时间:5分钟局域网电脑自动监控软件1.功能概述在当前信息时代,随着电脑网络的广泛应用,网络安全问题日益突出。为了有效应对各种潜在的泄密风险,域智盾软件推出了一款强大的局域网电脑自动监控软件。该软件具备微信、QQ、企业微信、钉钉、邮件等多方面监......
  • <<程序员修炼之道:从小工到专家>>读后感
    《程序员修炼之道:从小工到专家》是一本深刻揭示编程世界本质的著作。在阅读后,我深感这不仅仅是一本关于编程技术的书籍,更是一本关于职业发展、软件设计和人生态度的指南。首先,书中强调的代码质量和可读性给我留下深刻印象。作者强调写出高质量、易读懂的代码是每位程序员的责任。......
  • QT Creator12.0.1运行普通C/C++程序时候没有控制台输出
    问题:QTCreator12.0.1运行普通C/C++程序时候没有控制台输出菜单栏选择:[编辑]->[设置],按下图依次设置。启用终端输出,还有去掉内部终端输出的选项运行后控制台窗口正常弹出......
  • 剧本杀小程序app开发:开启沉浸式推理游戏新篇章
    随着社交媒体和移动设备的普及,人们对于线上娱乐的需求越来越高。在这样的背景下,剧本杀小程序app应运而生,它结合了角色扮演、推理和社交互动等元素,为玩家提供了一种全新的沉浸式游戏体验。本文将探讨剧本杀小程序app开发的关键要素和前景。一、剧本杀小程序app简介剧本杀小程序app是......
  • 微信小程序:生成二维码
    wxml<view><buttonbindtap='createQrcode'type="primary"style="width:400rpx;margin-top:400rpx;">生成二维码</button><canvasid='qrcode'type="2d"style='width:300rpx;height:30......
  • 程序运行过程中改变按钮的文本大小,你可以使用`configure`方
    在Python的Tkinter库中,你可以通过`font`参数来设置`Button`组件的文本大小²。以下是一个简单的例子:```pythonfromtkinterimport*root=Tk()button=Button(root,text="Hello",font=("Arial",20))button.pack()root.mainloop()```在这个例子中,我们创建了一个按钮并......
  • 苹果支付有哪些坑,为什么苹果支付比支付宝和微信容易丢单?
    苹果内购前言苹果内购苹果支付的难点方案设计1、商品设计2、用户和回执的绑定3、回调的重试充值冲遇到的问题点1、丢单2、充值成功,下发的物品不对3、处理退款苹果订阅1、配置服务端回调通知2、客户端通知;3、服务端定时轮询;StoreKit1对比2新的api......
  • 程序是怎样跑起来的
    二进制一般来说,二进制的位数是以8的倍数来增长的,比如8位、16位、32位,这是因为计算机处理信息的基本单位是8位2进制数,也称字节。字节是信息的基本单位。在内存和硬盘等设备中,数据是以字节为单位储存的,也是以字节为单位读写的,我们不能以比特为单位来读写。在程序中用十进制或字符来......