当前位置:首页 > 前端 > 正文内容

uniapp无缝滚动核心代码

Z先生5年前 (2020-02-18)前端800

1、页面代码核心代码

<scroll-view class="scoll-warp">
    <view id="scoll-wrapper" class="scoll-wrapper" :animation="animation.animationData">
        <view v-for="(item, index) in  listData" :key="index" class="floor-item gaokong-item">
            <image :src="item.thumb" mode="aspectFill"></image>
            <text class="title clamp">{{ item.name }}</text>
        </view>
    </view>
</scroll-view>

2、css核心样式

.scoll-warp {
    white-space: nowrap;
}
.scoll-wrapper {
    display: flex;
    align-items: flex-start;
}
.floor-item {
    width: 150upx;
    margin-right: 20upx;
    font-size: 26upx;
    color: #333;
    line-height: 1.8;
    image {
        width: 150upx;
        height: 150upx;
        border-radius: 6upx;
    }
}

3、系统变量

data() {
    return {
        listData: [], //数据
        animation: {
            scrollWidth: 0,
            width: 0,
            itemWidth: 0,
            animationData: {}
        }
    };
}

4、动画核心

readyScroll() {
    let that = this,
    query = uni.createSelectorQuery().in(this);
    this.$nextTick(function() {
            //不加延时,取得不到数据
        setTimeout(function() {
            query
                .selectAll('.gaokong-item')
                .boundingClientRect(res => {
                    if (res.length > 0) {
                        //宽度减去屏幕宽度that.systmInfo.windowWidth,that.systmInfo.windowWidth需要实际计算
                        that.animation.width = res[0].right * res.length - that.systmInfo.windowWidth;
                        that.animation.itemWidth = res[0].right;
                        console.log(that.animation);
                        if (that.animation.width > 10) {
                            that.doScroll();
                        }
                    }
                })
                .exec();
        });
    }, 500);
},
doScroll() {
    let that = this,
    speed = 3000;
    let animation = uni.createAnimation({
        timingFunction: 'linear',
        delay: 0
    });
    let width = 0 - that.animation.width;
    setInterval(() => {
        if (that.animation.scrollWidth <= width) {
            animation.translateX(0).step();
            that.animation.scrollWidth = 0;
            that.animation.animationData = animation.export();
        } else {
                that.animation.scrollWidth = that.animation.scrollWidth - that.animation.itemWidth;
                animation.translateX(that.animation.scrollWidth).step();
                that.animation.animationData = animation.export();
        }
    }, speed);
}

5、调用

onReady() {
    this.readyScroll();
}


分享给朋友:

相关文章

小程序二维码传参处理&UNIAPP H5传参兼容处理

首先小程序生成二维码,我这里调用的接口是:getUnlimited,我传递的参数是:invite=10001接着就是小程序中处理,如果直接扫二维码在App.vue的onLaunch中,使用decode...

ColorUI色系

ColorUI色系

                          ...

npm删除掉所有模块并重新安装的方法

1、删除目录:node_modules2、运行清理命令npm cache clean -f3、安装npm install...

记vue/uniapp本地调试以及部署代理服务器设置哪些事

接触vue差不多也一年有余,很多概念性的东西都不懂,逐步已经习惯了npm来安装,之前其他同事做的一些项目,npm自己拿过来也踩了很多坑,比如换个环境再npm就跑不起来了等等之类的,遇到最多的就是scs...

一个很漂亮的404页面

一个很漂亮的404页面

下载地址:404.zip...

截至2020年6月PC显示器分辨率占比

截至2020年6月PC显示器分辨率占比

做一个项目汇报,需要用到这个数据;首先想到的数据源是cnzz的统计,去看了半天,没有抽这个数据层面出来。然后又去找51la,很可惜没有类似的大数据报告;又去百度统计里面,看到有这个数据,但是比较抽象,...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。
请先 登录 再评论,若不是会员请先 注册