• 利用vue做一个倒计时抢购的组件


    需求就是如上图,我也是看见朋友在做这个需求,所以想着用vue也实现一下 

    其实这里主要就是用到了定时器

    用vue实现的话还是比较简单的

    以下是我实现的结果,倒计时结束,点击抢购按钮,弹出开始抢购的alert

     

     实现代码如下

    1. <template>
    2. <div>
    3. <div class="container">
    4. <div class="left">
    5. <span class="title1">杭州市通用5元券</span>
    6. <span>杭味面馆</span>
    7. </div>
    8. <button class="right" :disabled="isTrue" @click="btnCick()">
    9. {{ time }}
    10. </button>
    11. </div>
    12. </div>
    13. </template>
    14. <script>
    15. export default {
    16. data() {
    17. return {
    18. time: 10, //倒计时
    19. timer: null, //定时器
    20. isTrue: true, //控制按钮是否可用
    21. }
    22. },
    23. methods: {
    24. daoJiShi() {
    25. this.timer = setTimeout(() => {
    26. this.time = this.time - 1
    27. if (this.time == 0) {
    28. this.time = '开始抢购'
    29. }
    30. }, 1000)
    31. },
    32. btnCick() {
    33. alert('可以开始抢购啦')
    34. },
    35. },
    36. mounted() {
    37. setInterval(() => {
    38. if (this.time == '开始抢购') {
    39. this.isTrue = false
    40. clearInterval(this.timer)
    41. }
    42. this.daoJiShi()
    43. }, 1000)
    44. },
    45. }
    46. </script>
    47. <style lang="scss" scoped>
    48. .container {
    49. height: 136px;
    50. width: 430px;
    51. border-radius: 12px;
    52. background-color: #fff0f1;
    53. display: flex;
    54. justify-content: space-around;
    55. align-items: center;
    56. .left {
    57. display: flex;
    58. flex-direction: column;
    59. align-items: flex-start;
    60. .title1 {
    61. padding-bottom: 5px;
    62. font-size: 24px;
    63. font-weight: bold;
    64. }
    65. }
    66. .right {
    67. width: 108px;
    68. height: 45px;
    69. border-radius: 8px;
    70. font-size: 20px;
    71. background-color: #f00;
    72. color: white;
    73. border: none;
    74. }
    75. }
    76. </style>

  • 相关阅读:
    支持AGP8的Android路由库URouter
    【UCIe】UCIe 软件配置
    【2011】【论文笔记】用THz-TDS观察水树——
    蓝桥杯单片机综合练习——工厂灯光控制
    c++面试三 -- 智能指针--7000字
    17. Spring类型转换之PropertyEditor
    微信小程序Vue+nodejs+uniapp课堂教学辅助在线学习系统
    P6入门:项目初始化1-项目详情介绍
    Spring Boot 注解
    数据结构--队列
  • 原文地址:https://blog.csdn.net/weixin_56818823/article/details/125629990