微信小程序scroll-view组件官方文档  传送门

  提前准备:使用<view>组件制作五条撑满的横向区域

<!--index.wxml-->
Cynical丶Gary
<view > <!-- 手指按下后1s样式由a改变为d,手指松开后2s样式由d改变回a -->
<view class='a size'>a</view>
<view class='b size'>b</view>
<view class='c size'>c</view>
<view class='d size'>d</view>
<view class='e size'>e</view>
</view>

index.wxml

.container{
display: flex; } .size{
width: 100%;
height: 150rpx;
} .a{
background: red;
order:;
flex:;
} .b{
background: yellow;
order:;
flex:;
} .c{
background: blue;
order:;
flex:;
} .d{
background: green;
order:;
flex:;
} .e{
background: orange;
order:;
flex:;
}

index.wxss

 

  Learn

    一、scroll-y属性

    二、scroll-x属性

    

一、scroll-y属性

  scroll-y:允许纵向滚动【默认值false】

  使用<scroll-view>组件可以设置小程序中<view>组件纵向滚动,使用<scroll-view>组件时注意必须设置高度的样式如:style='height:300rpx'

<!--index.wxml-->
Cynical丶Gary
<scroll-view scroll-y="true" style='height:300rpx'> <!-- 手指按下后1s样式由a改变为d,手指松开后2s样式由d改变回a -->
<view class='a size'>a</view>
<view class='b size'>b</view>
<view class='c size'>c</view>
<view class='d size'>d</view>
<view class='e size'>e</view> </scroll-view>

index.wxml

.container{
display: flex; } .size{
width: 100%;
height: 150rpx;
} .a{
background: red;
order:;
flex:;
} .b{
background: yellow;
order:;
flex:;
} .c{
background: blue;
order:;
flex:;
} .d{
background: green;
order:;
flex:;
} .e{
background: orange;
order:;
flex:;
}

index.wxss

  upper-threshold:距顶部/左边多远时(单位px,2.4.0起支持rpx),触发 scrolltoupper 事件【默认值50】

  lower-threshold:距底部/右边多远时(单位px,2.4.0起支持rpx),触发 scrolltolower 事件【默认值50】

  bindscrolltoupper:滚动到顶部/左边,会触发 scrolltoupper 事件

  bindscrolltolower:滚动到底部/右边,会触发 scrolltolower 事件

  给<scroller-view>组件添加滚动触发事件

<scroll-view scroll-y="true" style='height:300rpx;'  bindscrolltoupper="scrolltoupper" bindscrolltolower="scrolltolower" >

  并在index.js中添加scrolltoupper函数事件和scrolltolower函数事件

  scrolltoupper:function(){
console.log("滚动到顶部");
},
scrolltolower:function(){
console.log("滚动到底部");
}

<!--index.wxml-->
Cynical丶Gary
<scroll-view scroll-y="true" style='height:300rpx;'
bindscrolltoupper="scrolltoupper" bindscrolltolower="scrolltolower" > <!-- 手指按下后1s样式由a改变为d,手指松开后2s样式由d改变回a -->
<view class='a size'>a</view>
<view class='b size'>b</view>
<view class='c size'>c</view>
<view class='d size'>d</view>
<view class='e size'>e</view> </scroll-view>

index.wxml

.container{
display: flex; } .size{
width: 100%;
height: 150rpx;
} .a{
background: red;
order:;
flex:;
} .b{
background: yellow;
order:;
flex:;
} .c{
background: blue;
order:;
flex:;
} .d{
background: green;
order:;
flex:;
} .e{
background: orange;
order:;
flex:;
}

index.wxss

Page({
data:{ }, scrolltoupper:function(){
console.log("滚动到顶部");
},
scrolltolower:function(){
console.log("滚动到底部");
} })

