Skip to content

視圖容器

scroll-view

  • 功能說明: 可滾動視圖區域。 使用豎向滾動時,需要給<scroll-view>一個固定高度,通過WXSS設定height。

  • 參數及說明:

内容類型預設值說明
scroll-xbooleanfalse允許橫向滾動
scroll-ybooleanfalse允許縱向滾動
upper-thresholdnumber/string50距頂部/左邊多遠時,觸發scrolltoupper
lower-thresholdnumber/string50距底部/右邊多遠時,觸發scrolltolower事件
scroll-topnumber/string-設定豎向滾動條位置
scroll-leftnumber/string-設定橫向滾動條位置
scroll-into-viewstring-值應為某子元素id(id不能以數位開頭)。 設定哪個方向可滾動,則在哪個方向滾動到該元素
scroll-with-animationbooleanfalse在設定滾動條位置時使用動畫過渡
enable-back-to-topbooleanfalseiOS按一下頂部狀態列、Android按兩下標題列時,滾動條返回頂部,只支持豎向
refresher-enabledbooleanfalse開啟自定義下拉刷新
refresher-thresholdnumber45設定自定義下拉刷新閾值
refresher-default-stylestring"black"white
refresher-backgroundstring-設定自定義下拉刷新區域背景顏色,默認為透明
refresher-triggeredbooleanfalse設定當前下拉刷新狀態,true表示下拉刷新已經被觸發,false表示下拉刷新未被觸發
bouncesbooleantrueiOS下scroll-view邊界彈性控制(同時開啟enhanced内容後生效)
show-scrollbarbooleantrue滾動條顯隱控制(同時開啟enhanced内容後生效)
fast-decelerationbooleanfalse滑動减速速率控制,僅在iOS下生效(同時開啟enhanced内容後生效)
binddragstarteventhandle-滑動開始事件 (同時開啟 enhanced 屬性後生效) detail { scrollTop, scrollLeft }
binddraggingeventhandle-滑動事件 (同時開啟 enhanced 屬性後生效) detail { scrollTop, scrollLeft }
binddragendeventhandle-滑動結束事件 (同時開啟 enhanced 屬性後生效) detail { scrollTop, scrollLeft, velocity }
bindscrolltouppereventhandle-滾動到頂部/左邊時觸發
bindscrolltolowereventhandle-滾動到底部/右邊時觸發
bindscrolleventhandle-滾動時觸發,event.detail = {scrollLeft, scrollTop, scrollHeight, scrollWidth, deltaX, deltaY}
scroll-anchoringbooleanfalse開啟scroll anchoring特性,即控制滾動位置不隨內容變化而抖動,僅在iOS下生效,安卓下可參攷CSSoverflow-anchor内容
enhancedbooleanfalse啟用scroll-view增强特性,啟用後可通過 Cwx.ScrollViewContext 操作scroll-view
paging-enabledbooleanfalse分頁滑動效果(同時開啟enhanced内容後生效)

TIP

  • 滾動條的長度是預估的,若直接子節點的高度差別較大,則滾動條長度可能會不準確。
  • 請勿在scroll-view中使用textarea、map、canvas、video組件。
  • scroll-into-view的優先順序高於scroll-top。
  • 在滾動scroll-view時會封锁頁面回彈,所以在scroll-view中滾動,是無法觸發onPullDownRefresh。
  • 若要使用下拉刷新,請使用頁面的滾動,而不是scroll-view,這樣也能通過點擊頂部狀態列回到頁面頂部。
  • 示例代碼:

wxml:

