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

uniapp无缝滚动核心代码

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

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();
}


分享给朋友:

相关文章

实现 OpenSearch(Tab to Search)功能

在网站上添加了 OpenSearch 功能以后,IE 7.0 和 Firefox 2.0 以上的浏览器就能够在自带的搜索栏里面添加这个网站的搜索功能了。第一:原始网站支持GET传递参数搜索:第二:编写...

HTML5桌面通知开启代码片段

<script type="text/javascript"> function showmsg() {   &n...

【原创】api接口模拟器(NodeJS版)

最近遇到一个项目,内网的APP,后端数据和前段开发属于异地,没法直接做接口数据测试。因此用NodeJs简单写了个API模拟器,只要将响应的数据放在对应目录下json.json文件中即可。请求地址全静态...

npm 更新模块

npm update只能按照package.json中标注的版本号进行更新,升级后不会修改package.json中的版本号,需要自己手动修改,比较麻烦。npm-check-updates 升级插件升...

uniapp开发小程序,小程序二维传参与H5、分享页面参数兼容的处理方案。

首先小程序获取二维码接口有三个,2个限制数量,一个不限制数量,但是限制生成频率(5000次/分钟)和参数长度(32位),我使用的是不限数量的接口:getUnlimited,传递的参数为:invite=...

发表评论

访客

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