Gallery 画廊

用于预览图片或其他操作。

使用指南

在 page.json 中引入组件

  1. {
  2. "navigationBarTitleText": "Gallery",
  3. "usingComponents": {
  4. "wux-gallery": "../../dist/gallery/index"
  5. }
  6. }

示例

!> 该组件主要依靠 JavaScript 主动调用,所以一般只需在 wxml 中添加一个组件,并设置 id 为 #wux-gallery 或其他,之后在 page.js 中调用 $wuxGallery(id) 获取匹配到的第一个组件实例对象。

  1. <wux-gallery id="wux-gallery" />
  2. <view class="page">
  3. <view class="page__hd">
  4. <view class="page__title">Gallery</view>
  5. <view class="page__desc">画廊</view>
  6. </view>
  7. <view class="page__bd">
  8. <view class="weui-cells__title">基于小程序原生的wx.previewImage</view>
  9. <view class="weui-cells weui-cells_after-title">
  10. <view class="weui-cell">
  11. <view class="weui-cell__bd">
  12. <view class="weui-uploader">
  13. <view class="weui-uploader__bd">
  14. <view class="weui-uploader__files">
  15. <block wx:for-items="{{ urls }}" wx:key="{{ index }}">
  16. <view class="weui-uploader__file" bindtap="previewImage" data-current="{{ item }}">
  17. <image class="weui-uploader__img" src="{{ item }}" />
  18. </view>
  19. </block>
  20. </view>
  21. </view>
  22. </view>
  23. </view>
  24. </view>
  25. </view>
  26. <view class="weui-cells__title">自定义gallery</view>
  27. <view class="weui-cells weui-cells_after-title">
  28. <view class="weui-cell">
  29. <view class="weui-cell__bd">
  30. <view class="weui-uploader">
  31. <view class="weui-uploader__bd">
  32. <view class="weui-uploader__files">
  33. <block wx:for-items="{{ urls }}" wx:key="{{ index }}">
  34. <view class="weui-uploader__file" bindtap="showGallery" data-current="{{ index }}">
  35. <image class="weui-uploader__img" src="{{ item }}" />
  36. </view>
  37. </block>
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. </view>
  43. </view>
  44. <view class="weui-cells__title">带说明文案</view>
  45. <view class="weui-cells weui-cells_after-title">
  46. <view class="weui-cell">
  47. <view class="weui-cell__bd">
  48. <view class="weui-uploader">
  49. <view class="weui-uploader__bd">
  50. <view class="weui-uploader__files">
  51. <block wx:for-items="{{ urls }}" wx:key="{{ index }}">
  52. <view class="weui-uploader__file" bindtap="showGallery2" data-current="{{ index }}">
  53. <image class="weui-uploader__img" src="{{ item }}" />
  54. </view>
  55. </block>
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. </view>
  61. </view>
  62. </view>
  63. </view>
  1. import { $wuxGallery } from '../../dist/index'
  2. Page({
  3. data: {
  4. urls: [
  5. 'https://unsplash.it/200/200',
  6. 'https://unsplash.it/300/300',
  7. 'https://unsplash.it/400/400',
  8. 'https://unsplash.it/600/600',
  9. 'https://unsplash.it/800/800',
  10. 'https://unsplash.it/900/900',
  11. 'https://unsplash.it/1000/1000',
  12. 'https://unsplash.it/1200/1200',
  13. ],
  14. },
  15. onLoad() {},
  16. showGallery(e) {
  17. const { current } = e.currentTarget.dataset
  18. const { urls } = this.data
  19. this.$wuxGallery = $wuxGallery()
  20. this.$wuxGallery.show({
  21. current,
  22. urls,
  23. [`delete`]: (current, urls) => {
  24. urls.splice(current, 1)
  25. this.setData({
  26. urls,
  27. })
  28. return true
  29. },
  30. cancel() {
  31. console.log('Close gallery')
  32. },
  33. onTap(current, urls) {
  34. console.log(current, urls)
  35. return true
  36. },
  37. onChange(e) {
  38. console.log(e)
  39. },
  40. })
  41. },
  42. showGallery2(e) {
  43. const { current } = e.currentTarget.dataset
  44. const { urls } = this.data
  45. $wuxGallery().show({
  46. current,
  47. urls: urls.map((n) => ({ image: n, remark: n })),
  48. showDelete: false,
  49. indicatorDots: true,
  50. indicatorColor: '#fff',
  51. indicatorActiveColor: '#04BE02',
  52. })
  53. },
  54. previewImage(e) {
  55. const { current } = e.currentTarget.dataset
  56. const { urls } = this.data
  57. wx.previewImage({
  58. current,
  59. urls,
  60. })
  61. },
  62. })

视频演示

Gallery

API

参数 类型 描述 默认值
options object 配置项 -
options.prefixCls string 自定义类名前缀 wux-gallery
options.classNames any 过渡的类名,更多内置过渡效果请参考 AnimationGroup wux-animate—slideInRight
options.indicatorDots boolean 是否显示面板指示点 false
options.indicatorColor string 指示点颜色 rgba(0, 0, 0, .3)
options.indicatorActiveColor string 当前选中的指示点颜色 #000000
options.autoplay boolean 是否自动切换 false
options.interval number 自动切换时间间隔 5000
options.duration number 滑动动画时长 500
options.circular boolean 是否采用衔接滑动 false
options.vertical boolean 滑动方向是否为纵向 false
options.showDelete boolean 是否显示删除按钮 true
options.allowScale boolean 是否支持手势缩放 true
options.current number 当前显示图片的索引值 0
options.urls array 需要预览的图片链接列表 []
options.delete function 点击删除的回调函数,返回值为 true 时将会关闭组件 -
options.cancel function 点击关闭的回调函数 -
options.onTap function 图片点击事件,返回值为 true 时将会关闭组件 -
  • Gallery.show
  • Gallery.hide
  • Gallery.slideTo
  • Gallery.slideNext
  • Gallery.slidePrev