js
<view class="container">
  <view class="page-body">
    <view class="page-section">
      <view class="page-section-title">
        <text>Vertical Scroll</text>
      </view>
      <view class="page-section-spacing">
        <scroll-view
          scroll-y="true"
          style="height: 300rpx;"
          bindscrolltoupper="upper"
          bindscrolltolower="lower"
          bindscroll="scroll"
          scroll-into-view="{{toView}}"
          scroll-top="{{scrollTop}}"
        >
          <view id="demo1" class="scroll-view-item demo-text-1"></view>
          <view id="demo2" class="scroll-view-item demo-text-2"></view>
          <view id="demo3" class="scroll-view-item demo-text-3"></view>
        </scroll-view>
      </view>
    </view>
    <view class="page-section">
      <view class="page-section-title">
        <text>Horizontal Scroll</text>
      </view>
      <view class="page-section-spacing">
        <scroll-view
          class="scroll-view_H"
          scroll-x="true"
          bindscroll="scroll"
          style="width: 100%"
        >
          <view id="demo1" class="scroll-view-item_H demo-text-1"></view>
          <view id="demo2" class="scroll-view-item_H demo-text-2"></view>
          <view id="demo3" class="scroll-view-item_H demo-text-3"></view>
        </scroll-view>
      </view>
    </view>
  </view>
</view>

javascript:

js
const order = ["demo1", "demo2", "demo3"];

Page({
  onShareAppMessage() {
    return {
      title: "scroll-view",
      path: "page/component/pages/scroll-view/scroll-view",
    };
  },

  data: {
    toView: "green",
  },

  upper(e) {
    console.log(e);
  },

  lower(e) {
    console.log(e);
  },

  scroll(e) {
    console.log(e);
  },

  scrollToTop() {
    this.setAction({
      scrollTop: 0,
    });
  },

  tap() {
    for (let i = 0; i < order.length; ++i) {
      if (order[i] === this.data.toView) {
        this.setData({
          toView: order[i + 1],
          scrollTop: (i + 1) * 200,
        });
        break;
      }
    }
  },

  tapMove() {
    this.setData({
      scrollTop: this.data.scrollTop + 10,
    });
  },
});

wxss:

js
.page-section-spacing{
  margin-top: 60rpx;
}
.scroll-view_H{
  white-space: nowrap;
}
.scroll-view-item{
  height: 300rpx;
}
.scroll-view-item_H{
  display: inline-block;
  width: 100%;
  height: 300rpx;
}

swiper

  • 功能說明: 滑塊視圖容器,change事件返回detail中包含一個source欄位,表示導致變更的原因,可能值如下:

    • autoplay自動播放導致swiper變化

    • touch用戶滑動引起swiper變化

    • 其他原因將用空字串表示。

  • 參數及說明:

内容類型預設值說明
indicator-dotsbooleanfalse是否顯示面板訓示點
indicator-colorcolorrgba(0, 0, 0, .3)訓示點顏色
indicator-active-colorcolor#000000當前選中的訓示點顏色
autoplaybooleanfalse是否自動切換
currentnumber0當前所在滑塊的index
current-item-idString""當前所在滑塊的item-id,不能與current被同時指定
intervalnumber5000自動切換時間間隔
durationnumber500滑動動畫時長
circularbooleanfalse是否採用銜接滑動
verticalbooleanfalse滑動方向是否為縱向
display-multiple-itemsnumber1同時顯示的滑塊數量
previous-marginstring"0px"前邊距,可用於露出前一項的一小部分,接受px和rpx值
easing-functionstring"default"指定swiper切換緩動動畫類型,合法值如下:
default:默認緩動函數
linear:線性動畫
easeInCubic:緩入動畫
easeOutCubic緩出動畫
easeInOutCubic緩入緩出動畫
next-marginstring"0px"後邊距,可用於露出後一項的一小部分,接受px和rpx值
bindchangeeventhandle-current 改變時會觸發 change 事件,event.detail = {current: current, source: source}
bindtransitioneventhandle-swiper-item 的位置改變時會觸發 transition 事件,event.detail = {dx: dx, dy: dy}
bindanimationfinisheventhandle-動畫結束時會觸發animationfinish事件,event.detail同上
skip-hidden-item-layoutbooleanfalse是否跳過未顯示的滑塊佈局,設為true可優化複雜情况下的滑動效能,但會遺失隱藏狀態滑塊的佈局資訊

TIP

  • 其中只可放置 swiper-item 組件,否則會導致未定義的行為。
  • 如果在bindchange的事件回呼函數中使用setData改變current值,則有可能導致setData被不停地調用,因而通常情况下請在改變,current值前檢測source欄位來判斷是否是由於用戶觸摸引起。

