微信小程序 自定义d窗

微信小程序 自定义d窗,第1张

 <button bindtap="powerDrawer" type="primary" size="mini" style="width: 55%" data-statu="open">中奖记录</button>  

<!-- 中间名单d窗 -->

<view class="drawer_div">

  <view class="drawer_screen" bindtap="powerDrawer" data-statu="close" wx:if="{{showModalStatus}}"></view>

    <view animation="{{animationPrize}}" class="drawer_box" wx:if="{{showModalStatus}}">

      <view class="drawer_title">

        中奖记录

        <image src="../img/x@2x.png" bindtap="powerDrawer" data-statu="close"></image>

      </view>

      <view class="drawer_content">

        <view class="top grid">

          <text>

            名称:啦啦啦啦啦啦啦啦

          </text>

          <text>

            时间:2020-02-20

          </text>

        </view>

        <view class="top grid">

          <text>

            名称:啦啦啦啦啦啦啦啦

          </text>

          <text>

            时间:2020-02-20

          </text>

        </view>

    </view>

  </view>

</view>

.drawer_screen {

  width: 100%

  height: 100%

  position: fixed

  top: 0

  left: 0

  z-index: 1000

  background: #000

  opacity: 0.5

  overflow: hidden

}

/*content*/

.drawer_box {

  width: 650rpx

  overflow: hidden

  position: fixed

  top: 50%

  left: 0

  z-index: 1001

  background: #FAFAFA

  margin: -150px 50rpx 0 50rpx

  border-radius: 3px

}

.drawer_title{

  padding:30rpx 20rpx

  font-size: 36rpx

  text-align: center

  position: relative

}

.drawer_title image{

  display: inline-block

  width: 30rpx

  height: 30rpx

  position: absolute

  right: 9px

  top: 10px

}

.drawer_content {

  height: 210px

  overflow-y: scroll /*超出父盒子高度可滚动*/

  padding: 0 20rpx

}

.top{

  display: flex

  justify-content: space-between

  border-bottom: 1px dashed #ccc

}

.top text{

  display: inline-block

  height: 60rpx

  line-height: 20rpx

}

data: {

animationPrize: {},

}

powerDrawer: function (e) {

    // console.log(e)

    let currentStatu = e.currentTarget.dataset.statu

    this.util(currentStatu)

  },

  util: function (currentStatu) {

    /* 动画部分 */

    // 第1步:创建动画实例

    var animation = wx.createAnimation({

      duration: 200,  //动画时长

      timingFunction: "linear", //线性

      delay: 0  //0则不延迟

    })

    // 第2步:这个动画实例赋给当前的动画实例

    this.animation = animation

    // 第3步:执行第一组动画

    animation.opacity(0).rotateX(-100).step()

    // 第4步:导出动画对象赋给数据对象储存

    this.setData({

      animationPrize: animation.export()

    })

    // 第5步:设置定时器到指定时候后,执行第二组动画

    setTimeout(function () {

      // 执行第二组动画

      animation.opacity(1).rotateX(0).step()

      // 给数据对象储存的第一组动画,更替为执行完第二组动画的动画对象

      this.setData({

        animationPrize: animation

      })

      //关闭

      if (currentStatu == "close") {

        this.setData({

          showModalStatus: false

        })

      }

    }.bind(this), 200)

    // 显示

    if (currentStatu == "open") {

      this.setData({

        showModalStatus: true

      })

    }

  },

微信小程序自带的API中的页面交互功能,虽然提示功能非常全面,但是所有的交互API中是没有可以自己在提示框中输入文本的功能,那么现在我们来自己做这样的一个提示框(可以带有输入功能),在提示框输入完内容之后,点击确定,可以将文本内容返回,点击取消则可以回到之前的状态。

(在这里,主页面的布局可以根据每个人的想法来布局,这里展示的css之后展示提示框部分的)

1、首先打开微信开发者工具,建立一个代码模板,那么我们这个提示框就是写在这个页面上。

    这里我们主页面叫做index

