公告左右滚动

效果:文字从右往左无限滚动
请添加图片描述

/*
*滚动公告
*/
<template>
	<view class="t-notice-bar">
		<view class="t-direction-row">
			<view class="t-notice-box" id="t-notice-box">
				<view
					class="t-notice-content"
					id="t-notice-content"
					:style="{
						animationDuration: animationDuration,
						animationPlayState: animationPlayState,
					}"
				>
					<text class="t-notice-text">{{showText}}</text>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
export default {
	props: {
		// 显示的内容,数组
		list: {
			type: Array,
			default() {
				return [];
			}
		},
		// 是否自动播放
		autoplay: {
			type: Boolean,
			default: true
		},
		// 水平滚动时的滚动速度,即每秒滚动多少rpx,这有利于控制文字无论多少时,都能有一个恒定的速度
		speed: {
			type: [Number, String],
			default: 140
		}
	},
	data() {
		return {
			textWidth: 0, // 滚动的文字宽度
			animationDuration: '10s', // 动画执行时间
			animationPlayState: 'paused', // 动画的开始和结束执行
			showText: '' // 显示的文本
		};
	},
	watch: {
		list: {
			immediate: true,
			handler(val) {
				this.showText = val.join(',');
				this.$nextTick(() => {
					this.initSize();
				});
			}
		},
		speed(val) {
			this.initSize();
		}
	},
	mounted() {
		this.$nextTick(() => {
			this.initSize();
		});
	},
	methods: {
		initSize() {
			let query = [],
				textWidth = 0;
			let textQuery = new Promise((resolve, reject) => {
				uni.createSelectorQuery()
					.in(this)
					.select(`#t-notice-content`)
					.boundingClientRect()
					.exec(ret => {
						this.textWidth = ret[0].width;
						resolve();
					});
			});
			query.push(textQuery);
			Promise.all(query).then(() => {
				this.animationDuration = `${this.textWidth / uni.upx2px(this.speed)}s`;
				this.animationPlayState = 'paused';
				setTimeout(() => {
					if(this.autoplay) this.animationPlayState = 'running';
				}, 10);
			});
		},
	}
};
</script>

<style  scoped>

.t-notice-bar {
	overflow: hidden;
	color:#fff;
}

.t-direction-row {
	display: flex;
	flex-direction: row;
	align-items: center;
	justify-content: space-between;
}


.t-notice-box {
	flex: 1;
	display: flex;
	flex-direction: row;
	overflow: hidden;
}

.t-notice-content {
	animation: loop-animation 10s linear infinite both;
	text-align: right;
	// 这一句很重要,为了能让滚动左右连接起来
	padding-left: 100%;
	display: flex;
	flex-direction: row;
	flex-wrap: nowrap;
	
}

.t-notice-text {
	font-size: 30rpx;
	word-break: keep-all;
	white-space: nowrap
}

@keyframes loop-animation {
	0% {
		transform: translate3d(0, 0, 0);
	}

	100% {
		transform: translate3d(-100%, 0, 0);
	}
}
</style>

引入使用

<rolling style="flex: 1;" :list="rollinglist"></rolling>

//传个数组
import rolling from '@/components/rolling /rolling '
rollinglist:['楼盘的新闻资讯 电话咨询:0735-3886666'],//公告
可以使用jQuery和CSS来实现公告文字左右滚动。这里提供一个简单的示例代码: HTML: ```html <div class="notice"> <ul> <li>公告1</li> <li>公告2</li> <li>公告3</li> <li>公告4</li> <li>公告5</li> </ul> </div> ``` CSS: ```css .notice { width: 200px; height: 20px; overflow: hidden; } .notice ul { list-style: none; margin: 0; padding: 0; } .notice li { float: left; margin-right: 20px; } ``` JavaScript: ```javascript $(function() { var noticeWidth = $(".notice").width(); var ulWidth = 0; $(".notice li").each(function() { ulWidth += $(this).outerWidth(); }); $(".notice ul").width(ulWidth); function scrollNotice() { var scrollLeft = $(".notice ul").position().left - 1; if (scrollLeft < -ulWidth + noticeWidth) { scrollLeft += ulWidth; } $(".notice ul").css("left", scrollLeft); requestAnimationFrame(scrollNotice); } scrollNotice(); }); ``` 解释一下代码的实现过程: - 首先设置公告容器 `.notice` 的宽度和高度,并设置 `overflow: hidden`,这样公告文字就不会超出容器范围而显示滚动条。 - 然后设置公告列表 `.notice ul` 的样式,将列表项的样式设置为 `float: left`,这样列表项就可以水平排列。 - 在 JavaScript 中,首先计算出公告列表的总宽度,然后将公告列表的宽度设置为总宽度。这样公告列表的宽度就可以自适应文字长度。 - 接着使用 `requestAnimationFrame` 方法循环执行 `scrollNotice` 函数,实现公告文字的左右滚动效果。在 `scrollNotice` 函数中,先获取公告列表的左侧位置,然后将其减去 1,实现向左滚动的效果。当公告列表左侧位置小于容器宽度减去列表总宽度时,将其重置为 0,实现循环滚动的效果。最后使用 `css` 方法将公告列表的左侧位置设置为滚动后的位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值