示例代碼:

wxml:

js
<view class="container">
  <view class="page-body">
    <view class="page-section page-section-spacing swiper">
      <swiper
        indicator-dots="{{indicatorDots}}"
        autoplay="{{autoplay}}"
        interval="{{interval}}"
        duration="{{duration}}"
      >
        <block wx:for="{{background}}" wx:key="*this">
          <swiper-item>
            <view class="swiper-item {{item}}"></view>
          </swiper-item>
        </block>
      </swiper>
    </view>
    <view class="page-section" style="margin-top: 40rpx;margin-bottom: 0;">
      <view class="weui-cells weui-cells_after-title">
        <view class="weui-cell weui-cell_switch">
          <view class="weui-cell__bd">Indicator</view>
          <view class="weui-cell__ft">
            <switch
              checked="{{indicatorDots}}"
              bindchange="changeIndicatorDots"
            />
          </view>
        </view>
        <view class="weui-cell weui-cell_switch">
          <view class="weui-cell__bd">Auto play</view>
          <view class="weui-cell__ft">
            <switch checked="{{autoplay}}" bindchange="changeAutoplay" />
          </view>
        </view>
      </view>
    </view>

    <view class="page-section page-section-spacing">
      <view class="page-section-title">
        <text>Slide transition duration(ms)</text>
        <text class="info">{{ duration }}</text>
      </view>
      <slider
        bindchange="durationChange"
        value="{{duration}}"
        min="500"
        max="2000"
      />
      <view class="page-section-title">
        <text>Auto play Interval duration(ms)</text>
        <text class="info">{{ interval }}</text>
      </view>
      <slider
        bindchange="intervalChange"
        value="{{interval}}"
        min="2000"
        max="10000"
      />
    </view>
  </view>
</view>

javascript:

js
Page({
  onShareAppMessage() {
    return {
      title: "swiper",
      path: "page/component/pages/swiper/swiper",
    };
  },

  data: {
    background: ["demo-text-1", "demo-text-2", "demo-text-3"],
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
  },

  changeIndicatorDots() {
    this.setData({
      indicatorDots: !this.data.indicatorDots,
    });
  },

  changeAutoplay() {
    this.setData({
      autoplay: !this.data.autoplay,
    });
  },

  intervalChange(e) {
    this.setData({
      interval: e.detail.value,
    });
  },

  durationChange(e) {
    this.setData({
      duration: e.detail.value,
    });
  },
});

swiper-item

  • 功能說明: 僅可放置在swiper組件中,寬高自動設定為100%

  • 參數及說明:

内容類型預設值說明
item-idstring""該swiper-item的識別字

view

  • 功能說明: 視圖容器。

  • 參數及說明:

内容類型預設值說明
hover-classstring-指定按下去的樣式類,當hover-class='none'時,沒有點擊態效果
hover-stop-propagationbooleanfalse指定是否封锁本節點的祖先節點出現點擊態
hover-start-timenumber50按住後多久出現點擊態,單位毫秒
hover-stay-timenumber400手指鬆開後點擊態保留時間,單位毫秒

TIP

如果需要使用滾動視圖,請使用 scroll-view

示例代碼:

wxml:

js
<view class="container">
  <view class="page-body">
    <view class="page-section">
      <view class="page-section-title">
        <text>flex-direction: row\nHorizontal layout</text>
      </view>
      <view class="page-section-spacing">
        <view class="flex-wrp" style="flex-direction:row;">
          <view class="flex-item demo-text-1"></view>
          <view class="flex-item demo-text-2"></view>
          <view class="flex-item demo-text-3"></view>
        </view>
      </view>
    </view>
    <view class="page-section">
      <view class="page-section-title">
        <text>flex-direction: column\nVertical layout</text>
      </view>
      <view class="flex-wrp" style="flex-direction:column;">
        <view class="flex-item flex-item-V demo-text-1"></view>
        <view class="flex-item flex-item-V demo-text-2"></view>
        <view class="flex-item flex-item-V demo-text-3"></view>
      </view>
    </view>
  </view>
</view>

