上一章节
上一章节我们一起了解了在uniapp如何使用阿里巴巴的字体,因为在小程序里不能直接上传woff字体等文件,因此需要在线引入字体才行。传送门
什么是scroll-view
scroll-view是可滚动视图区域。用于区域滚动,可以设置垂直滚动和横向滚动两种方式。
官方文档
官方Demo
- 在pages中创建一个新的页面scrollView
- 复制官方文档里的Demo,下面看下复制完毕的Demo
<template>
<view>
<page-head title="scroll-view,区域滚动视图"></page-head>
<view class="uni-padding-wrap uni-common-mt">
<view class="uni-title uni-common-mt">
Vertical Scroll
<text>\n纵向滚动</text>
</view>
<view>
<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltoupper="upper" @scrolltolower="lower"
@scroll="scroll">
<view id="demo1" class="scroll-view-item uni-bg-red">A</view>
<view id="demo2" class="scroll-view-item uni-bg-green">B</view>
<view id="demo3" class="scroll-view-item uni-bg-blue">C</view>
</scroll-view>
</view>
<view @tap="goTop" class="uni-link uni-center uni-common-mt">
点击这里返回顶部
</view>
<view class="uni-title uni-common-mt">
Horizontal Scroll
<text>\n横向滚动</text>
</view>
<view>
<scroll-view class="scroll-view_H" scroll-x="true" @scroll="scroll" scroll-left="120">
<view id="demo1" class="scroll-view-item_H uni-bg-red">A</view>
<view id="demo2" class="scroll-view-item_H uni-bg-green">B</view>
<view id="demo3" class="scroll-view-item_H uni-bg-blue">C</view>
</scroll-view>
</view>
<view class="uni-common-pb"></view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
scrollTop: 0,
old: {
scrollTop: 0
}
}
},
methods: {
upper: function(e) {
console.log(e)
},
lower: function(e) {
console.log(e)
},
scroll: function(e) {
console.log(e)
this.old.scrollTop = e.detail.scrollTop
},
goTop: function(e) {
// 解决view层不同步的问题
this.scrollTop = this.old.scrollTop
this.$nextTick(function() {
this.scrollTop = 0
});
uni.showToast({
icon:"none",
title:"纵向滚动 scrollTop 值已被修改为 0"
})
}
}
}
</script>
<style>
.scroll-Y {
height: 300rpx;
}
.scroll-view_H {
white-space: nowrap;
width: 100%;
}
.scroll-view-item {
height: 300rpx;
line-height: 300rpx;
text-align: center;
font-size: 36rpx;
}
.scroll-view-item_H {
display: inline-block;
width: 100%;
height: 300rpx;
line-height: 300rpx;
text-align: center;
font-size: 36rpx;
}
.uni-bg-red {
background: #f76260;
color: #fff;
}
.uni-bg-green {
background: #09bb07;
color: #fff;
}
.uni-bg-blue {
background: #007aff;
color: #fff;
}
</style>
- 查看效果
升级版
效果展示
我们看看效果展示
代码逻辑如下:
- 先创建一个scroll-view标签,然后设置该标签横向滑动,设置默认滚动框的位置,设置不显示滚动框
<scroll-view class="scroll-view_H" scroll-x="true" @scroll="scroll" scroll-left="141" show-scrollbar="false">
...
<view ></view>
...
</scroll-view>
- 设置scroll-view标签中view属性间隔,这里使用magin属性进行间隔的。(这里只设置了第二个框框)
.cricle2{
margin: 0 150rpx;
}
- 设置默认的view样式
- 设置变大后的view的样式
- 通过methods方法获取当前滚动框的位置,然后将用户滑动的位置区间设置不同的变量,然后通过变量的变化去改变view的样式。这里判断的是tobigactive变成。若为一,则第一个框变大;若为二,则第二个框变大;若为三,则类似。
代码如下:
<template>
<view>
<!-- 升级版 -->
<view class="service_car">
<scroll-view class="scroll-view_H" scroll-x="true" @scroll="scroll" scroll-left="141" show-scrollbar="false">
<view id="" class="scroll-view-item_1"></view>
<view id="demo1" :class="['scroll-view-item_H','cricle1' , service_state==1? 'cricle_active':'',tobigactive==1?'toBig':'']" @click="prePutCar()">快乐童年</view>
<view id="demo2" :class="['scroll-view-item_H', 'cricle2', service_state==3? 'cricle_active':'',tobigactive==2?'toBig':'']" @click="getCar()">茁壮成长</view>
<view id="demo3" :class="['scroll-view-item_H', 'cricle3', service_state==3? 'cricle_active':'',tobigactive==3?'toBig':'']" @click="preGetCar()">相伴永远</view>
<view id="" class="scroll-view-item_1"></view>
</scroll-view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
scrollTop: 0,
old: {
scrollTop: 0
},
service_state:0,
//横向移动
scrollTop: 200,
old: {
scrollTop: 200
},
//变大效果
tobigactive:2
}
},
methods: {
upper: function(e) {
console.log(e)
},
lower: function(e) {
console.log(e)
},
scroll: function(e) {
console.log(e)
this.old.scrollTop = e.detail.scrollTop
//console.log('scroll',e.detail.scrollLeft);
//this.old.scrollTop = e.detail.scrollTop
if(e.detail.scrollLeft<45){
this.tobigactive=1;
}else if(e.detail.scrollLeft<190){
this.tobigactive=2;
}else{
this.tobigactive=3;
}
},
goTop: function(e) {
// 解决view层不同步的问题
this.scrollTop = this.old.scrollTop
this.$nextTick(function() {
this.scrollTop = 0
});
uni.showToast({
icon:"none",
title:"纵向滚动 scrollTop 值已被修改为 0"
})
}
}
}
</script>
<style lang="less">
.scroll-Y {
height: 300rpx;
}
.scroll-view_H {
white-space: nowrap;
width: 100%;
}
.scroll-view-item {
height: 300rpx;
line-height: 300rpx;
text-align: center;
font-size: 36rpx;
}
.scroll-view-item_H {
display: inline-block;
width: 100%;
height: 300rpx;
line-height: 300rpx;
text-align: center;
font-size: 36rpx;
}
.uni-bg-red {
background: #f76260;
color: #fff;
}
.uni-bg-green {
background: #09bb07;
color: #fff;
}
.uni-bg-blue {
background: #007aff;
color: #fff;
}
.scroll-view-item_1{
display: inline-block;
height: 200rpx;
width: 200rpx;
}
.cricle_active{
background-color: #00aa00;
box-shadow: 2px 2px 3px #aaa;
}
.toBig{
width: 270rpx;
height: 270rpx;
line-height: 270rpx;
border-radius: 150rpx;
font-size: 52rpx;
font-weight: 600;
}
.cictle{
width: 750rpx;
position: absolute;
left: 30%;
transform: translate(-50%,-50%);
-webkit-perspective: 1500;
-moz-perspective: 1500;
.box{
position: absolute;
box-shadow: 0px 7px 7px #888888;
top: 0;
font-size: 40rpx;
left: 53%;
width: 270rpx;
height: 270rpx;
transition: all 1s;
backface-visibility: hidden;
cursor: pointer;
border-radius: 150rpx;
background: #FFFFFF;
color:#000;
display: flex;
align-items: center;
justify-content : space-around;
flex-direction:column;
font-weight: 600;
text:nth-child(1) {
font-size: 110rpx;
} text:nth-child(2) {
margin-bottom: 10rpx;
font-size: 33rpx;
}
}
}
.scroll-view_H {
white-space: nowrap;
width: 100%;
.cricle2{
margin: 0 150rpx;
}
.scroll-view-item_1{
display: inline-block;
height: 200rpx;
width: 200rpx;
}
.scroll-view-item_H{
background-color: #a6e5af;
border-radius: 100rpx;
display: inline-block;
height: 200rpx;
width: 200rpx;
line-height: 200rpx;
text-align: center;
font-size: 40rpx;
color: #FFFFFF;
transition: 2.5s;
margin-bottom: 20rpx;
}
.cricle_active{
background-color: #a6e5af;
box-shadow: 2px 2px 3px #aaa;
}
.toBig{
width: 270rpx;
height: 270rpx;
line-height: 270rpx;
border-radius: 150rpx;
font-size: 52rpx;
font-weight: 600;
background-color: #00aa00;
}
}
</style>