本文实例为大家分享了微信小程序实现首页弹出广告的具体代码,供大家参考,具体内容如下
这个微信小程序首页广告demo
仅供有需要的参考
.wxml
1
2
3
4
5
6
7
8
9
10
11
12
|
<!-- 广告展示 --> < view class = "AdView" hidden = "{{showAd}}" catchtouchmove = "preventMove" > < view class = "bg" ></ view > < view class = "active active-sport" > < view class = "cancel" bind:tap = "cancelTap" ></ view > < view class = "active-content" bind:tap = "activeContent" data-id = '2340173092' > < text >this is ad</ text > < image class = "active-img" src = 'cloud://international-3bp20.696e-international-3bp20-1300609829/my-image.jpeg' mode = "aspectFit" ></ image > </ view > </ view > </ view > |
//事件 catchtouchmove方法主要作用是固定广告防止点击穿透,就是使用弹出广告的后面内容不能上下拉动。其中对应的.js方法可以不做任何处理。此处只能在真机上看到实际效果,在电脑上不能。并且此方法要放到广告view最外层
.wxss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
.bg{ width : 100% ; height : 100% ; position : fixed ; top : 0 ; left : 0 ; z-index : 990 ; background-color : rgb ( 180 , 180 , 180 ); opacity: 0.5 ; } .active{ width : 80% ; height : 80% ; background-color : #fff ; position : absolute ; top : 10% ; left : 50% ; transform: translate( -50% ); z-index : 992 ; } .active-sport{ animation: sport 1.5 s linear 1 ; } @keyframes sport{ from{ transform:rotate( 0 deg) skew( -10 deg) scale( 2.0 ) translate( -100% , 0 )} to{ transform:rotate( 360 deg) skew( 0 deg) scale( 1.0 ) translate( -50% , 0 )} } .active-content{ width : 80% ; height : 80% ; background-color : blue ; position : absolute ; top : 50% ; left : 50% ; transform: translate( -50% , -50% ); text-align : center ; } .active-img{ width : 100% ; } .cancel{ background : url ( 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/PjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCB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y41NDg2NzgtNDUuOTkyNDk3IDAtNjMuNTI1ODI1bDY3Ny42NzEyMjctNjc3LjY4NTU1M2MxNy41NDg2NzgtMTcuNTQ4Njc4IDQ1Ljk3NzE0Ny0xNy41NDg2NzggNjMuNTI1ODI1IDAgMTcuNTQ4Njc4IDE3LjUzNDM1MiAxNy41NDg2NzggNDUuOTkyNDk3IDAgNjMuNTI1ODI1TDIwNC42Mjk1MTcgODgyLjM1NDk3OUMxOTUuODU1NjkgODkxLjEyODgwNyAxODQuMzYwODkxIDg5NS41MTY3NDQgMTcyLjg2NzExNiA4OTUuNTE2NzQ0eiIgcC1pZD0iMjEyMSIgZmlsbD0iI2NkY2RjZCI+PC9wYXRoPjwvc3ZnPg==' ) no-repeat 10 rpx 10 rpx/ 40 rpx 40 rpx; width : 60 rpx; height : 60 rpx; position : absolute ; top : 5 rpx; right : 5 rpx; z-index : 998 ; } |
此处cancel的背景图片只能使用base64或本地。
image 只能是网络或者fieldId上
可以作用CSS3特性,制作动画,制作动画时要注意加上transform之后,才可以增加rotate旋转,skew翻转,scale缩放,translate移动。
.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
// miniprogram/pages/demo/demo.js Page({ /** * 页面的初始数据 */ data: { playlist:[], showAd: false }, //cancelTap隐藏方向 cancelTap(){ this .setData({showAd:! this .data.showAd}); }, //广告链接 activeContent(event){ console.log(event.currentTarget.dataset.id); // this.setData({showAd:!this.data.showAd}); let musicId=event.currentTarget.dataset.id; wx.navigateTo({ url:`../musicList/musicList?playlistId=${musicId}`, success(){ console.log( 'navigate success' ) }, fail(err){ console.log(err) } }); }, //阻止滚动穿透 preventMove(e){ console.log(e); }, /** * 生命周期函数--监听页面加载 */ async onLoad(options) { this ._getPlaylist(); }, async _getPlaylist(){ wx.showLoading({ title: '加载中' , }) let listCount = await wx.cloud.callFunction({ name: 'music' , data: { $url: 'getPlaylist' , start: this .data.playlist.length, count: 15 } }) .then(res => { console.log(res); res.result.map(value=>{ this .setData({ playlist: this .data.playlist.concat(value) }) }) wx.hideLoading(); return res.result.length; }) . catch (err=>{console.error;wx.hideLoading();wx.showToast({ title: '加载失败,稍后再试!' , icon: 'none' , duration:1500 })}); return listCount; }, //tcbRouter async tcbmusicTap(){ let result = await wx.cloud.callFunction({ name: 'tcbRouter' , data:{ $url: 'music' } }) .then(res=>console.log(res)) . catch (err=>console.error); }, async tcbmovieTap(){ let result = await wx.cloud.callFunction({ name: 'tcbRouter' , data:{ $url: 'movie' } }) .then(res=>console.log(res)) . catch (err=>console.error); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { // this.setData({showAd:false}); }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ async onPullDownRefresh() { this .setData({ playlist:[]}); await this ._getPlaylist(); wx.stopPullDownRefresh(); }, /** * 页面上拉触底事件的处理函数 */ async onReachBottom() { let flag = await this ._getPlaylist(); if (!flag){ wx.showToast({ title: '我是一个有底的人!' , duration:2000, image: '../../images/girl3.png' }) } }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } }) |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/hjiaqing/article/details/104107024