
可滚动视图渗扮区域。
组件用法:
纵向滚动用法
Tip:
使用竖向滚动时,需要给一个固定高度,通过 WXSS 设置 height,否则无法滚动。
当滚动到顶部时会触发bindscrolltoupper事件(具体可留意GIF输出)
当滚动到底部时会触发bindscrolltolower事件(具体可留意GIF输出)
效果图:
wxml
<scroll-view scroll-y="true" style="height: 200px" bindscrolltoupper="upper" bindscrolltolower="lower" bindscroll="scroll" scroll-into-view="{{toView}}" scroll-top="{{scrollTop}}">
<view id="green" class="scroll-view-item bc_green"></view>
<view id="red" class="scroll-view-item bc_red"></view>
<view id="yellow" class="scroll-view-item bc_yellow"></view>
<view id="blue" class="scroll-view-item bc_blue"></view>
</scroll-view>
<view class="btn-area">
<button size="mini" bindtap="tap">click me to scroll into view </button>
<button size="mini" bindtap="tapMove">click me to scroll</button>
</view>
js
var order = ['red', 'yellow', 'blue', 'green', '亏喊稿red']
Page({
data: {
toView: 'green',
scrollTop: 100,
scrollLeft: 0
},
//滚动条滚到顶部的时候触发
upper: function(e) {
console.log(e)
},
//滚动条滚到底部的时候触发
lower: function(e) {
console.log(e)
},
//滚动条滚动后触发
scroll: function(e) {
console.log(e)
},
//点击按钮切换到下一个view
tap: function(e) {
for (var i = 0i <order.length++i) {
if (order[i] === this.data.toView) {
this.setData({
toView: order[i + 1]
})
break
}
}
},
//通过设置滚动条位置实现画面滚动
tapMove: function(e) {
this.setData({
scrollTop: this.data.scrollTop + 10
})
}
})
css
.scroll-view_H{
white-space: nowrap
}
.scroll-view-item{
height: 200px
}
.scroll-view-item_H{
display: inline-block
width: 100%
height: 200px
}
横向滚动用法
Tip:
横向滚销孝动无法使用scroll-into-view属性。
当滚动到最左边时会触发bindscrolltoupper事件(具体可留意GIF输出)
当滚动到最右边时会触发bindscrolltolower事件(具体可留意GIF输出)
效果图:
wxml
<scroll-view class="scroll-view_H" scroll-x="true" style="width: 100%" bindscrolltoupper="upper" bindscrolltolower="lower" bindscroll="scroll" scroll-left="{{scrollLeft}}">
<view id="green" class="scroll-view-item_H bc_green"></view>
<view id="red" class="scroll-view-item_H bc_red"></view>
<view id="yellow" class="scroll-view-item_H bc_yellow"></view>
<view id="blue" class="scroll-view-item_H bc_blue"></view>
</scroll-view>
js
var order = ['red', 'yellow', 'blue', 'green', 'red']
Page({
data: {
toView: 'green',
scrollTop: 100,
scrollLeft: 0
},
//滚动条滚到顶部的时候触发
upper: function(e) {
console.log(e)
},
//滚动条滚到底部的时候触发
lower: function(e) {
console.log(e)
},
//滚动条滚动后触发
scroll: function(e) {
console.log(e)
},
//点击按钮切换到下一个view
tap: function(e) {
for (var i = 0i <order.length++i) {
if (order[i] === this.data.toView) {
this.setData({
toView: order[i + 1]
})
break
}
}
},
//通过设置滚动条位置实现画面滚动
tapMove: function(e) {
this.setData({
scrollLeft: this.data.scrollLeft + 10
})
}
})
wxss
.scroll-view_H{
white-space: nowrap
}
.scroll-view-item{
height: 200px
}
.scroll-view-item_H{
display: inline-block
width: 100%
height: 200px
}
主要属性:
属性类型默认值描述
scroll-xBooleanfalse允许横向滚动
scroll-yBooleanfalse允许纵向滚动
upper-thresholdNumber50距顶部/左边多远时(单位px),触发 scrolltoupper 事件
lower-thresholdNumber50距底部/右边多远时(单位px),触发 scrolltolower 事件
scroll-topNumber设置竖向滚动条位置
scroll-leftNumber设置横向滚动条位置
scroll-into-viewString值应为某子元素id,则滚动到该元素,元素顶部对齐滚动区域顶部
bindscrolltoupperEventHandle滚动到顶部/左边,会触发 scrolltoupper 事件
bindscrolltolowerEventHandle滚动到底部/右边,会触发 scrolltolower 事件
bindscrollEventHandle滚动时触发,event.detail = {scrollLeft, scrollTop, scrollHeight, scrollWidth, deltaX, deltaY}
1、利用样式实现小程序动画(用法和css用法相识)
wxml 文件
<image class="aniamtion" src="../../images/page4.jfif" style="width:200rpxheight:200rpx position: relative"></image>
wxss文件
.aniamtion {
animation: mymove 5s infinite
/* //infinite属性是表示无限循环的意思,没有这个属性的话动画只执行一次。 */
}
@keyframes mymove {
from {
/* left: 0px*/
/* transform: rotate(7deg) skew(50deg) translate(30rpx,30rpx)*/
transform: rotate3d(100,200,300,0deg)
}
to {
/* left: 200px*/
/* transform: rotate(7deg) skew(5deg) translate(100rpx,100rpx)*/
transform: rotate3d(200,300,400,360deg)
}
}
2、 用小程序的API来实现动画
用wx.createAnimation(object) 来创建一个动画 -->返回一个animation对象
创建一个动画实例 animation。
onReady: function () {
this.animation = wx.createAnimation({
duration:1000,
timingFunction:'linear',
delay:100,
transformOrigin:"left top 0"
})
},
调用实例的方法来描述动画。
Animation.step() 表示一组动画的完成迟袜,可以在一组动画中调用任意多个动画方法,一组动画中的所有动画会同时开始,一组动画完成后才会进行下一组动画
rotate(){
this.animation.rotate(150).step() //对动画码配激进行简单的描述
this.setData({
animation:this.animation.export()
})
},
最后通过动卖茄画实例的 export 方法导出动画数据传递给组件的 animation 属性。
this.animation.export() 导出动画队列。export 方法每次调用后会清掉之前的动画 *** 作
rotate(){
this.animation.rotate(150).step() //对动画进行简单的描述
this.setData({ // 在setData({}) 导出动画数据数据给组件
animation:this.animation.export()
})
},
完整的wxml
<view class="container">
<view animation="{{animation}}" class="view">
将做动画的块
</view>
</view>
<button type="default" size="mini" bindtap="rotate">
旋转
</button>
完整的wxjs
Page({
data: {
animation:''
},
onReady: function () {
this.animation = wx.createAnimation({
duration:1000,
timingFunction:'linear',
delay:100,
transformOrigin:"left top 0"
})
},
rotate(){
this.animation.rotate(150).step().translate(100).step()
this.setData({
animation:this.animation.export()
})
}
})
3、用选择器来绑定组件来来实现组件的动画(小程序2.9.0 的库可用,版本不够会报this.animate不是一个方法)
<text>pages/index7/index7.wxml</text>
<view id="container" style="height: 100pxwidth: 100pxbackground-color: blue">
container
</view>
<view class="block" style="height: 100pxwidth: 100pxbackground-color: #ccc">
block
</view>
用选择器选择相应的组件进行相应的动画
进行关键帧的处理
onLoad: function () {
this.animate('#container', [
{ opacity: 1.0, rotate: 0, backgroundColor: '#FF0000' },
{ opacity: 0.5, rotate: 45, backgroundColor: '#00FF00' },
{ opacity: 1.0, rotate: 90, backgroundColor: '#FF0000' },
], 5000)
this.animate('.block', [
{ scale: [1, 1], rotate: 0, ease: 'ease-out' },
{ scale: [1.5, 1.5], rotate: 45, ease: 'ease-in'},
{ scale: [2, 2], rotate: 90 },
], 5000)
},
}
4、用第三方的库 animation.css
需要做的有
从https://daneden.github.io/animate.css/下载css动画文件
把 .css 文件 改名成 .wxss文件(可进行相应的需改,毕竟小程序的大小限制摆在那里)
把它引入到你的app.wxss文件中
@import “动画文件的相对目录”
在用的时候把他和你的样式绑定
<view class="swing" style="height: 100pxwidth: 100pxbackground-color: #ccc">
block
</view>
// 给类名为swing 的文件绑定swing 的动画
.swing{
animation: swing 5s infinite
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)