index.js

  当然我们也可以给<scroll-view>添加两个额外的属性upper-thresholdlower-threshold

  upper-threshold:距顶部/左边多远时(单位px,2.4.0起支持rpx),触发 scrolltoupper 事件【默认值50】

  lower-threshold:距底部/右边多远时(单位px,2.4.0起支持rpx),触发 scrolltolower 事件【默认值50】

  使用方法

<scroll-view scroll-y="true" style='height:300rpx;'
bindscrolltoupper="scrolltoupper" bindscrolltolower="scrolltolower" upper-threshold="0" lower-threshold="0">

  scroll-top:设置竖向滚动条位置(单位px,2.4.0起支持rpx)【无默认值】

  可以看到,当我们设置scroll-top="100"时,滚动条默认出现在距离顶部100px位置的地方,当我们设置scroll-top="150"时,滚动条默认出现在距离顶部150px位置的地方

<!--index.wxml-->
Cynical丶Gary
<scroll-view scroll-y="true" style='height:300rpx;'
bindscrolltoupper="scrolltoupper" bindscrolltolower="scrolltolower"
upper-threshold="0"
lower-threshold="0"
scroll-top="150"> <!-- 手指按下后1s样式由a改变为d,手指松开后2s样式由d改变回a -->
<view class='a size'>a</view>
<view class='b size'>b</view>
<view class='c size'>c</view>
<view class='d size'>d</view>
<view class='e size'>e</view> </scroll-view>

index.wxml

.container{
display: flex; } .size{
width: 100%;
height: 150rpx;
} .a{
background: red;
order:1;
flex:10;
} .b{
background: yellow;
order:2;
flex:2;
} .c{
background: blue;
order:3;
flex:1;
} .d{
background: green;
order:4;
flex:1;
} .e{
background: orange;
order:5;
flex:1;
}

index.wxss

Page({
data:{ }, scrolltoupper:function(){
console.log("滚动到顶部");
},
scrolltolower:function(){
console.log("滚动到底部");
} })

index.js

  enable-back-to-top:iOS点击顶部状态栏、安卓双击标题栏时,滚动条返回顶部,只支持竖向【默认值false】

  注意:微信小程序开发工具中默认在web环境中进行开发,而此属性需要在手机上运行~

  bindscroll:滚动时触发,event.detail = {scrollLeft, scrollTop, scrollHeight, scrollWidth, deltaX, deltaY}

  在index.js中添加滚动时触发的函数bindscroll()

  bindscroll:function(){
console.log("滚动中~");
}

  在index.wxml中进行事件绑定bindscroll="bindscroll"

  <scroll-view scroll-y="true" style='height:300rpx;'
  bindscrolltoupper="scrolltoupper" bindscrolltolower="scrolltolower"
  upper-threshold="0"
  lower-threshold="0"
  scroll-top="150"
  enable-back-to-top="true"
  bindscroll="bindscroll">

<!--index.wxml-->
Cynical丶Gary
<scroll-view scroll-y="true" style='height:300rpx;'
bindscrolltoupper="scrolltoupper" bindscrolltolower="scrolltolower"
upper-threshold="0"
lower-threshold="0"
scroll-top="150"
enable-back-to-top="true"
bindscroll="bindscroll"> <!-- 手指按下后1s样式由a改变为d,手指松开后2s样式由d改变回a -->
<view class='a size'>a</view>
<view class='b size'>b</view>
<view class='c size'>c</view>
<view class='d size'>d</view>
<view class='e size'>e</view> </scroll-view>

index.wxml

Page({
data:{ }, scrolltoupper:function(){
console.log("滚动到顶部");
},
scrolltolower:function(){
console.log("滚动到底部");
},
bindscroll:function(){
console.log("滚动中~");
} })

index.js

  

  scroll-into-view:值应为某子元素id(id不能以数字开头)。设置哪个方向可滚动,则在哪个方向滚动到该元素

  index.wxml中给五个<view>组件添加id元素

  <view id="a" class='a size'>a</view>
