Banner Ad

View Markdown

Last edited on 2026-07-23

Mini Program Ad traffic owner operation guide: Document link Developers can use the ad component to create a Banner ad component. After creation, the Banner ad component automatically fetches ad data and displays it.

Ad Size Configuration

Banner ads do not allow direct style attribute settings. The default width is 100% (width: 100%), and the height is automatically calculated proportionally. Therefore, developers can adjust the ad size by setting the width of the outer ad container component.

The width of the outer ad container component must not be less than 300px. If the width is less than 300px, the width of the Banner ad will be forcibly adjusted to 300px.

/* The width of the outer component can be set to 100% or a specific value */
.adContainer {
  width: 100%;
}
<view class="adContainer">
  <ad unit-id="xxxx"></ad>
</view>

Ad Event Listening

After a Banner ad is created, it automatically fetches the ad. Developers can listen for successful or failed ad fetches using the onload and onerror events of the ad component, and listen for the ad being closed using the onclose event.

<view class="adContainer">
  <ad unit-id="xxxx" bindload="adLoad" binderror="adError" bindclose="adClose"></ad>
</view>
Page({
  adLoad() {
    console.log('Banner ad loaded successfully')
  },
  adError(err) {
    console.log('Banner ad failed to load', err)
  },
  adClose() {
    console.log('Banner ad closed')
  }
})

Scheduled Ad Refresh

Developers can achieve scheduled ad refresh by passing the ad-intervals parameter when creating a Banner ad. The ad-intervals parameter is of numeric type, with the unit in seconds. Note: The automatic refresh interval cannot be less than 30 seconds; therefore, the value of ad-intervals must be greater than or equal to 30.

<view class="adContainer">
  <ad unit-id="xxxx" ad-intervals="30"></ad>
</view>