movable-area

  • 功能說明: movable-view的可移動區域

  • 參數及說明:

内容類型預設值說明
scale-areabooleanfalse當裡面的movable-view設定為支持雙指縮放時,設定此值可將縮放手勢生效區域修改為整個movable-area

TIP

  • movable-area必須設定width和height内容,不設定默認為10px。
  • 當movable-view小於movable-area時,movable-view的移動範圍是在movable-area內。
  • 當movable-view大於movable-area時,movable-view的移動範圍必須包含movable-area(x軸方向和y軸方向分開考慮)。

wxml:

js
<view class="container">
  <view class="page-body">
    <view class="page-section">
      <view class="page-section-title first">
        movable-view  area smaller movable-area
      </view>
      <movable-area>
        <movable-view x="{{x}}" y="{{y}}" direction="all">
          text
        </movable-view>
      </movable-area>
    </view>
    <view class="btn-area">
      <button bindtap="tap" class="page-body-button" type="primary">
        click to move to (30px, 30px)
      </button>
    </view>

    <view class="page-section">
      <view class="page-section-title">movable-view region larger than movable-area</view>
      <movable-area>
        <movable-view class="max" direction="all">
          text
        </movable-view>
      </movable-area>
    </view>

    <view class="page-section">
      <view class="page-section-title">only horizontal movement is allowed.</view>
      <movable-area>
        <movable-view direction="horizontal">text</movable-view>
      </movable-area>
    </view>

    <view class="page-section">
      <view class="page-section-title">only vertical movement is allowed.</view>
      <movable-area>
        <movable-view direction="vertical">text</movable-view>
      </movable-area>
    </view>

    <view class="page-section">
      <view class="page-section-title">Can exceed the boundary</view>
      <movable-area>
        <movable-view direction="all" out-of-bounds>
          text
        </movable-view>
      </movable-area>
    </view>

    <view class="page-section">
      <view class="page-section-title">With inertia</view>
      <movable-area>
        <movable-view direction="all" inertia>
          text
        </movable-view>
      </movable-area>
    </view>

    <view class="page-section">
      <view class="page-section-title">Scalable</view>
      <movable-area scale-area>
        <movable-view
          direction="all"
          bindchange="onChange"
          bindscale="onScale"
          scale
          scale-min="0.5"
          scale-max="4"
          scale-value="{{scale}}"
        >
          text
        </movable-view>
      </movable-area>
    </view>

    <view class="btn-area">
      <button bindtap="tap2" class="page-body-button" type="primary">
        Click to zoom in 3 times
      </button>
    </view>
  </view>
</view>

javascript:

js
Page({
  onShareAppMessage() {
    return {
      title: "movable-view",
      path: "page/component/pages/movable-view/movable-view",
    };
  },

  data: {
    x: 0,
    y: 0,
    scale: 2,
  },

  tap() {
    this.setData({
      x: 30,
      y: 30,
    });
  },

  tap2() {
    this.setData({
      scale: 3,
    });
  },

  onChange(e) {
    console.log(e.detail);
  },

  onScale(e) {
    console.log(e.detail);
  },
});

movable-view

  • 功能說明: 可移動的視圖容器,在頁面中可以拖拽滑動 movable-view 必須在 movable-area 組件中,並且必須是直接子節點,否則不能移動

  • 參數及說明:

