uniapp scroll-view基础用法

这篇具有很好参考价值的文章主要介绍了uniapp scroll-view基础用法。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前言

        在uniapp日常开发的过程中经常会有局部滚动的需求,而scroll-view组件正好可以满足这一需求。需注意在webview渲染的页面中,区域滚动的性能不及页面滚动。

纵向滚动

        将scroll-view组件中的属性scroll-y设定为true开启纵向滚动功能,给scroll-view设置一个高度,当内容高度大于scroll-view高度时即可开启滚动功能(内容高度小于scroll-view高度时无法体现滚动功能)

实现代码:

<template>
	<view>
		<scroll-view scroll-y="true" style="height: 700rpx;">
			<view v-for="(item,index) in 3" style="height: 500rpx;" :style="{ backgroundColor: colorList[index]}">
				{{index}}
			</view>
		</scroll-view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				colorList:["blue","red","yellow"]
			}
		},
		methods: {
			
		}
	}
</script>

<style>

</style>

效果图:

uniapp scroll-view基础用法

横向滚动

        将scroll-view组件中的属性scroll-x设定为true开启横向滚动功能,给scroll-view设置一个宽度,当内容宽度大于scroll-view宽度时即可开启滚动功能(内容宽度小于scroll-view宽度时无法体现滚动功能)

        注意:scroll-view本身的display:flex不生效、如果想实现display:flex功能,则可以给scroll-view加上white-space: nowrap,给内容容器加上display:inline-block

实现代码:

<template>
	<view>
		<scroll-view  scroll-x="true" style="height: 500rpx;white-space: nowrap;">
			<view v-for="(item,index) in 3" style="height: 500rpx;width: 100%;display: inline-block;" :style="{ backgroundColor: colorList[index]}">
				{{index}}
			</view>
		</scroll-view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				colorList:["blue","red","yellow"]
			}
		},
		methods: {
			
		}
	}
</script>

<style>

</style>

效果图:

uniapp scroll-view基础用法

 锚点定位

        当我们已进入页面就需要滚动到某一个元素的时候,锚点定位就可以很好的帮助我们定位并滚动到指定位置

        将scroll-with-animation设定为true开启动画效果、对scroll-into-view进行动态绑定

        注意:scroll-into-view绑定的值得是字符串,使用其他类型则会报错

实现代码:

<template>
	<view>
		<scroll-view  scroll-x="true" style="height: 500rpx;white-space: nowrap;" scroll-with-animation="true" :scroll-into-view="'scroll'+scrollId">
			<view v-for="(item,index) in 3" style="height: 500rpx;width: 80%;display: inline-block;" :style="{ backgroundColor: colorList[index]}" :id="'scroll'+index">
				{{index}}
			</view>
		</scroll-view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				colorList:["blue","red","yellow"],
				scrollId:1
			}
		},
		methods: {
			
		}
	}
</script>

<style>

</style>

效果图:

uniapp scroll-view基础用法

触底事件       

        在滑动的数据需要懒加载的时候,我们就需要通过用户滑动到底部时触发懒加载方法,通过绑定scrolltolower方法即可实现纵/横触底时触发懒加载方法

实现代码:

<template>
	<view>
		<scroll-view  scroll-x="true" style="height: 500rpx;white-space: nowrap;" @scrolltolower="onReachScollBottom">
			<view v-for="(item,index) in 3" style="height: 500rpx;width: 80%;display: inline-block;" :style="{ backgroundColor: colorList[index]}">
				{{index}}
			</view>
		</scroll-view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				colorList:["blue","red","yellow"],
			}
		},
		methods: {
			onReachScollBottom(){
				uni.showToast({
					title:"触发了触底事件",
					duration:1500,
					icon:"none"
				})
			}
		}
	}
</script>

<style>

</style>

 效果图:

uniapp scroll-view基础用法

 下拉刷新事件

        scroll-view组件也可以满足我们下拉刷新的需求、首先通过设置refresher-enabled为true开启下拉加载、动态绑定refresher-triggered对下拉加载的状态进行控制、绑定refresherrefresh触发下拉刷新事件

实现代码:

<template>
	<view>
		<scroll-view scroll-x="true" style="height: 500rpx;white-space: nowrap;" refresher-enabled="true" :refresher-triggered="refresh" @refresherrefresh="onRefresh">
			<view v-for="(item,index) in 3" style="height: 500rpx;width: 80%;display: inline-block;" :style="{ backgroundColor: colorList[index]}">
				{{index}}
			</view>
		</scroll-view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				colorList:["blue","red","yellow"],
				refresh: false
			}
		},
		methods: {
			onRefresh() {
				this.refresh= true;
				uni.showToast({
					title:"触发了下拉刷新",
					duration:1500,
					icon:"none"
				})
				// 这里不能直接让refresh直接为false,否则可能会发生下拉加载无法复位的情况
				setTimeout(() => {
					this.refresh = false;
				}, 500)
			}
		}
	}
