【问题标题】:CSS - Animation whole ul and not just first row when new item entersCSS - 动画整个 ul 而不仅仅是新项目进入时的第一行
【发布时间】:2018-08-29 19:30:52
【问题描述】:

看看this simple JsFiddle I made,它正在为进入列表的新项目设置动画。

但是,动画只影响第一行,而不是所有行。 我使颜色随机化,因此当插入新项目时,您实际上可以看到其他行上的“跳跃”。

有没有办法让它只用 CSS 影响所有行?

@keyframes enter {
  0% {
    transform: translateX(-100%);
    margin-left: calc(var(--w) * -1);
  }
  100% {
    transform: translateX(0px);
    margin-left: 0
  }
}

这是我的动画,这是我的列表项:

.slidepush li {
  --w: 50px;
  width: var(--w);
  height: 50px;
  background-color: red;
  vertical-align: top;


  border: 0;
  padding: 0;
  margin: 0;
  display: inline-block;
}

.enter {
  animation: enter 1s;
}

创建了一个管理ul并实现add的简单类:

class SlidePush {

    constructor(ulElement) {
    this.element = ulElement;
  }

  add(item) {
    this.element.prepend(item.addClass('enter'));
  }
}

const sp = new SlidePush($(".slidepush"));

setInterval(() => {//
  var colors = ['red', 'blue', 'green', 'black'];
  var color = colors[Math.floor(Math.random() * colors.length)];
  sp.add($("<li style='background-color:" + color +" !important;'></li>"));
}, 1000);

【问题讨论】:

    标签: javascript css css-animations


    【解决方案1】:

    您可以考虑nth-child 添加动画,因此您始终添加到每行的第一个动画。您还需要在动画完成后删除动画,以便在到达下一行时再次将其添加到同一元素。

    class SlidePush {
    
    	constructor(ulElement) {
      	this.element = ulElement;
      }
      
      add(item) {
      	this.element.prepend(item);
        $('.slidepush li:nth-child(4n+1)').addClass('enter');
        setTimeout(function(){ $('.slidepush li:nth-child(4n+1)').removeClass('enter') }, 1000);
      }
    }
    
    const sp = new SlidePush($(".slidepush"));
    
    setInterval(() => {//
    var colors = ['red', 'blue', 'green', 'black'];
    var color = colors[Math.floor(Math.random() * colors.length)];
    sp.add($("<li style='background-color:" + color +" !important;'></li>"));
    }, 1200);
    div {
    --w: 200px;
      max-width: var(--w);
    }
    
    
    .slidepush {
      list-style: none;
      border: 0;
      padding: 0;
      margin: 0;
      display: table;
      }
    
    .slidepush li {
      --w: 50px;
      width: var(--w);
      height: 50px;
      background-color: red;
      vertical-align: top;
    
      
      border: 0;
      padding: 0;
      margin: 0;
      display: inline-block;
    }
          
    .enter {
      animation: enter 1s;
      position:relative;
      z-index:-1;
    }
          
    @keyframes enter {
      0% {
        transform: translateX(-100%);
        margin-left: calc(var(--w) * -1);
      }
      100% {
        transform: translateX(0px);
        margin-left: 0;
      }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div>
      <ul class="slidepush">
    
      </ul>
    </div>

    【讨论】:

    • 这样做的问题是它有随机跳跃,不平滑,并且只适用于固定宽度(例如200px)。
    • @Jony 好吧,我尝试了很多方法,但并不容易..你对动画有什么想法吗?如果您可以做一个静态示例来说明您希望它的外观,它可能会有所帮助
    【解决方案2】:

    你能不能试试下面的代码。

    class SlidePush {
    
      constructor(ulElement) {
        this.element = ulElement;
      }
    
      add(item) {
        $('.slidepush li').removeClass('enter');
        $('.slidepush li').removeClass('push');
        $('.slidepush li:nth-child(4n+1)').addClass('push');
        this.element.prepend(item.addClass('enter'));
      }
    }
    
    const sp = new SlidePush($(".slidepush"));
    
    setInterval(() => { //
      var colors = ['red', 'blue', 'green', 'black'];
      var color = colors[Math.floor(Math.random() * colors.length)];
      sp.add($("<li style='background-color:" + color + " !important;'></li>"));
    }, 1000);
    div {
      --w: 200px;
      max-width: var(--w);
    }
    
    .slidepush {
      list-style: none;
      border: 0;
      padding: 0;
      margin: 0;
      display: table;
    }
    
    .slidepush li {
      --w: 50px;
      width: var(--w);
      height: 50px;
      background-color: red;
      vertical-align: top;
      border: 0;
      padding: 0;
      margin: 0;
      display: inline-block;
    }
    
    .enter {
      animation-name: enter;
      animation-duration: 1s;
      animation-fill-mode: forwards;
    }
    
    .push {
      animation-name: push;
      animation-duration: 1s;
      animation-fill-mode: forwards;
    }
    
    @keyframes push {
      0% {
        margin-left: calc(var(--w) * -1);
      }
      100% {
        margin-left: 0
      }
    }
    
    @keyframes enter {
      0% {
        transform: translateX(-100%);
      }
      100% {
        transform: translateX(0px);
      }
    }
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
    <div>
      <ul class="slidepush">
    
      </ul>
    </div>

    【讨论】:

    • 再一次,它并不流畅,每次插入一个项目时都会出现一点错误
    猜你喜欢
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    相关资源
    最近更新 更多