内容類型必填說明
directionstringnonemovable-view的移動方向,屬性值有all、 vertical、horizontal、none
inertiabooleanfalsemovable-view是否帶有慣性
out-of-boundsbooleanfalse超過可移動區域後,movable-view是否還可以移動
xnumber / string0定義x軸方向的偏移,如果x的值不在可移動範圍內,會自動移動到可移動範圍; 改變x的值會觸發動畫
ynumber / string0定義y軸方向的偏移,如果y的值不在可移動範圍內,會自動移動到可移動範圍; 改變y的值會觸發動畫
dampingnumber20阻尼係數,用於控制x或y改變時的動畫和過界回彈的動畫,值越大移動越快
frictionnumber2摩擦係數,用於控制慣性滑動的動畫,值越大摩擦力越大,滑動越快停止; 必須大於0,否則會被設定成預設值
disabledbooleanfalse是否禁用
scalebooleanfalse是否支持雙指縮放,默認縮放手勢生效區域是在movable-view內
scale-minnumber0.5定義縮放倍數最小值
scale-maxnumber10定義縮放倍數最大值
scale-valuenumber1定義縮放倍數,取值範圍為0.5 - 10
animationbooleantrue是否使用動畫
bindchangeeventhandle-拖曳過程中觸發的事件,event.detail = {x: x, y: y, source: source},其中 source 表示產生移動的原因,數值可為:
touch:拖動;
touch-out-of-bounds: 超出移動範圍;
out-of-bounds: 超出移動範圍後的回彈;
friction: 慣性;
空字串: setData;
bindscaleeventhandle-縮放過程中觸發的事件,event.detail = {x: x, y: y, scale: scale}
除了基本事件外,movable-view提供了兩個特殊事件。
事件名;類型觸發條件
htouchmoveeventhandle初次手指觸摸後移動為橫向的移動,如果catch此事件,則意味著touchmove事件也被catch
vtouchmoveeventhandle初次手指觸摸後移動為縱向的移動,如果catch此事件,則意味著touchmove事件也被catch

TIP

  • movable-area必須設定width和height内容,不設定默認為10px。
  • movable-view默認為絕對定位,top和left内容為0px。

cover-image

  • 功能說明 覆蓋在原生組件之上的圖片視圖,可覆蓋的原生組件同 cover-view ,支持嵌套在 cover-view 裏。

  • 參數及說明:

内容類型預設值說明
srcstring-圖標路徑,支持臨時路徑、網路位址;
暫不支持SVG和BASE64格式;
bindloadeventhandle-圖片加載成功時觸發
binderroreventhandle-圖片加載失敗時觸發

cover-view

内容類型預設值說明
scroll-topnumber / string-設定頂部滾動偏移量,僅在設定了overflow-y: scroll成為滾動元素後生效

TIP

  • 事件模型遵循冒泡模型,但不會冒泡到原生組件。
  • 文字建議都套上cover-view標籤,避免排版錯誤。
  • 只支持基本的定位、佈局、文字樣式。 不支持設定單邊的border、background-image、shadow、overflow: visible等。
  • 建議子節點不要溢出父節點。
  • 支持使用z-index控制層級。
  • 默認設置的樣式有: white-space: nowrap; line-height: 1.2; display: block。
  • 自定義組件嵌套cover-view時,自定義組件的slot及其父節點暫不支持通過wx:if控制顯隱,否則會導致cover-view不顯示。

示例代碼:

wxml:

js
<view class="container">
<view class="page-body">
  <view class="page-section page-section-gap">
    <map
      style="width: 100%; height: 300px;"
      latitude="{{latitude}}"
      longitude="{{longitude}}"
    >
      <cover-view class="cover-view">
        <cover-view class="container">
          <cover-view class="flex-wrp" style="flex-direction:row;">
            <cover-view class="flex-item demo-text-1"></cover-view>
            <cover-view class="flex-item demo-text-2"></cover-view>
            <cover-view class="flex-item demo-text-3"></cover-view>
          </cover-view>
        </cover-view>
      </cover-view>
    </map>
  </view>
</view>
</view>

javascript:

js
Page({
  onShareAppMessage() {
    return {
      title: 'cover-view',
      path: 'page/component/pages/cover-view/cover-view'
    }
  },

  data: {
    latitude: 23.099994,
    longitude: 113.324520,
  }
})

wxss:

js
.cover-view {
  position: absolute;
  top: calc(50% - 150rpx);
  left: calc(50% - 300rpx);
  /* opacity: .7; */
}

.flex-wrp{
  display:flex;
}

.flex-item{
  width: 200rpx;
  height: 300rpx;
  font-size: 26rpx;
}

.demo-text-1 {
  background: rgba(26, 173, 25, 0.7);
}

.demo-text-2 {
  background: rgba(39, 130, 215, 0.7);
}

.demo-text-3 {
  background: rgba(255, 255, 255, 0.7);
}