目录

cover-image

Represents an image that covers system components.

It can be used on the map, video, canvas, and camera components.

Important notes:

The cover-image component is designed for compatibility with earlier versions of mini programs. For new projects, use the image component instead.

Since version 1.0.22, the nested content within the map, video, canvas, and camera components is displayed by default. In earlier versions, the nested content within the tags is not displayed by default.

Since: 1.0.22

Constraints:

The default width and height of the cover-image component are 320 px and 240 px, respectively.

Attributes

Name Type Default Value Mandatory Description
src string - Yes Image resource address. Online and local resources in the following image formats are supported: PNG, JPG, JPEG, GIF, SVG, BMP, and WebP.
mode string scaleToFill No Cropping and scaling modes for images. For details, see "Valid mode values" below.
binderror eventhandle - No Event triggered when an error occurs. The format is event.detail = {errMsg}.
bindload eventhandle - No Event triggered when the image is fully loaded. The format is event.detail = {height, width}.

Valid mode values

Value Description
scaleToFill Scaling mode that stretches the image to completely fill the image element, without preserving the aspect ratio.
aspectFit Scaling mode that maintains the aspect ratio and ensures the entire image is visible That is, the entire image can be displayed.
aspectFill Scaling mode that maintains the aspect ratio and ensures the shorter side of the image is fully visible That is, a non-square image is displayed completely either horizontally or vertically, with potential cropping in the other direction.
widthFix Scaling mode that keeps the width unchanged and adjusts the height to maintain the original aspect ratio.
heightFix Scaling mode that keeps the height unchanged and adjusts the width to maintain the original aspect ratio.
top Cropping mode that displays only the top part of the image without scaling.
bottom Cropping mode that displays only the bottom part of the image without scaling.
center Cropping mode that displays only the central part of the image without scaling.
left Cropping mode that displays only the left part of the image without scaling.
right Cropping mode that displays only the right part of the image without scaling.
top left Cropping mode that displays only the top left part of the image without scaling.
top right Cropping mode that displays only the top right part of the image without scaling.
bottom left Cropping mode that displays only the bottom left part of the image without scaling.
bottom right Cropping mode that displays only the bottom right part of the image without scaling.

Example:

HXML file:

<map
  id="myMap"
  class="my-map-container"
  latitude="{{ latitude }}"
  longitude="{{ longitude }}"
  scale="16"
  show-location>
  <scroll-view class="comp-container" scroll-y="true">
    <cover-view class="info-card" has:for="{{ modeArray }}" has:for-item="item">
      <cover-view class="card-title">
          {{ item.text }}
      </cover-view>
      <cover-image class="card-image" src="{{ src }}" mode="{{ item.mode }}" bindload="imageLoad" binderror="imageLoadError"></cover-image>
      <cover-view class="card-btn" bindtap="onBtnClick" data-mode="{{ item.mode }}">
          Tap the button.
      </cover-view>
    </cover-view>
  </scroll-view>
</map>

JavaScript file:

Page({
  data: {
    latitude: 39.908823,
    longitude: 116.397470,

    modeArray: [
      { mode: 'scaleToFill', text: 'scaleToFill: stretches the image to completely fill the image element, without preserving the aspect ratio.' },
      { mode: 'aspectFit', text: 'aspectFit: maintains the aspect ratio and ensures the longer side of the image is fully visible.' },
      { mode: 'aspectFill', text: 'aspectFill: maintains the aspect ratio and ensures the shorter edge of the image is fully visible.' },
      { mode: 'widthFix', text: 'widthFix: keeps the width unchanged and adjusts the height to maintain the original aspect ratio.' },
      { mode: 'top', text: 'top: displays only the top part of the image without scaling.' },
      { mode: 'bottom', text: 'bottom: displays only the bottom part of the image without scaling.' },
      { mode: 'center', text: 'center: displays only the center part of the image without scaling.' },
      { mode: 'left', text: 'left: displays only the left part of the image without scaling.' },
      { mode: 'right', text: 'right: displays only the right part of the image without scaling.' },
      { mode: 'top left', text: 'top left: displays only the top left part of the image without scaling.' },
      { mode: 'top right', text: 'top right: displays only the top right part of the image without scaling.' },
      { mode: 'bottom left', text: 'bottom left: displays only the bottom left part of the image without scaling.' },
      { mode: 'bottom right', text: 'bottom right: displays only the bottom right part of the image without scaling.' },
    ],

    src: 'https://www.example.com/example.jpg', // Replace the example with the actual image resource address.
  },

  onBtnClick(e) {
    has.showToast({
      title: `Current mode: ${e.currentTarget.dataset.mode}`,
      icon: 'success'
    });
  },

  imageLoadError(e) {
    console.error ('Loading failed', e.detail);
  },

  imageLoad(e) {
    console.info('Loading succeeded', e.detail);
  }
});

CSS file:

.my-map-container {
  width: 100%;
  height: 100vh;
}

.comp-container {
  position: relative;
  width: 100vw;
  height: 40vh;
}

.info-card {
  background: rgba(255,255,255,0.5);
  border-radius: 16rpx;
  padding: 30rpx;
  box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.15);
  margin-bottom: 20rpx;
}

.card-title {
  font-size: 30rpx;
  font-weight: 500;
  color: #333;
  margin-bottom: 20rpx;
}

.card-image {
  width: 100%;
  height: 320rpx;
  border-radius: 12rpx;
  object-fit: cover;
  margin-bottom: 24rpx;
  border: 3rpx solid #0A59F7;
  box-sizing: border-box;
}

.card-btn {
  width: 100%;
  height: 80rpx;
  line-height: 80rpx;
  background-color: #0A59F7;
  color: #fff;
  text-align: center;
  border-radius: 500rpx;
  font-size: 32rpx;
}