js实现抖音排名动画效果

代码语言:html

所属分类:布局界面

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>排行动画</title>
  <style>
    html, body, div, ul, li, img, span {
      padding: 0;
      margin: 0;
    }
 
    ul, li {
      list-style: none
    }
 
    span {
      display: inline-block;
    }
 
    img {
      vertical-align: top;
    }
 
    #box {
      padding: 20px;
      height: 700px;
    }
 
    #box ul {
      padding-left: 20px;
      position: relative;
    }
 
    #box li {
      margin-left: 60px;
      height: 28px;
      box-sizing: border-box;
      position: absolute;
      line-height: 28px;
      transition: width ease 1s, transform ease 1s, opacity ease 1s;
      opacity: 0;
      background: #ddd;
      border-radius: 20px;
      box-shadow: inset -5px 0px 25px rgba(0,0,0,.4);
    }
 
    #box li .name {
      position: absolute;
      left: -60px;
      white-space: nowrap;
    }
 
    #box li .count {
      margin-left: 15px;
      position: absolute;
      left: 100%;
      font-size: 18px;
      letter-spacing: 1px;
      color: #fff;
      font-style: italic;
      text-shadow: 1px 0 2px #000, 0 1px 2px #000;
    }
  </style>
</head>
<body>
<div id="box">
  <h3>销量排行</h3>
  <ul id="list"></ul>
</div>
<script>
  class Animate {
    constructor () {
      this._baseData;
      this.htmlDOMData = [];
    }
 
    init() {
      // 初始化数据
      this._initArr();
 
      // 创建动画
      this._createAnimate();
 
      // 新增事件
      this._addNewAnimate();
    }
 
    _initArr() {
      let data = [];
 
      for(let i = 0; i < 10; i++) {
        let _obj = {
          id: `id${i+1}`,
          name: `第${i+1}位`,
          count: parseInt(Math.random()*800)
        };
        data.push(_obj)
      }
      this._baseData = data;
    }
 
    _createAnimate() {
      // 如果是第一次先创建DOM元素,不是就开始动画
      for(let i = 0; i < 5; i++) {
        if(i === 0) {
          this._firstCreate();
        } else {
          setInterval(() => {
            this._beginAnimate()
          }, i * 3000)
        }
      }
    }
 
    _firstCreate() {
      this._first = false;
      let bar = document.querySelector('#list');
 
      this._baseData.sort((l, n) => n.count - l.count);
      this._baseData.forEach((_data, index) => {
        let li = this._createLiDom(_data, index);
        bar.........完整代码请登录后点击上方下载按钮下载查看

网友评论0