</script>

<style>

</style>

效果图:

uniapp scroll-view基础用法

 

 总结

        以上就是我整理的scroll-view的基础用法、想要了解更多的用法可以前往uniapp scroll-view部分进行了解文章来源地址https://www.toymoban.com/news/detail-409601.html

到了这里,关于uniapp scroll-view基础用法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • uniapp中scroll-view去除滚动条

    uniapp中我们使用scroll-view实现横向或者竖向滑动时,我们会发现横向或者竖向是有一个滚动条的,不是很美观,那么我们就可以尝试去去除一下滚动条(如果采用以下官方方法无法去除时,那么请向官方反馈信息) 不过它默认是false,那就是本身不显示,可以忽略这个方法 代

    2024年02月14日
    浏览(35)
  • Scroll-view的用法(网页和微信小程序)

    微信小程序: 1. 在wxml文件中,使用scroll-view标签包裹需要滚动的内容。 例如,下面的代码将一个view组件包裹在scroll-view中: 2. 设置scroll-view的高度和宽度,以及滚动方向,可以通过设置属性来控制,如scroll-x和scroll-y。 scroll-x和scroll-y分别用于控制scroll-view的横向滚动和纵向滚

    2024年02月03日
    浏览(33)
  • uniapp利用scroll-view实现滚动触底加载

    可以使用以下这种方式来做滚动触底加载,但是官网提示长列表性能不太好,目前也没有更好的方式,暂时先这么处理。 微信小程序官方在scroll-view中也提供了一种触底加载的方式,也没有我写的这么复杂,在uniapp中要改写成vue的方式 @scrolltolower 才能生效,两个方式各有优劣

    2024年02月13日
    浏览(38)
  • 【uniapp】scroll-view 实现自动滚动到最底部

    在做uniapp项目中,有个滚动视图组件 scroll-view ,跟微信小程序里的组件一样的,想要实现自动滚动到最底部,是一件容易忽略的,小事情。 官网uniapp文档上说可以控制滚动条,并没有自动滚动到底部的设置选项,请看布局源代码,如下,大多数可能都是这样写的 🤔 虽然可

    2024年02月11日
    浏览(49)
  • uniapp/微信小程序 scroll-view 设置scroll-x 失效问题解决

    实现一个横向滑动的scrollview,直接给scroll-view设置 scroll-x ,但是并没有实现想药实现横向滑动的效果,先看代码 仔细看了官网后发现有这样一句话, 然而加上也并没有实现,直接说解决方案吧! 使用横向滚动时,不仅仅需要给 scroll-view 添加 white-space: nowrap; 样式,还要给他

    2024年02月16日
    浏览(34)
  • scroll-view在小程序页面里实现滚动,uniapp项目

     要实现红框中的区域进行滚动,scroll-view必须写高  overflow-y:auto 不写也会滚动

    2024年01月17日
    浏览(32)
  • uniapp微信小程序scroll-view滚动scrollLeft不准确

    今天在实现微信小程序的一个横向导航的时候出现了一个问题,就是每次滑到滚动条最右边的时候 scrollLeft的值都不准确 原因:因为每次滚动监听事件都会被调用比较耗费资源系统会默认节流,可以在scroll-view 加一个 throttle=“{{false}}” 关闭节流,如下: 但是这种方法会影响

    2024年02月15日
    浏览(29)
  • [uniapp] scroll-view 简单实现 u-tabbar效果

    效果图 方案 官方scroll-view 进行封装 配合属性 scroll-left Number/String 设置横向滚动条位置 即可 scroll-into-view 属性尝试过,方案较难实现 踩坑 1.scroll-view 横向失败 安装官网的解释 使用竖向滚动时,需要给 scroll-view 一个固定高度,通过 css 设置 height;使用横向滚动时,需要给scr

    2024年02月11日
    浏览(28)
  • uniapp/小程序 scroll-view 中设置flex布局无效问题

    使用 scroll-view 设置横向滑动时在 scroll-view 元素上设置flex布局,没有生效 在 scroll-view 下再加一层 view 包着 scroll-view 的内部元素,并在新增的这层 view 中设置 flex 布局: 👇觉得有帮助的朋友可以支持下作者哦,您的鼓励是我创作的最大动力,如有开发问题可联系作者

    2024年02月15日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包