经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » 微信小程序 » 查看文章
微信小程序带动画弹窗组件使用方法详解
来源:jb51  时间:2018/11/28 9:31:21  对本文有异议

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

基本效果如下:

具体实现如下:

第一步:

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

第二步上代码:

popup.wxml

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

popup.wxss

  1. .container{font-size:15px;color:#666;font-weight: bold;z-index:2;position:fixed;width:100vw;height:100vh;}
  2. .wrap{position:fixed;top:0;left:0;bottom:0;right:0;}
  3. .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;}
  4. .wx-popup-title {width: 100%;padding: 20rpx 0;text-align: center;font-size: 40rpx;border-bottom: 2rpx solid #89cfea;}
  5. .wx-popup-con {margin: 60rpx 10rpx;text-align: center;}
  6. .wx-popup-btn {display: flex;justify-content: space-around;margin-bottom: 40rpx;}
  7. .wx-popup-btn text {display: flex;align-items: center;justify-content: center;width: 30%;height: 88rpx;border: 2rpx solid #ccc;border-radius: 88rpx;}
  8. .btn-colse{width:35px;height:35px;position:absolute;bottom:-60px;left:50%;margin-left:-17.5px;}
  9. .wrapAnimate{animation: wrapAnimate 1s linear forwards}
  10. @keyframes wrapAnimate{
  11. 0%{}
  12. 100%{background:rgba(0,0,0,0.7);}
  13. }
  14. .wrapAnimateOut{animation: wrapAnimateOut 1s 0.2s linear forwards}
  15. @keyframes wrapAnimateOut{
  16. 0%{background:rgba(0,0,0,0.7);}
  17. 100%{background:rgba(0,0,0,0);}
  18. }
  19. .popupAnimate{animation: popupAnimate 1.2s linear forwards}
  20. @keyframes popupAnimate{
  21. 0%{}
  22. 60%{top:47%;opacity: 1;}
  23. 80%{top:53%;opacity: 1;}
  24. 100%{top:50%;opacity: 1;}
  25. }
  26. .popupAnimateOut{animation: popupAnimateOut 1.2s linear forwards}
  27. @keyframes popupAnimateOut{
  28. 0%{top:50%;opacity: 1;}
  29. 20%{top:47%;opacity: 1;}
  30. 100%{}
  31. }

popup.js

  1. Component({
  2. options: {
  3. multipleSlots: true // 在组件定义时的选项中启用多slot支持
  4. },
  5. /*组件的属性列表*/
  6. properties: {
  7. title: {
  8. type: String,
  9. value: '标题'
  10. },
  11. // 弹窗内容
  12. content: {
  13. type: String,
  14. value: '内容'
  15. },
  16. // 弹窗取消按钮文字
  17. btn_no: {
  18. type: String,
  19. value: '取消'
  20. },
  21. // 弹窗确认按钮文字
  22. btn_ok: {
  23. type: String,
  24. value: '确定'
  25. }
  26. },
  27. /* 组件的初始数据 */
  28. data: {
  29. flag: true,
  30. bgOpacity:0,
  31. wrapAnimate:'wrapAnimate',
  32. popupAnimate:'popupAnimate'
  33. },
  34. /* 组件的方法列表 */
  35. methods: {
  36. //隐藏弹框
  37. hidePopup: function () {
  38. const that = this;
  39. this.setData({ bgOpacity: 0.7, wrapAnimate: "wrapAnimateOut", popupAnimate:"popupAnimateOut"})
  40. setTimeout(function(){
  41. that.setData({flag: false})
  42. },1200)
  43. },
  44. /* 内部私有方法建议以下划线开头 triggerEvent 用于触发事件 */
  45. _error() {//触发取消回调
  46. this.triggerEvent("error")
  47. },
  48. _success() {//触发成功回调
  49. this.triggerEvent("success");
  50. }
  51. }
  52. })

popup.json

  1. {
  2. "component": true,
  3. "usingComponents": {}
  4. }
  5.  
  6.  

第三步引用组件:

index.json

  1. {
  2. "usingComponents": {
  3. "popup":"/components/popup/popup"
  4. }
  5. }

index.wxml

  1. <popup
  2. id='popup'
  3. title='弹窗组件'
  4. content='学会了吗'
  5. btn_no='没有'
  6. btn_ok='学会了'
  7. binderror="_error"
  8. bindsuccess="_success"
  9. >
  10.  
  11. </popup>
  12.  

index.js

  1. Page({
  2. showPopup() {
  3. this.popup.showPopup();
  4. },
  5. //取消事件
  6. _error() {
  7. console.log('你点击了取消');
  8. this.selectComponent("#popup").hidePopup();
  9. },
  10. //确认事件
  11. _success() {
  12. console.log('你点击了确定');
  13. this.selectComponent("#popup").hidePopup();
  14. }
  15. })

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号