微信小程序实现时间进度条功能

前端之家收集整理的这篇文章主要介绍了微信小程序实现时间进度条功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_0@关于答题类,或者一些游戏环节的小程序需要用到时间进度条,该功能怎么实现?看下面源码

  1. <view class='out' style='margin-top:10px'>
  2. <view class='in' style='width:{{progressWidth}}%'></view>
  3. </view>
  4. <view class='caozuo'>
  5. <text>{{progressTime}}秒</text>
  6. <text bindtap='playbtn' data-change='1'>{{playPausetips}}</text>
  7. </view>
@H_301_0@CSS:

  1. .out {margin-left:auto;margin-right:auto;width:250px;height:20px;border:1px solid red;border-radius:20px;overflow:hidden;}
  2. .in {height:100%;background-color:red;}
  3. .caozuo{font-size:14px;color:#333;margin-left:auto;margin-right:auto;width:250px;margin-top:10px;display: flex;justify-content:space-between}
@H_301_0@JS:

  1. Page({
  2. data: {
  3. progressWidth:0,progressTime:60,mark:true,playPausetips:"开始"
  4. },playbtn() {
  5. let that = this;
  6. let mark = that.data.mark;
  7. if (mark){
  8. that.timer = setInterval(that.run,1000); //that.timer关键点
  9. wx.showToast({
  10. title: '开始',})
  11. that.setData({
  12. mark:false,playPausetips:"暂停"
  13. })
  14. }else{
  15. clearInterval(that.timer);
  16. wx.showToast({
  17. title: '暂停',})
  18. that.setData({
  19. mark: true,playPausetips: "开始"
  20. })
  21. }
  22. },run(){
  23. let that = this;
  24. let totalProgressTime = 60 //秒
  25. let progressWidth = that.data.progressWidth; //显示进度
  26. let progressTime = that.data.progressTime; //时间
  27.  
  28. if (progressWidth === 100) {
  29. wx.showToast({
  30. title: '结束回调处理',})
  31. clearInterval(that.timer);
  32. that.setData({
  33. progressTime: totalProgressTime,//进度条需要总时间s
  34. progressWidth: 100,//进度100%
  35. progressTime: 60
  36. })
  37. return;
  38. }
  39. progressTime--;
  40. progressWidth = (totalProgressTime - progressTime) * (100 / 60)
  41. that.setData({
  42. progressWidth: progressWidth,progressTime: progressTime
  43. })
  44. }
  45.  
  46. })
@H_301_0@

微信小程序实现时间进度条功能


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

猜你在找的JavaScript相关文章