<view id="b" class='b size'>b</view>
<view id="c" class='c size'>c</view>
<view id="d" class='d size'>d</view>
<view id="e" class='e size'>e</view>

  通过scroll-into-view给<scroll-view>组件添加属性

  <scroll-view scroll-y="true" style='height:300rpx;'
  bindscrolltoupper="scrolltoupper" bindscrolltolower="scrolltolower"
  upper-threshold="0"
  lower-threshold="0"
  scroll-into-view="e"
  enable-back-to-top="true"
  bindscroll="bindscroll">

<!--index.wxml-->
Cynical丶Gary
<!-- scroll-top="150" -->
<scroll-view scroll-y="true" style='height:300rpx;'
bindscrolltoupper="scrolltoupper" bindscrolltolower="scrolltolower"
upper-threshold="0"
lower-threshold="0"
scroll-into-view="e"
enable-back-to-top="true"
bindscroll="bindscroll"> <!-- 手指按下后1s样式由a改变为d,手指松开后2s样式由d改变回a -->
<view id="a" class='a size'>a</view>
<view id="b" class='b size'>b</view>
<view id="c" class='c size'>c</view>
<view id="d" class='d size'>d</view>
<view id="e" class='e size'>e</view> </scroll-view>

index.wxml

Page({
data:{ }, scrolltoupper:function(){
console.log("滚动到顶部");
},
scrolltolower:function(){
console.log("滚动到底部");
},
bindscroll:function(){
console.log("滚动中~");
} })

index.js

二、scroll-x属性

  scroll-x:允许横向滚动【默认值为false】

  scroll-left:设置横向滚动条位置(单位px,2.4.0起支持rpx)

<!--index.wxml-->
Cynical丶Gary
<scroll-view class='container' scroll-x="true" style='height:300rpx;'
scroll-left="200">
<!-- 手指按下后1s样式由a改变为d,手指松开后2s样式由d改变回a -->
<view id="a" class='a size'>a</view>
<view id="b" class='b size'>b</view>
<view id="c" class='c size'>c</view>
<view id="d" class='d size'>d</view>
<view id="e" class='e size'>e</view>
</scroll-view>

index.wxml

.container{
display: flex;
white-space: nowrap;
} .size{
width: 250rpx;
height: 150rpx;
display:inline-block;
} .a{
background: red;
order:;
flex:;
} .b{
background: yellow;
order:;
flex:;
} .c{
background: blue;
order:;
flex:;
} .d{
background: green;
order:;
flex:;
} .e{
background: orange;
order:;
flex:;
}

index.wxss

Page({
data:{ }, scrolltoupper:function(){
console.log("滚动到顶部");
},
scrolltolower:function(){
console.log("滚动到底部");
},
bindscroll:function(){
console.log("滚动中~");
} })

index.js

