news 2026/4/22 21:44:21

4399小程序banner广告和插屏广告

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
4399小程序banner广告和插屏广告

banner广告

// 获取真机设备像素比 const pixelRatio = gamebox.getSystemInfoSync().pixelRatio; // 定义 Banner 广告的宽高和位置 const width = 320 * pixelRatio; const height = 50 * pixelRatio; const bannerLeft = (gamebox.getSystemInfoSync().screenWidth * pixelRatio - width) / 2; const bannerTop = gamebox.getSystemInfoSync().screenHeight * pixelRatio - height; let currentBannerAd = null; function createBannerAd() { return gamebox.createBannerAd({ style: { width: width, height: height, left: bannerLeft, top: bannerTop } }); } function showBannerAd() { if (currentBannerAd) { currentBannerAd.destroy(); // 销毁之前的 Banner 实例 } currentBannerAd = createBannerAd(); // 监听 Banner 广告加载事件回调函数 const bannerOnLoadCb = (res) => { console.info('Banner onLoad', res); }; // 监听 Banner 广告错误事件回调函数 const bannerOnErrorCb = (res) => { console.info('Banner onError', res); }; // 监听 Banner 广告加载事件 currentBannerAd.onLoad(bannerOnLoadCb); // 取消监听 Banner 广告加载事件(监听取消的函数,应与监听回调函数为同一个) currentBannerAd.offLoad(bannerOnLoadCb); // 监听 Banner 广告错误事件 currentBannerAd.onError(bannerOnErrorCb); // 取消监听 Banner 广告错误事件(监听取消的函数,应与监听回调函数为同一个) currentBannerAd.offError(bannerOnErrorCb); // 显示 Banner 广告 currentBannerAd.show().catch(err => { console.info('Banner 广告显示失败', err); }); } // 初始展示 Banner 广告 showBannerAd(); // 设置定时器,每2分钟重新展示 Banner 广告 setInterval(showBannerAd, 2 * 60 * 1000);

插屏广告

// 创建插屏广告实例 const interstitialAd = gamebox.createInterstitialAd(); // 监听 InterstitialAd 广告加载事件回调函数 const onLoadCb = (res) => { console.info('InterstitialAd onLoad ' + res); } // 监听 InterstitialAd 广告关闭事件回调函数 const onCloseCb = (res) => { console.info('InterstitialAd onClose ' + res); } // 监听 InterstitialAd 广告错误事件回调函数 const onErrorCb = (res) => { console.info('InterstitialAd onError ' + res); } // 监听 InterstitialAd 广告加载事件 interstitialAd.onLoad(onLoadCb); // 取消监听 InterstitialAd 广告加载事件(监听取消的函数,应与监听回调函数为同一个) interstitialAd.offLoad(onLoadCb); // 监听 InterstitialAd 广告关闭 interstitialAd.onClose(onCloseCb); // 取消监听 InterstitialAd 广告关闭(监听取消的函数,应与监听回调函数为同一个) interstitialAd.offClose(onCloseCb); // 监听 InterstitialAd 广告错误 interstitialAd.onError(onErrorCb); // 取消监听 InterstitialAd 广告错误(监听取消的函数,应与监听回调函数为同一个) interstitialAd.offError(onErrorCb); // 定义一个函数用于显示广告 function showInterstitialAd() { interstitialAd.show().catch(() => { // 失败重试 interstitialAd.load() .then(() => interstitialAd.show()) .catch(err => { console.log('InterstitialAd 广告显示失败', err); }); }); } // 设置一个定时器,每6分钟显示一次广告 setInterval(showInterstitialAd, 6 * 60 * 1000); // 6分钟转换为毫秒 // 销毁 InterstitialAd 广告 // interstitialAd.destroy(); // 如果需要在某些情况下销毁广告可以调用此方法
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 8:15:35

Wan2.2-T2V-A14B在龙卷风形成机制科普中的空气涡旋建模

Wan2.2-T2V-A14B在龙卷风形成机制科普中的空气涡旋建模 在气象教育和科学传播领域,如何让公众“看见”那些肉眼无法捕捉、却又真实存在的自然现象?比如龙卷风——它不是凭空出现的怪物,而是大气中一系列精密物理过程演化的结果。然而&#xf…

作者头像 李华
网站建设 2026/4/23 11:11:38

基于Springboot线上装修管理系统【附源码+文档】

💕💕作者: 米罗学长 💕💕个人简介:混迹java圈十余年,精通Java、小程序、数据库等。 💕💕各类成品Java毕设 。javaweb,ssm,springboot等项目&#…

作者头像 李华
网站建设 2026/4/23 12:34:28

搞单片机的简单吗?

我们和一家公司合作,对方派来个写代码的程序员。他敲代码的本事还不错,但沟通起来总是有点费劲。这种沟通问题倒不是说他代码写得不行,而是一起干活的时候,理解和执行任务的效率不太够。举个例子,我让他写文档&#xf…

作者头像 李华
网站建设 2026/4/23 9:52:25

量子计算时代来临,MCP 2025新增考点你准备好了吗?

第一章:量子计算时代来临,MCP 2025认证新纪元随着量子计算技术从理论走向工程实现,全球IT认证体系迎来重大变革。微软于2025年推出的全新MCP(Microsoft Certified Professional)认证架构,首次将量子算法设计…

作者头像 李华
网站建设 2026/4/23 9:52:09

静态住宅IP实战测评:如何高效获取全球最新资讯?

一、引言:全球化视野下的信息壁垒与应对策略 在全球经济一体化纵深发展的当下,跨国运营、市场研究与国际关系分析都极度依赖对海外一手资讯的精准把握。然而,一个无形的数字鸿沟已然形成:企业、研究机构与个人在获取全球信息时&a…

作者头像 李华