好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

小程序实现跑马灯效果

本文实例为大家分享了小程序实现跑马灯效果的具体代码,供大家参考,具体内容如下

先看效果图

实现步骤:

index.wxml文件

<!-- 跑马灯效果 -->
<view class="example">
? <view class="marquee_box">
? ? <view class="marquee_text"?
? ? ?? ? ?style="{{orientation}}:{{marqueeDistance}}px;font-size: {{size}}px;">
? ? ? <image src="{{adUrl}}" class='ad-image' />{{text}}
? ? </view>
? </view>
</view>

wxss文件

/* 跑马灯效果 */
.example {
? display: block;
? width: 100%;
? height: 70rpx;
? background-color: #f2f2f2;
? line-height: 70rpx;
}
.marquee_box {
? width: 100%;
? position: relative;
}
.marquee_text {
? white-space: nowrap;
? position: absolute;
? top: 0;
? display: flex;
? flex-direction: row;
}
.ad-image {
? width: 40rpx;
? height: 40rpx;
? margin-right: 10rpx;
? margin-top: 15rpx;
}

js文件

// pages/home/home.js
var app = getApp()
Page({
? data: {
? ? //跑马灯
? ? text: '618淘甄貨,一个可以省钱的购物平台',
? ? marqueePace: 1,//滚动速度
? ? marqueeDistance: 0,//初始滚动距离
? ? size: 14,
? ? orientation: 'left',//滚动方向
? ? intervals: 20, // 时间间隔
? ? adUrl: 'images/detail/like.jpeg'
? },
? onShow: function () {
? ? // 页面显示
? ? var that = this;
? ? var length = that.data.text.length * that.data.size;//文字长度
? ? var windowWidth = wx.getSystemInfoSync().windowWidth;// 屏幕宽度
? ? that.setData({
? ? ? length: length,
? ? ? windowWidth: windowWidth,
? ? });
? ? that.runMarquee();// 水平一行字滚动完了再按照原来的方向滚动
? },
? runMarquee: function () {
? ? var that = this;
? ? var interval = setInterval(function () {
? ? ? //文字一直移动到末端
? ? ? if (-that.data.marqueeDistance < that.data.length) {
? ? ? ? that.setData({
? ? ? ? ? marqueeDistance: that.data.marqueeDistance - that.data.marqueePace,
? ? ? ? });
? ? ? } else {
? ? ? ? clearInterval(interval);
? ? ? ? that.setData({
? ? ? ? ? marqueeDistance: that.data.windowWidth
? ? ? ? });
? ? ? ? that.runMarquee();
? ? ? }
? ? }, that.data.intervals);
? }
})?

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

查看更多关于小程序实现跑马灯效果的详细内容...

  阅读:32次