<view class="right-left-scroll">
		<view 
			class="content" 
			:style="{'animation': `noticeAnimation ${time}s linear 1s infinite`}"
		>
			{{notice}}
		</view>
	</view>
export default {
		data() {
			return {
				notice: '空前折扣力度,专享福利大家快来抢利大家快快来抢购下dfdfdfddfdfdfd专享福利大家快来抢利大家快快来抢购下专享福利大家快来抢',
                time: 0,
			}
		},
		mounted() {
			let scroll_width = 0;
			const query = uni.createSelectorQuery().in(this);
			query.select('.right-left-scroll').boundingClientRect(data => {
				scroll_width = (data.width).toFixed(2);
			}).exec();
			
			query.select('.right-left-scroll .content').boundingClientRect(data => {
				if(data.width - scroll_width > 0) {
					this.time = (9 * data.width / scroll_width).toFixed(2);
				}
			}).exec();
		}
	}}
	}
.right-left-scroll {
		white-space: nowrap;
		overflow: hidden;
		
		.content {
			display: inline-block;
		}
	}
注意:只有在app.vue里面定义的keyframes才能在style里面使用
app.vue文件style中:

@keyframes noticeAnimation {
	from {
		transform: translateX(100rpx);
	}
	to {
		// transform: translateX(calc(-100% + 150px));
		transform: translateX(-100%);
	}
}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