先看效果圖
image.png
實(shí)現(xiàn)思路:
1.寫一個(gè)定時(shí)器,綁定一個(gè)數(shù)據(jù)拼岳,這里使用的是miao枝誊,初始值為6。
2.在進(jìn)入頁面時(shí)開始執(zhí)行定時(shí)器惜纸,每秒執(zhí)行一次miao-1操作叶撒。
this.time = setInterval(function () {
that.setData({
miao: that.data.miao-1
})
3.當(dāng)miao==0時(shí)绝骚,清除定時(shí)器clearInterval(this.time)(一定要清除定時(shí)器),然后自動跳轉(zhuǎn)到首頁祠够。
if (that.data.miao == 0){
clearInterval(this.time);
wx.switchTab({ //保留當(dāng)前頁面压汪,跳轉(zhuǎn)到應(yīng)用內(nèi)的某個(gè)頁面
url: "/pages/index/index"
})
}
4.也可以點(diǎn)擊跳過廣告,當(dāng)用戶點(diǎn)擊跳過廣告時(shí)古瓤,清除定時(shí)器(一定要清除定時(shí)器)止剖,然后頁面跳轉(zhuǎn)
cliadv: function(){
clearInterval(this.time)
wx.switchTab({ //保留當(dāng)前頁面,跳轉(zhuǎn)到應(yīng)用內(nèi)的某個(gè)頁面(最多打開5個(gè)頁面落君,之后按鈕就沒有響應(yīng)的)
url: "/pages/index/index"
})
}
整體代碼
WXML部分
<view class='main' wx:if="{{ismian}}">
<view class='adv1'>
<image src='/images/1.jpg' class='adv-img'></image>
<text class='tiaoguo' bindtap='cliadv'>跳過廣告 {{miao}}</text>
</view>
<view>
<button>程序員可可</button>
<text class='text2'>程序員可可</text>
</view>
</view>
WXSS部分
/* pages/advertising/advertising.wxss */
.adv1{
width: 100%;
height: 900rpx;
background: url('/img/ba3.png') no-repaeat 0 0;
background-size: contain;
}
.adv-img{
width: 100%;
height: 900rpx;
position: absolute;
}
.tiaoguo{
font-size: 25rpx;
background-color: wheat;
border-radius: 80rpx;
display: inline-block;
margin-left: 10rpx;
position: absolute;
z-index: 999;
right: 25rpx;
top: 850rpx;
padding-left: 10rpx;
padding-right: 10rpx;
}
button{
border-radius: 18rpx;
width: 220rpx;
background-color: #EECBAD;
color: #8B5742;
margin-top: 38rpx;
font-size: 33rpx;
}
.text2{
width: 100%;
height: 100%;
display: flex;
justify-content: center;
margin-top: 23rpx;
font-size: 28rpx;
}
JS代碼
// pages/advertising/advertising.js
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
miao: 6,
time:'',
ismian:true //圖片開始默認(rèn)顯示
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
var that = this;
this.time = setInterval(function () {
that.setData({
miao: that.data.miao-1
})
if (that.data.miao == 0){
clearInterval(this.time);
wx.switchTab({ //保留當(dāng)前頁面穿香,跳轉(zhuǎn)到應(yīng)用內(nèi)的某個(gè)頁面(最多打開5個(gè)頁面,之后按鈕就沒有響應(yīng)的)
url: "/pages/index/index"
})
//秒數(shù)等于0就隱藏整個(gè)頁面
that.setData({
ismian:false
})
}
}, 1000)
},
cliadv: function(){
clearInterval(this.time)
wx.switchTab({ //保留當(dāng)前頁面叽奥,跳轉(zhuǎn)到應(yīng)用內(nèi)的某個(gè)頁面(最多打開5個(gè)頁面扔水,之后按鈕就沒有響應(yīng)的)
url: "/pages/index/index"
})
隱藏整個(gè)頁面
this.setData({
ismian:false
})
}
})