16. 视图容器

2023-06-15 11:41:23发布
52

view

视图容器

属性类型默认值必填说明最低版本
hover-classstringnone指定按下去的样式类。当 hover-class="none" 时,没有点击态效果1.0.0
hover-stop-propagationbooleanfalse指定是否阻止本节点的祖先节点出现点击态1.5.0
hover-start-timenumber50按住后多久出现点击态,单位毫秒1.0.0
hover-stay-timenumber400手指松开后点击态保留时间,单位毫秒1.0.0

例子 :

<view>
    <view class="myview" hover-class="myviewHover" hover-start-time="50" hover-stay-time="1000"></view>
</view>

wxss

.myview {
  width: 100px;
  height: 100px;
  background: red;
}

.myviewHover {
  background: green;
}


scroll-view

可滚动视图区域

属性名类型默认值说明
scroll-xBooleanfalse允许横向滚动
scroll-yBooleanfalse允许纵向滚动
upper-thresholdNumber50距顶部/左边多远时(单位px),触发 scrolltoupper 事件
lower-thresholdNumber50距底部/右边多远时(单位px),触发 scrolltolower 事件
scroll-topNumber 设置竖向滚动条位置
scroll-leftNumber 设置横向滚动条位置
scroll-into-viewString 值应为某子元素id(id不能以数字开头)。设置哪个方向可滚动,则在哪个方向滚动到该元素
scroll-with-animationBooleanfalse在设置滚动条位置时使用动画过渡
enable-back-to-topBooleanfalseiOS点击顶部状态栏、安卓双击标题栏时,滚动条返回顶部,只支持竖向。该功能需要真机
bindscrolltoupperEventHandle 滚动到顶部/左边,会触发 scrolltoupper 事件
bindscrolltolowerEventHandle 滚动到底部/右边,会触发 scrolltolower 事件
bindscrollEventHandle 滚动时触发,event.detail = {scrollLeft, scrollTop, scrollHeight, scrollWidth, deltaX, deltaY}

使用竖向滚动时,需要给<scroll-view/>一个固定高度,通过wxss设置 height

例子 :

wxml

<scroll-view 
  class='scroll-view' 
  scroll-y
  upper-threshold="50"
  bindscrolltoupper="scrolltoupper"
  lower-threshold="50"
  bindscrolltolower="scrolltolower"
  scroll-with-animation
  enable-back-to-top
  scroll-into-view="item6"
  bindscroll="bindscroll"
>
    <view class='view-item' style='background: green;'>1</view>
    <view class='view-item' style='background: orange;'>2</view>
    <view class='view-item' style='background: pink;'>3</view>
    <view class='view-item' style='background: silver;'>4</view>
    <view class='view-item' style='background: blue;'>5</view>
    <view class='view-item' id="item6" style='background: red'>6</view>
</scroll-view>

js

Page({
  scrolltoupper() {
    console.log('scrolltoupper');
  },
  scrolltolower() {
    console.log('scrolltolower');
  },
  bindscroll(e) {
    console.log(e)
  }
})

wxss

.scroll-view {
  height: 800rpx;
}
.view-item {
  height: 250rpx;
}


Bug & Tip

1. 请勿在scroll-view中使用textarea、map、canvas、video组件

2. scroll-into-view的优先级高于scroll-top

3. 在滚动scroll-view时会阻止页面回弹,所以在scroll-view中滚动,是无法触发onPullDownRefresh

4. 若要使用下拉刷新,请使用页面的滚动,而不是scroll-view,这样也能通过点击顶部状态栏回到页面顶部


swiper

滑块视图容器

属性类型默认值必填说明最低版本
indicator-dotsbooleanfalse是否显示面板指示点1.0.0
indicator-colorcolorrgba(0, 0, 0, .3)指示点颜色1.1.0
indicator-active-colorcolor#000000当前选中的指示点颜色1.1.0
autoplaybooleanfalse是否自动切换1.0.0
currentnumber0当前所在滑块的 index1.0.0
intervalnumber5000自动切换时间间隔1.0.0
durationnumber500滑动动画时长1.0.0
circularbooleanfalse是否采用衔接滑动1.0.0
verticalbooleanfalse滑动方向是否为纵向1.0.0
previous-marginstring"0px"前边距,可用于露出前一项的一小部分,接受 px 和 rpx 值1.9.0
next-marginstring"0px"后边距,可用于露出后一项的一小部分,接受 px 和 rpx 值1.9.0
display-multiple-itemsnumber1同时显示的滑块数量1.9.0
skip-hidden-item-layoutbooleanfalse是否跳过未显示的滑块布局,设为 true 可优化复杂情况下的滑动性能,但会丢失隐藏状态滑块的布局信息1.9.0
easing-functionstring"default"指定 swiper 切换缓动动画类型2.6.5
bindchangeeventhandlecurrent 改变时会触发 change 事件,event.detail = {current, source}1.0.0
bindtransitioneventhandleswiper-item 的位置发生改变时会触发 transition 事件,event.detail = {dx: dx, dy: dy}2.4.3
bindanimationfinisheventhandle动画结束时会触发 animationfinish 事件,event.detail 同上1.9.0

easing-function 的合法值

说明
default默认缓动函数
linear线性动画
easeInCubic缓入动画
easeOutCubic缓出动画
easeInOutCubic缓入缓出动画

例子 :

wxml

<view>
  <swiper 
    indicator-dots="{{ cf.indicatorDots }}" 
    indicator-color="{{ cf.indicatorColor }}" 
    indicator-active-color="{{ cf.indicatorActiveColor }}"
    autoplay="{{ cf.autoplay }}"
    interval="{{ cf.interval }}"
    circular="{{ cf.circular }}"
    vertical="{{ cf.vertical }}"
    previous-margin="{{ cf.previousMargin }}"
    next-margin="{{ cf.nextMargin }}"
    display-multiple-items="{{ cf.displayMultipleItems }}"
  >
    <swiper-item style="background: red;">1</swiper-item>
    <swiper-item style="background: green;">2</swiper-item>
    <swiper-item style="background: blue;">3</swiper-item>
  </swiper>
</view>

js

Page({
  data: {
    cf: {
      indicatorDots: true,
      indicatorColor: '#fff',
      indicatorActiveColor: '#c00',
      autoplay: true,
      interval: 1000,
      circular: true,
      vertical: true,
      previousMargin: '0px',
      nextMargin: '0px',
      displayMultipleItems: 2,
    }
  }
})


movable-area

https://www.w3cschool.cn/weixinapp/weixinapp-movable-view.html


cover-view

https://www.w3cschool.cn/weixinapp/weixinapp-neqy38d9.html


cover-image

https://www.w3cschool.cn/weixinapp/weixinapp-19xz38dd.html