酷听说(英语学习)
60.1MB · 2025-10-27
在很多移动端应用中,广告弹窗是常见的变现手段。但频繁弹出、无法关闭、重复打扰的广告往往会适得其反,导致用户流失。
本文将带你用 uni-app 实现一个 “智能不扰民” 的广告弹窗组件,支持24小时只弹一次、点击跳转、优雅关闭,可直接复制使用。
@click="closeAd",内部容器加 @click.stop 阻止冒泡。uni.setStorageSync('lastAdTime', Date.now()) 记录本次弹出时间,下次 onLoad 时对比 24 h 间隔,不到时间绝不骚扰。600x800rpx,体积 < 100KBuni.request 动态获取广告图和跳转链接uni.setStorageSync('neverShowAd', true)<template>
<view class="content">
<!-- 其他元素 -->
<!-- 广告弹窗 -->
<view v-if="visible" class="ad-mask" @click="closeAd">
<view class="ad-container" @click.stop>
<image class="ad-image" :src="adImage" mode="aspectFit" @click="onAdClick" />
<view class="ad-close-btn" @click="closeAd"></view>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
visible: false,
adImage: '/static/ad.png',
adLink: '/pages/activity/index'
}
},
onLoad() {
const last = uni.getStorageSync('lastAdTime');
const now = Date.now();
const oneDay = 24 * 60 * 60 * 1000;
if (!last || now - last > oneDay) {
this.showAd()
}
},
methods: {
showAd() {
this.visible = true;
},
closeAd() {
this.visible = false;
uni.setStorageSync('lastAdTime', Date.now());
},
onAdClick() {
uni.navigateTo({ url: adLink });
this.closeAd();
}
}
}
</script>
<style>
.ad-mask {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.6);
display: flex;
justify-content: center;
align-items: center;
z-index: 9999;
}
.ad-container {
position: relative;
width: 600rpx;
height: 800rpx;
}
.ad-image {
width: 100%;
height: 100%;
border-radius: 16rpx;
}
.ad-close-btn {
position: absolute;
top: 16rpx;
right: 16rpx;
color: #fff;
font-size: 36rpx;
background: rgba(0, 0, 0, 0.5);
border-radius: 50%;
width: 60rpx;
height: 60rpx;
line-height: 60rpx;
text-align: center;
}
</style>