微信小程序_(组件)scroll-view可滚动视图的更多相关文章

  1. 微信小程序_(组件)icon、text、rich-text、progress四大基础组件

    微信小程序基础组件官方文档 传送门 Learn 一.icon图标组件 二.rich-text富文本组件 三.text文本组件 四.progress进度条组件 一.icon图标组件 type:icon的 ...

  2. 微信小程序_(组件)可拖动movable-view

    微信小程序movable-view组件官方文档 传送门 Learn 一.moveable-view组件 一.movable-view组件 direction:movable-view的移动方向,属性值 ...

  3. 微信小程序_(组件)view视图容器

    微信小程序view组件官方文档 传送门 Learn 一.hover-class属性 二.hover-start-time与hover-stay-time属性 三.hover-stop-propagat ...

  4. 微信小程序_(组件)picker

    picker组件效果 官方文档:传送门 Page({ data: { array: ['美国', '中国', '巴西', '日本'], objectArray: [ { id: 0, name: '美 ...

  5. 微信小程序_(组件)组件基础

    (progress.text.block) 组件基础效果 官方文档:传送门 Page({ /** * 页面的初始数据 */ data: { text:"Gary 微信小程序\n", ...

  6. 微信小程序_(组件)canvas画布

    canvas画布效果 官方文档:传送门 Page({ canvasIdErrorCallback: function (e) { console.error(e.detail.errMsg) }, o ...

  7. 微信小程序_(组件)swiper轮播图

    微信小程序swiper轮播图组件官方文档 传送门 Learn: swiper组件 一.swiper组件 indicator-dots:是否显示面板指示点[默认值false] autoplay:是否自动 ...

  8. 微信小程序_(组件)flex布局

    小程序建议使用flex布局进行排版 flex是一个盒装弹性布局 flex是一个容器,所有子元素都是他的成员 定义布局:display:flex flex容器的属性: 一.flex-direction: ...

  9. 微信小程序_(组件)form表单

    Form表单.switch开关.数值选择器效果 官方文档:传送门 点击提交表单(按钮,提交开关,数值选择器,输入文本中)的值,显示在控制台上,点击重置,重置表单中的值. 实现过程 form表单,添加f ...

随机推荐

  1. GET方法和POST方法的区别,Get方法到底可传递的字符串的最大长度是多少?

    GET方法和POST方法的区别,Get方法到底可传递的字符串的最大长度是多少?曾经人介绍,如果使用GET方式传输参数,URL的最大长度是256个字节,对此深信不疑. 但是最近看到一些超长的url,能够 ...

  2. 【原创】大叔经验分享(69)docker启动java应用的时区问题

    在docker中启动tomcat或java类应用,获取时间默认是UTC时间,这是因为容器内的locale没有设置为东8区,最简单的方式是增加JAVA_OPTS 如果是java,直接在java命令后增加 ...

  3. git的详细安装

    git的详细安装 Git 是时候动手尝试下 Git 了,不过得先安装好它.有许多种安装方式,主要分为两种,一种是通过编译源代码来安装:另一种是使用为特定平台预编译好的安装包. 从源代码安装 若是条件允 ...

  4. Java基础第四天--常用API

    常用API 基本类型包装类概述 将基本数据类型封装成对象的好处可以在对象中定义更多的功能方法操作该数据 常用的操作之一:用于基本数据类型与字符串之间的转换 基本数据类型 包装类 byte Byte s ...

  5. Json-server在Vue 2.0中使用--build文件中没有dev-server文件

    跟大佬的视频使用json-server模拟后台数据调用,发现build文件中并没有dev-server.js. 新版的vue-cli取消了dev-server.js和dev-client.js   改 ...

  6. 配置rsync同步文件到nas

    windows下以前的做法是安装一个cygwin包,现在不需要了,直接安装一个linux子系统用linux命令就行了. start cmd /k "c:\cygwin64\bin\rsync ...

  7. VMwarevSphere Client 链接 vCenter Server中的主机,开启虚拟机提示:在主机当前连接状况下不允许执行该操作

    VMwarevSphere Client 链接 vCenter Server中的主机,开启虚拟机提示:在主机当前连接状况下不允许执行该操作很多原因都可以导致该问题出现,例如 vCenter Serve ...

  8. 【异常】postman能够请求成功获取到参数,前端请求的却请求不到

    1 前端联调的时候,反馈自己的参数没有生效,无论传递任何参数都是一样的结果 盯了一下日志发现 postman请求的是   :{"getParameter":{"provi ...

  9. 【转】关于IAP与APP互相跳转的实现

    关于IAP与APP互相跳转的实现 首先,在您动手做这个实验之前,先要弄清除咱俩的软硬件有什么不同: 1. 我的CPU是STM32F103ZET6,里面有512K的FLASH,您的CPU如果是其它类型, ...

  10. weakref:对象的弱引用

    介绍 weakref支持对象的弱引用,正常的引用会增加对象的引用计数,并避免它被垃圾回收.但结果并不是总和期望的那样,比如有时候可能会出现一个循环引用,或者有时候需要内存时可能要删除对象的缓存.而弱引 ...