uniapp 电商app 监听页面返回按键实现功能

旧城等待, 2021-07-26 02:27 944阅读 0赞

监听页面返回按键

电商app很多地方都是需要监听返回按键的,如果点击了返回则弹窗提示信息,用户点击“确认”则返回,否则页面不返回。

以电商app支付页面为例,如果在支付页面进行页面返回,则需要提示用户是否退出支付。

页面如下:
在这里插入图片描述

  1. data(){
  2. return{
  3. giveUp:false,
  4. backFlag:false
  5. }
  6. },
  7. onBackPress(e) {
  8. if(this.giveUp){
  9. return;
  10. }
  11. if(e.from == "backbutton"){
  12. this.backFlag = true;
  13. return true;
  14. }
  15. },

弹窗部分代码:

  1. <view class="tanchuang" v-if="backFlag" @click="backFun">
  2. <view class="toast" @click.stop>
  3. <view class="toast-tit">提示</view>
  4. <view class="toast-con">
  5. 确定要放弃本次支付吗?
  6. </view>
  7. <view class="toast-btns">
  8. <text @click="backFun">放弃支付</text>
  9. <text @click="retypePay">继续支付</text>
  10. </view>
  11. </view>
  12. </view>

所用到的函数代码如下:

  1. retypePay(){
  2. this.toastFlag = false;
  3. this.backFlag = false;
  4. },
  5. backFun(){
  6. this.backFlag = false;
  7. this.giveUp = true;
  8. uni.navigateBack();
  9. },

弹窗css样式如下:

  1. .tanchuang{
  2. position: fixed;
  3. bottom:0;
  4. left:0;
  5. top:0;
  6. right:0;
  7. background:rgba(0,0,0,.4);
  8. .toast{
  9. position: absolute;
  10. top:50%;
  11. left:50%;
  12. transform: translate(-50%,-50%);
  13. width:560upx;
  14. height:424upx;
  15. background:#fff;
  16. text-align: center;
  17. border-radius: 10upx;
  18. overflow: hidden;
  19. .toast-btns{
  20. position: absolute;
  21. bottom:0;
  22. left:0;
  23. width:100%;
  24. height:80upx;
  25. line-height: 80upx;
  26. border-top:1px solid #EEEEEE;
  27. color:#9A9A9A;
  28. font-size:30upx;
  29. display: flex;
  30. align-items: center;
  31. text{
  32. flex:1;
  33. text-align: center;
  34. }
  35. text:last-child{
  36. color:#FF4C4C;
  37. border-left:1px solid #EEEEEE;
  38. }
  39. }
  40. .toast-btn{
  41. width:490upx;
  42. height:70upx;
  43. line-height: 70upx;
  44. background:#FF4C4C;
  45. border-radius: 10upx;
  46. color:#fff;
  47. font-size:30upx;
  48. margin:0 auto;
  49. }
  50. .toast-con{
  51. height:232upx;
  52. width:450upx;
  53. margin:0 auto;
  54. text-align: left;
  55. padding:0 20upx;
  56. box-sizing: border-box;
  57. font-size:26upx;
  58. color:#343434;
  59. line-height: 40upx;
  60. display: flex;
  61. align-items: center;
  62. }
  63. .toast-tit{
  64. height:80upx;
  65. line-height: 80upx;
  66. background:linear-gradient(to right,#FF4C4C,#FF9A6C);
  67. font-size:30upx;
  68. color:#fff;
  69. }
  70. }
  71. }

发表评论

表情:
评论列表 (有 0 条评论,944人围观)

还没有评论,来说两句吧...

相关阅读