原生小程序开发组件|视图容器组件汇总(一)

view

视图容器

属性说明

属性类型默认值必填说明备注
hover-classstringnone指定按下去的样式类。当 hover-class="none" 时,没有点击态效果
hover-start-timenumber50按住后多久出现点击态,单位毫秒
hover-stay-timenumber400手指松开后点击态保留时间,单位毫秒

示例代码

TYML
<view class="page-head">
	<view class="page-head-title">view</view>
	<view class="page-head-line"></view>
</view>
 
<view class="page-section">
  <view class="page-section-title l-r-padding">
    <view>flex-direction: row</view>
    <view>横向布局</view>
  </view>
  <view class="flex-wrp" style="flex-direction:row;justify-content: center;">
    <view class="flex-item demo-text-1">A</view>
    <view class="flex-item demo-text-2">B</view>
    <view class="flex-item demo-text-3">C</view>
  </view>
</view>
 
<view class="page-section">
  <view class="page-section-title l-r-padding">
    <view>flex-direction: column</view>
    <view>纵向布局</view>
  </view>
  <view class="flex-wrp" style="flex-direction:column;">
    <view class="flex-item flex-item-V demo-text-1">A</view>
    <view class="flex-item flex-item-V demo-text-2">B</view>
    <view class="flex-item flex-item-V demo-text-3">C</view>
  </view>
</view>

 

scroll-view

可滚动视图区域,可实现横向滚动和竖向滚动。使用竖向滚动时,需要给定一个固定高度,可以通过样式来设置 height。组件属性的长度单位支持 px 和 rpx。

属性说明

属性类型默认值必填说明备注
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在设置滚动条位置时使用动画过渡
refresher-enabledbooleanfalse开启自定义下拉刷新
refresher-thresholdnumber45设置自定义下拉刷新阈值
refresher-default-stylestring"black"设置自定义下拉刷新默认样式,支持设置 black、white、none, none 表示不使用默认样式
refresher-backgroundstring"#FFF"设置自定义下拉刷新区域背景颜色
refresher-triggeredbooleanfalse设置当前下拉刷新状态,true 表示下拉刷新已经被触发,false 表示下拉刷新未被触发
bind:scrolleventhandle--滚动时触
bind:scrolltouppereventhandle--滚动到顶部/左边时触发
bind:scrolltolowereventhandle--滚动到底部/右边时触发
bind:refresherpullingeventhandle--自定义下拉刷新控件被下拉
bind:refresherrefresheventhandle--自定义下拉刷新被触发
bind:refresherrestoreeventhandle--自定义下拉刷新被复位
bind:refresheraborteventhandle--自定义下拉刷新被中止

👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。  

示例代码

TYML
<view class="page-head">
	<view class="page-head-title">scroll-view</view>
	<view class="page-head-line"></view>
</view>
 
<view class="page-section">
  <view class="page-section-title l-r-padding">
    <text>Vertical Scroll</text>
    <view>纵向滚动</view>
  </view>
  <view class="page-section-spacing">
    <scroll-view
      style="height: 150px;"
      scrollY="{{true}}"
      bind:scroll="scroll"
      bind:scrolltoupper="upper"
      bind:scrolltolower="lower"
      >
      <view id="demo1" class="scroll-view-item demo-text-1">A</view>
      <view id="demo2"  class="scroll-view-item demo-text-2">B</view>
      <view id="demo3" class="scroll-view-item demo-text-3">C</view>
    </scroll-view>
  </view>
</view>
 
<view class="page-section">
  <view class="page-section-title l-r-padding">
    <text>Horizontal Scroll</text>
    <view>横向滚</view>
  </view>
  <view class="page-section-spacing">
    <scroll-view
      class="scroll-view_H"
      scrollX="{{true}}"
      scrollIntoView="{{toView}}"
      scrollWithAnimation="{{true}}"
      bind:scroll="scroll"
      bind:scrolltoupper="upper"
      bind:scrolltolower="lower"
      >
      <view class="box">
        <view id="demo4" class="scroll-view-item_H demo-text-1">A</view>
        <view id="demo5"  class="scroll-view-item_H demo-text-2">B</view>
        <view id="demo6" class="scroll-view-item_H demo-text-3">C</view>
      </view>
    </scroll-view>
  </view>
</view>

JS
Page({
  data: {
    toView: 'demo5',
  },
  upper(e) {
    console.log('demo upper', e);
  },
  lower(e) {
    console.log('demo lower', e);
  },
  scroll(e) {
    console.log('demo scroll', e);
  },
});

TYSS
.page-section {
  width: 100%;
  display: block;
  margin: 30px 0;
}
.page-section:last-child {
  margin-bottom: 0;
}
.page-section-spacing {
  margin-top: 30px;
  box-sizing: border-box;
  padding: 0 40px;
}
.scroll-view_H {
  width: 100%;
  white-space: nowrap;
}
.box {
  display: flex;
  flex-wrap: nowrap;
  width: 300%;
}
.scroll-view-item_H {
  flex: 1;
  height: 150px;
  color: #ffffff;
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.scroll-view-item {
  height: 150px;
  color: #ffffff;
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.demo-text-1 {
  background-color: #1aad19;
}
.demo-text-2 {
  background-color: #2782d7;
}
.demo-text-3 {
  background-color: #f1f1f1;
  color: #353535;
}

常见问题(FAQ)

为何 scroll-view 在 popup 扩展组件中无法滑动?

popup 组件上加上 disableScroll={{false}} 属性才能滑动。

如何监听 scroll-view 滚动到底部?

可以直接在 onScroll 方法中进行处理,使用 onScrollToLower 监听 scroll-view 的滚动高度来进行判断是否滑动到了底部。 scrollHeight 是 scroll-view 里面所有 view 的高度和,scrollTop 是滚动的值。

自定义页面蒙层的时候,当滚动蒙层里面的内容,蒙层底下页面也能跟着滑动?

可以给蒙层内部 scroll-view 或 view 添加 catch:touchmove事件,阻止事件冒泡。

 👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IoT砖家涂拉拉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值