微信小程序带动画弹窗组件使用方法详解

 更新时间:2018年11月27日 14:34:02   作者:孙智斌  
这篇文章主要为大家详细介绍了微信小程序带动画弹窗组件的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了微信小程序带动画弹窗的具体代码,供大家参考,具体内容如下

基本效果如下:

具体实现如下:

第一步:

新建一个 components 文件夹,用于存放我们以后开发中的所用组件,在 components 组件中新建一个popup文件夹来存放我们的弹窗组件,在popup下右击新建 Component 并命名为 popup 后,会生成对应的 json wxml wxss js 4个文件,也就是一个自定义组件的组成部分,此时项目结构应该如下图所示:

第二步上代码:

popup.wxml

<view hidden="{{!flag}}" class='container' style=''>
 <view bindtap='_error' class='wrap {{wrapAnimate}}' style='background:rgba(0,0,0,{{bgOpacity}});'></view>
 <view class='popup-container {{popupAnimate}}'>
 <view class="wx-popup-title">{{title}}</view>
 <view class="wx-popup-con">{{content}}</view>
 <view class="wx-popup-btn">
  <text class="btn-no" bindtap='_error'>{{btn_no}}</text>
  <text class="btn-ok" bindtap='_success'>{{btn_ok}}</text>
 </view>
 <image bindtap='_error' src='../../image/close.png' mode='widthFix' class='btn-colse'></image>
 </view>
</view>

popup.wxss

.container{font-size:15px;color:#666;font-weight: bold;z-index:2;position:fixed;width:100vw;height:100vh;}
.wrap{position:fixed;top:0;left:0;bottom:0;right:0;}
.popup-container {position: fixed;left: 50%;top: 100%;width: 80%;max-width: 600rpx;border: 2rpx solid #ccc;border-radius: 10rpx;box-sizing: bordre-box;transform: translate(-50%, -50%);background: #fff;opacity: 0;}
.wx-popup-title {width: 100%;padding: 20rpx 0;text-align: center;font-size: 40rpx;border-bottom: 2rpx solid #89cfea;}
.wx-popup-con {margin: 60rpx 10rpx;text-align: center;}
.wx-popup-btn {display: flex;justify-content: space-around;margin-bottom: 40rpx;}
.wx-popup-btn text {display: flex;align-items: center;justify-content: center;width: 30%;height: 88rpx;border: 2rpx solid #ccc;border-radius: 88rpx;}
.btn-colse{width:35px;height:35px;position:absolute;bottom:-60px;left:50%;margin-left:-17.5px;}
.wrapAnimate{animation: wrapAnimate 1s linear forwards}
@keyframes wrapAnimate{
 0%{}
 100%{background:rgba(0,0,0,0.7);}
}
.wrapAnimateOut{animation: wrapAnimateOut 1s 0.2s linear forwards}
@keyframes wrapAnimateOut{
 0%{background:rgba(0,0,0,0.7);}
 100%{background:rgba(0,0,0,0);}
}
.popupAnimate{animation: popupAnimate 1.2s linear forwards}
@keyframes popupAnimate{
 0%{}
 60%{top:47%;opacity: 1;}
 80%{top:53%;opacity: 1;}
 100%{top:50%;opacity: 1;}
}
.popupAnimateOut{animation: popupAnimateOut 1.2s linear forwards}
@keyframes popupAnimateOut{
 0%{top:50%;opacity: 1;}
 20%{top:47%;opacity: 1;}
 100%{}
}

popup.js

Component({
 options: {
 multipleSlots: true // 在组件定义时的选项中启用多slot支持
 },
 /*组件的属性列表*/
 properties: {
 title: {
  type: String,
  value: '标题'
 },
 // 弹窗内容
 content: {
  type: String,
  value: '内容'
 },
 // 弹窗取消按钮文字
 btn_no: {
  type: String,
  value: '取消'
 },
 // 弹窗确认按钮文字
 btn_ok: {
  type: String,
  value: '确定'
 }
 },
 /* 组件的初始数据 */
 data: {
 flag: true,
 bgOpacity:0,
 wrapAnimate:'wrapAnimate',
 popupAnimate:'popupAnimate'
 },
 /* 组件的方法列表 */
 methods: {
 //隐藏弹框
 hidePopup: function () {
  const that = this;
  this.setData({ bgOpacity: 0.7, wrapAnimate: "wrapAnimateOut", popupAnimate:"popupAnimateOut"})
  setTimeout(function(){
  that.setData({flag: false})
  },1200)
 },
 /* 内部私有方法建议以下划线开头 triggerEvent 用于触发事件 */
 _error() {//触发取消回调
  this.triggerEvent("error")
 },
 _success() {//触发成功回调
  this.triggerEvent("success");
 }
 }
})

popup.json

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


第三步引用组件:

index.json

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

index.wxml

<popup
 id='popup'
 title='弹窗组件'
 content='学会了吗'
 btn_no='没有'
 btn_ok='学会了'
 binderror="_error"
 bindsuccess="_success"
 >

</popup>

index.js

Page({
 showPopup() {
 this.popup.showPopup();
 },
 //取消事件
 _error() {
 console.log('你点击了取消');
 this.selectComponent("#popup").hidePopup();
 },
 //确认事件
 _success() {
 console.log('你点击了确定');
 this.selectComponent("#popup").hidePopup();
 }
})

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

相关文章

  • 微信小程序下拉菜单效果的实例代码

    微信小程序下拉菜单效果的实例代码

    这篇文章主要介绍了微信小程序下拉菜单效果的实例代码,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-05-05
  • 七个基于JavaScript实现的情人节表白特效

    七个基于JavaScript实现的情人节表白特效

    情人节将至 程序员证明自己不是直男的时候到啦 我们也有自己的专属代码浪漫。本文将介绍七个利用JavaScript实现的情人节表白特效,需要的可以参考一下
    2022-01-01
  • javascript 打字游戏实现代码

    javascript 打字游戏实现代码

    javascript 打字游戏实现代码,非常不错的效果,功能还不是很完善,喜欢的朋友可以参考下。
    2010-04-04
  • Javascript 一些需要注意的细节(必看篇)

    Javascript 一些需要注意的细节(必看篇)

    下面小编就为大家带来一篇Javascript 一些需要注意的细节(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • JavaScript跨域方法汇总

    JavaScript跨域方法汇总

    这篇文章主要介绍了JavaScript跨域方法汇总,本文总结了12种JavaScript的跨域方法,需要的朋友可以参考下
    2014-10-10
  • 浅谈layui里的上传控件问题

    浅谈layui里的上传控件问题

    今天小编就为大家分享一篇浅谈layui里的上传控件问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-09-09
  • js实现3D照片墙效果

    js实现3D照片墙效果

    这篇文章主要介绍了js实现3D照片墙效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-10-10
  • 如何消除inline-block属性带来的标签间间隙

    如何消除inline-block属性带来的标签间间隙

    这篇文章主要介绍了如何消除inline-block属性带来的标签间间隙的相关资料,需要的朋友可以参考下
    2016-03-03
  • JS表的模拟方法

    JS表的模拟方法

    这篇文章主要介绍了JS表的模拟方法,涉及javascript模拟表的生成、添加与删除节点的操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-02-02
  • JS this关键字在ajax中使用出现问题解决方案

    JS this关键字在ajax中使用出现问题解决方案

    这篇文章主要介绍了JS this关键字在ajax中使用出现问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07

最新评论