【uniapp+vue3】scroll-view实现纵向自动滚动及swiper实现纵向自动滚动

scroll-view本身不支持自动滚动,通过scroll-top属性控制滚动,但是不可以循环滚动

		<scroll-view class="notice-bar" scroll-y="true" ref="scrollViewRef" 
		:scroll-top="data.scrollViewTop"
				scroll-with-animation>
				<view class="carousel-wrapper">
					<view class="notice-item" 
					v-for="(item,index) in data.notices"    		
							:key='index'>
						<image :src="item.icon" alt=""></image>
						<text class="notice-text">{{item.text}}</text>
					</view>
				</view>
			</scroll-view>
<script lang="ts" setup>
import { reactive, getCurrentInstance, onMounted, onUnmounted, ref } from "vue";
const data = reactive({
		notices: [
			{
				icon: "/static/images/common/1.png",
				text: "这是第1条滚动通知"
			},
			{
				icon: "/static/images/common/2.png",
				text: "这是第2条滚动通知"
			},
			{
				icon: "/static/images/common/3.png",
				text: "这是第3条滚动通知"
			},
		],
		timer: null,
		currentIndex: 0,
		scrollViewTop: 0
	})
	onMounted(() => {
		startScroll();
	})
	onUnmounted(() => {
		stopScroll()
	})
		function startScroll() {
		if (data.notices.length <= 1) return
		data.timer = setInterval(() => {
			data.currentIndex++;
			if (data.currentIndex >= data.notices.length) {			
				stopScroll()
			} else {
				data.scrollViewTop = data.currentIndex * 30
			}
		}, 1000);
	}
	function stopScroll() {
		clearInterval(data.timer);
		data.timer = null;
	}
	</script>
	.invite-rules {
			display: flex;
			justify-content: center;
			text-align: center;
			height: 50rpx;
			line-height: 50rpx;		
			.notice-bar {				
				width: 60%;
				height: 100%;
				padding: 0 15rpx;
				margin-top: 25rpx;
				margin-right: 25rpx;
				background-color: rgba(87, 184, 253, .5);
				border-radius: 25rpx;
				white-space: nowrap;
				.carousel-wrapper {				
					display: inline-block;				
					.notice-item {
						display: flex;
						margin-top: 10rpx;					
						image {
							width: 40rpx;
							height: 40rpx;
							border-radius: 50%;
							margin-right: 10rpx;
						}
						.notice-text {
							margin-right: 10rpx;
							font-size: 18rpx;
							color: #FFFFFF;
							line-height: 36rpx;
						}
					}
				}

			}

在这里插入图片描述
使用swiper组件实现很简单,封装下即可用,且支持衔接滑动

<template>
	<view class="">
		<swiper :autoplay="true" :interval="3000" indicator-color="#999" indicator-active-color="#333" circular vertical
			class="notice-bar">
			<swiper-item v-for="(item, index) in list" :key="index" class="notice-item ">
				<image :src="item.imageUrl" class="notice-image"></image>
				<text class="notice-text">{{ item.text }}</text>
			</swiper-item>
		</swiper>
	</view>
</template>

<script lang="ts" setup>
	const props = defineProps({
		list: Array,
	});
</script>

<style lang="scss" scoped>
	.notice-bar {
		display: flex;
		width: 410rpx;
		height: 50rpx;
		line-height: 50rpx;
		padding: 0 15rpx;
		margin-top: 25rpx;
		margin-right: 25rpx;
		background-color: rgba(87, 184, 253, .5);
		border-radius: 25rpx;
		white-space: nowrap;

		.notice-item {
			display: flex;
			// justify-content: center;
			align-items: center;

			.notice-image {
				width: 40rpx;
				height: 40rpx;
				border-radius: 50%;
				margin-right: 10rpx;
			}

			.notice-text {
				margin-right: 10rpx;
				font-size: 18rpx;
				color: #FFFFFF;
				line-height: 36rpx;
			}

		}

	}
</style>

父组件中使用

<view class="invite-rules">
			<scrollSwiper :list="data.notices" />
			<view class="rules" >
		xxxx
			</view>
		</view>
.invite-rules {
			display: flex;
			justify-content: center;
			text-align: center;
			height: 50rpx;
			line-height: 50rpx;
			
		}
<script lang="ts" setup>
import { reactive, getCurrentInstance, onMounted, onUnmounted, ref } from "vue";
import scrollSwiper from './component/scrollSwiper.vue'
const data = reactive({
	
		notices: [
			{
				imageUrl: "/static/images/common/1.png",
				text: "这是1条滚动通知"
			},
			{
				imageUrl: "/static/images/common/1.png",
				text: "这是2条滚动通知"
			},
			{
				imageUrl: "/static/images/common/1.png",
				text: "这是3条滚动通知"
			},
		],
		})
		
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Uniapp中,使用scroll-view实现自动滚动可以通过设置scroll-into-viewscroll-top属性来实现。引用\[1\]中的代码示例展示了如何使用scroll-into-view属性来实现自动滚动。在scroll-view组件中,设置scroll-into-view为一个具有相应id的元素的id值,当该值发生变化时,scroll-view自动滚动到对应id的元素位置。 另外,引用\[2\]中的代码示例展示了如何使用scroll-into-view属性来实现自动滚动到高亮序号的位置。在Vue文件中,设置scroll-into-view为一个具有相应id的元素的id值,当该值发生变化时,scroll-view自动滚动到对应id的元素位置。 如果你想要使用scroll-top属性来实现自动滚动,可以参考引用\[3\]中的代码示例。在scroll-view组件中,设置scroll-top为一个具体的数值,表示滚动条距离顶部的距离。当该值发生变化时,scroll-view自动滚动到对应的位置。 需要注意的是,使用scroll-view实现自动滚动时,需要确保scroll-view具有明确的高度,以便正确计算滚动位置。另外,如果滚动过程需要更加平滑,可以设置scroll-with-animation属性为true,以添加滚动过渡效果。 总结起来,Uniapp中可以通过设置scroll-into-viewscroll-top属性来实现scroll-view自动滚动效果。具体使用哪种方式取决于你的需求和代码实现。 #### 引用[.reference_title] - *1* *3* [uniapp关于scroll-view一直自动滚动到底部的功能](https://blog.csdn.net/u010513497/article/details/120321441)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [uni-app开发小程序,利用scroll-view实现自动滚动至对应数据](https://blog.csdn.net/m0_61387674/article/details/122751501)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值