uniapp 文字上下滚动,抽奖效果

1.vue展示页面内容

<template>
	<view>
		<view class="main">
			<view class="subject">抽奖结果</view>
			<view class="body">
				<maoScroll :data="data" :showNum="showNum" :lineHeight="lineHeight" :animationScroll="animationScroll" :animation="animation">
					<template v-slot="{line}">
						<view class="line">{{line.author}} 获得 {{line.subject}}</view>
					</template>
				</maoScroll>
			</view>
		</view>
		<view class="main">
			<view class="subject">滑动配置</view>
			<view class="body">
				<view><text>数据总数:</text><text>{{count}}条(模拟)</text></view>
				<view><text>显示条数:</text><text>{{showNum}}条</text></view>
				<view><text>每行高度:</text><text>{{lineHeight}}rpx</text></view>
				<view><text>滑动时间:</text><text>{{animationScroll}}毫秒</text></view>
				<view><text>滑动间隔:</text><text>{{animation}}毫秒</text></view>
			</view>
		</view>
	</view>
</template>

<script>
	import maoScroll from '@/components/mao-scroll/mao-scroll.vue';
	export default {
		components:{
			maoScroll
		},
		data() {
			return {
				title: 'Hello',
				data: [],
				count: 30,
				showNum: 5,
				lineHeight: 60,
				animationScroll: 800,
				animation: 800,
			}
		},
		onLoad() {
			console.log('init');
			console.log(this.data, '初始化数据 空数据');
			let self = this;
			setTimeout(function(){
				console.log('模拟从网上获取数据 花费了1秒');
				self.createData();
				console.log(self.data, '获取到的数据');
			}, 1000);
		},
		methods: {
			createData: function(){
				for(let i = 1; i <= this.count; i++){
					this.data.push({
						author: 'MaoUI',
						subject: 'OnePlus手机 * ' + i + '部'
					})
				}
			}
		}
	}
</script>

<style>
	.main{margin:30rpx;background-color: #FF6700;border-radius: 10rpx;border:1px solid #FF6700;}
	.main .subject{height: 80rpx;font-size: 36rpx;text-align: center;line-height: 80rpx;color: #fff;}
	.main .body{padding: 20rpx;background-color: #FFFFFF;}
	.main .body .line{height: 60rpx;line-height: 60rpx;}
</style>

2./components/mao-scroll/mao-scroll.vue   文件内容

<template>
	<view>
		<view class="maoScroll-main" :style="'height:'+(lineHeight*showLine)+'rpx;'">
			<view :style="'margin-top:-'+marginTop/2+'rpx;'">
				<view v-for="(item,index) in showdata" :key="'maoScroll'+index" :style="'height:'+lineHeight+'rpx;'">
					<slot :line="item" />
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'maoScroll',
		data() {
			return {
				showdata: [],
				marginTop: 0,
				showLine: 0,
			}
		},
		props:{
			data: {
				type: Array,
				default: []
			},
			showNum: {
				type: Number,
				default: 3,
			},
			lineHeight: {
				type: Number,
				default: 60,
			},
			animationScroll: {
				type: Number,
				default: 500,
			},
			animation: {
				type: Number,
				default: 2000,
			}
		},
		methods: {
			init: function(){
				this.showLine = this.showNum < this.data.length ? this.showNum : this.data.length;
				for(let i = 0; i < this.data.length; i++){
					this.showdata.push(this.data[i]);
				}
				for(let i = 0; i < this.showLine; i++){
					this.showdata.push(this.data[i]);
				}
				setInterval(this.animationFunc, this.animation);
			},
			animationFunc: function(){
				if(this.marginTop >= this.data.length*this.lineHeight){
					this.marginTop = 0;
				}
				let stepTime = this.animationScroll/this.lineHeight;
				
				var step = 0;
				let self = this;
				var index = setInterval(function(){
					self.marginTop = self.marginTop + 1;
					step++;
					if (step >= self.lineHeight) {
						clearInterval(index);
					}
				}, stepTime);
			}
		},
		watch: {
			data(outdata, newdata) {
				this.init();
			}
		}
	}
</script>

<style>
	.maoScroll-main{width: 100%;overflow: hidden;}
</style>

最终效果暂时

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值