使用anime.js实现列表滚动轮播

官网:https://animejs.com/

html

复制代码
<div id="slide1">
    <div class="weather-item" v-for="item in weatherList">
        <div><img src="../../images/hdft/position.png" alt="">{{item.body.cityInfo.cityName | city}}</div>
        <div class="left-bd"><span>{{item.body.weather.wc_12h[0] | weather}}</span><span>{{item.body.weather.wd[0] | wind}}</span><span>{{item.body.weather.ws[0]}}m/s</span></div>
        <div><img src="../../images/hdft/temperature.png" alt="">{{Number(item.body.weather.bcmi[0]).toFixed(1)}}℃</div>
    </div>
</div>

css

这只是我这里所使用的样式,实际设置好height即可,没有借鉴意义

复制代码
.con-left{
    width: 3550px;
    overflow: hidden;//父元素overflow hidden
}
.weather-item{
    display: flex;
    height: 90px;//设置height
}
img{
    vertical-align: middle;
    margin-right: 50px;
}
.weather-item div{
    width: 700px;
    text-align: center;
}
.weather-item .left-bd{
    border-left: 8px solid #FFF;
    border-right: 8px solid #FFF;
    display: flex;
    justify-content: space-around;
    padding: 0 80px;
    box-sizing: border-box;
    width: 1000px;
}
.weather-item div{
    width: 700px;
    text-align: center;
}
.weather-item .left-bd{
    border-left: 8px solid #FFF;
    border-right: 8px solid #FFF;
    display: flex;
    justify-content: space-around;
    padding: 0 80px;
    box-sizing: border-box;
    width: 1000px;
}

js

复制代码
//请求完接口以后执行_createTableAnime(数据,dom)
this._createTableAnime(this.weatherList, 'slide1')

_createTableAnime(data, dom) {
    let keyframes = [];
    for (let i = 1; i < data.length; i++) {
        keyframes.push({
            translateY: -90 * i,//这里就是设置的高度
            duration: 1000,//滚动用时
            delay: 2000//停留多久
        });
    }
    if (this.slide1Anime) {
        this.slide1Anime.pause();
        document.getElementById(dom).style.transform = 'translateY(0px)'
        this.slide1Anime = null;
    }
    this.slide1Anime = anime({
        targets: document.getElementById(dom),
        keyframes: keyframes,
        easing: 'linear',
        autoplay: true,
        loop: true,
        endDelay: 2000,
    })
},

这样页面就会正常开始滚动了,而且放大缩小不会受影响,而我们用原生的scrollTop++方式在页面缩小比例较大的时候就不能正常滚动了,所以这算是替代scrollTop的一种方式吧。

相关推荐
如果超人不会飞6 小时前
TinyRobot SuggestionPills紧凑的建议按钮组组件
前端·vue.js
如果超人不会飞6 小时前
TinyRobot Container构建优雅的AI对话容器
前端·vue.js
幸运小圣6 小时前
全面解析 Web 核心性能指标:LCP、INP、CLS 是什么、怎么用、怎么看
前端
如果超人不会飞7 小时前
TinyRobot SuggestionPopover智能建议弹出框组件
前端·vue.js
LiuJun2Son7 小时前
Angular 快速入门:从零搭建你的第一个应用
前端·javascript·angular.js
烬羽7 小时前
从零理解树与二叉树:用 JS 带你手撕遍历和递归
javascript·数据结构
小徐_23337 小时前
Wot UI 2.1.0 发布:ConfigProvider 全局配置能力升级
前端·uni-app
方白羽7 小时前
Vibe Coding 四个核心阶段
android·前端·app
奶油话梅糖7 小时前
浏览器解析 HTML 头部的底层逻辑:从字节流到资源调度
前端·html
YHL7 小时前
🚀从零理解树与二叉树 —— 概念、实现与遍历
前端·javascript·数据结构