当前位置:首页 > 前端

uniapp无缝滚动核心代码

Z先生6年前 (2020-02-18)前端900

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


分享给朋友:

相关文章

字体图标的妙用

什么是字体图标对于前端的小伙伴来说,字体应该是再熟悉不过的东西了,对于文字我们可以通过css指定文字对应的字体,字号大小,颜色。得益于css3,我们还可以在css中引入服务端字体来实现自定义字体。而字…

VUE打包时from UglifyJs Unexpected token: punc (()错误解决方案

归其原因是因为缺少ES2015的语法解析。因此进行如下2步操作即可解决问题:1、安装babel-preset-es2015npm install babel-preset-es20…

npm 更新模块

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

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

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

微信html链接小程序原始写法(暂未测试)

微信html链接小程序原始写法(暂未测试)

无意间发现了微信的这个链接,这几天比较忙,暂时还没有测试,等空了再测试一下。…

js获取指定文件的路径

获取指定js文件的路径var src = '/static/js/demo.js'; var path = src.sub…

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。