2、在基本页面中写上一个姓名的文本,当前姓名用<text>{{stuName}}</text>表示,然后为一个button按钮,再在js文件中,建立相应的点击事件以及stuName的信息。这样,一个原始页面就写好了。

下面我们开始d出框页面的制作

<view class='toast-box' hidden='{{!ifName}}'>

<view class='toastbg'></view>

<view class='showToast'>

<view class='toast-title'>

<text>修改姓名</text>

</view>

<view class='toast-main'>

<view class='toast-input'>

<input placeholder='请输入姓名' bindinput='setValue' data-name='stuEidtName'></input>

</view>

</view>

<view class='toast-button'>

<view class='button1'>

<button catchtap='cancel'>取消</button>

</view>

<view class='button2'>

<button catchtap='confirm'>确定</button>

</view>

</view>

</view>

</view>

3、我们可以发现,点击按钮后d出输入框,如果点击除取消和确定之外的地方,是不会有反应的。为了做到这个功能,我们用一个绝对位置的渲染层(toastbg),覆盖住整个页面,并且如果你的页面长度没有滚动的话,请输入min—height:100vh,如果页面发生滚动,请把长度控制在height:100%即可看到整个页面都被覆盖。并且这个覆盖的页面要表现为透明,opacity:0.2,即可

4、bindinput为写文本时所触发的事件,data-name为文本数据所保存的地方,在js中我们可以把这个数据打印出来,会发现我们所输入的文本信息。

以下为css的代码

.toast-box {

width: 100%

height: 100%

opacity: 1

position: fixed

top: 0px

left: 0px

}

.toastbg {

opacity: 0.2

background-color: black

position: absolute

width: 100%

min-height: 100vh

}

.showToast {

position: absolute

opacity: 1

width: 70%

margin-left: 15%

margin-top: 40%

}

.toast-title {

padding-left: 5%

background-color: #2196f3

color: white

padding-top: 2vh

padding-bottom: 2vh

border-top-right-radius: 16rpx

border-top-left-radius: 16rpx

}

.toast-main {

padding-top: 2vh

padding-bottom: 2vh

background-color: white

text-align: center

}

.toast-input {

margin-left: 5%

margin-right: 5%

border: 1px solid #ddd

padding-left: 2vh

padding-right: 2vh

padding-top: 1vh

padding-bottom: 1vh

}

.toast-button {

display: flex

}

.button1 {

width: 50%

}

.button2 {

width: 50%

}

.button1 button {

width: 100%

background-color: white

color: red

border-radius: 0px

border-bottom-left-radius: 16rpx

}

.button2 button{

width: 100%

background-color: white

color: black

border-radius: 0px

border-bottom-right-radius: 16rpx

}

.picker {

padding-top: 1vh

padding-bottom: 1vh

}

我们可以根据自己的喜欢,对提示框的样式进行改变

5、编写js代码,我们需要实现以下一些基本功能(点击出现d窗,取消不改变数据值,确定进行判断数据值,若为空则不能改变,否则可以改变,并且主页面上的内容要变为相应改变后的内容)

6、给最外层的d窗附上hidden(如图所示),为这个值初始为false,点击按钮后触发事件,改false为true,这样即可点击出现d窗。

7、为取消按钮附上点击事件,与hidden的部分刚好相反即可。

8、为书写文本绑定事件,上述代码中命名为setValue,这个函数我们传入一个event进去,将其打印,我们可以发现在其的detail中有我们刚刚所书写的内容,我们将这个值,传给js中data一个属性,这里我们命名为edit。

9、为确定绑定事件,用this.data.edit将这个值进行判断,若为空,我们用wx.showToast提示用户信息没有填写完整,并且页面不会改变。若不为空,则我们setData一下我们的stuName为这个edit的值,并且重新把hidden的属性值改为false。

10、返回到初始页面我们就可以看到我们自己做得一个提示框,并且具有修改值的功能


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12124646.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-21
下一篇2023-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存