小程序笔记(10)豆瓣引导页的实现-倒计时

小程序笔记(10)豆瓣引导页的实现-倒计时

效果图

![https://i.loli.net/2020/11/26/HlaJWEVQ3mf75rj.png](https://img-blog.csdnimg.cn/20201126161117178.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzQ5MzU1OQ==,size_16,color_FFFFFF,t_70#pic_cente

分析

引导页面的分析

  • 一个轮播图swiper用来显示广告数据

    • 这里需要用到一个定时器

          data: {
              // 要显示的公告时间
              time: 5
          }
          onLoad: function (options) {
              // 这里会返回这个事件,用于后面讲这个事件清除
              var timer = setInterval(function () {
                  // 如果当前定时大于0,就0--
                  if (that.data.time > 0) {
                      that.setData({
                          time: that.data.time - 1
                      })
                      // 如果当前定时等于0,也就是广告结束
                      // 将定时清除,并跳转到广告后的界面
                      if (that.data.time == 0) {
                          clearInterval(timer);
                          wx.switchTab({
                              url: '/pages/index/index',
                          })
                      }
                  }
                  // 每一秒执行一次
              }, 1000)
          }
            
      
  • 一个```navigator``用来跳过广告,进入一个新的页面

    <navigator open-type="switchTab" url="/pages/index/index" class="skip">跳过{{time}}</navigator>
    
  • 我们从效果图可以看到跳过按钮和广告页面是重叠的,那么这里用到了绝对布局position: absolute;

    .skip {
      position: absolute;
      right: 0rpx;
      flex-direction: column;
      width: 100rpx;
      color: #fff;
      z-index: 100;
    }
    
  • 轮播图数据我们从云开发的存储中获取,需要注意的是存储在云开发中的数据,需要设置为所有用户可读

    image-20201126160047839
  • 在加载页面的时候调用云开发函数访问数据即可

    data: {
        subjects: []
    }
    onLoad: function (options) {
        var that = this;
        const db = wx.cloud.database();
        //  coming_soon - 表名称
        db.collection('coming_soon').get({
          success(res) {
            console.log(res);
            that.setData({
              subjects: res.data[0].subjects
            })
          }
        })
      }
    
  • 将数据遍历到轮播图里面

    <swiper indicator-dots="true">
    	<swiper-item wx:for="{{subjects}}" style=" width:100%;height:100%">
    		<view class="image" style="width:100%;height:100%">
    			<image src="{{item.images.large}}" style="width:100%;height:100%"></image>
    		</view>
    	</swiper-item>
    </swiper>
    
  • 设置轮播图广告的样式

    swiper {
      position: absolute;
      top: 0rpx;
      height: 100%;
      width: 100%;
    }
    

完整代码

  • splash.wxml

    <!-- 倒计时,跳转 -->
    <navigator open-type="switchTab" url="/pages/index/index" class="skip">跳过{{time}}</navigator>
    
    <swiper indicator-dots="true">
    	<swiper-item wx:for="{{subjects}}" style=" width:100%;height:100%">
    		<view class="image" style="width:100%;height:100%">
    			<image src="{{item.images.large}}" style="width:100%;height:100%"></image>
    		</view>
    	</swiper-item>
    </swiper>
    
  • splash.wxss

    page {
      width: 100%;
      height: 100%;
    }
    
    .skip {
      position: absolute;
      right: 0rpx;
      flex-direction: column;
      width: 100rpx;
      color: #fff;
      z-index: 100;
    }
    
    swiper {
      position: absolute;
      top: 0rpx;
      height: 100%;
      width: 100%;
    }
    
  • splash.js

    // pages/splash/splash.js
    Page({
    
      /**
       * 页面的初始数据
       */
      data: {
        time: 5,
        subjects: []
      },
    
      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {
        var that = this;
        const db = wx.cloud.database();
        db.collection('coming_soon').get({
          success(res) {
            console.log(res);
            that.setData({
              subjects: res.data[0].subjects
            })
          }
        })
        
        var timer = setInterval(function () {
          if (that.data.time > 0) {
            that.setData({
              time: that.data.time - 1
            })
            if (that.data.time == 0) {
              clearInterval(timer);
              wx.switchTab({
                url: '/pages/index/index',
              })
            }
          }
        }, 1000)
      }
    })
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值