继上篇无限滚动列表案例(使用css animation 兼容IE11)

背景:

上篇博文我们讲到了一个dom API(animate),虽然很好用,但它在IE11上却不支持,今天就简单讲一下,如何在IE11中使用动画,其实可以理解为换种方式实现,这里我会总结列出我遇到的一些坑

假如在实际项目中要支持IE11浏览器,但该animate属性又不兼容,此时我们可以改写成用css 的animation API 来实现动画…

模拟需求:我们还是实现一个无限滚动列表为demo

DEMO

// js文件
// index.jsx

import React, { createRef, Component } from 'react';
import './index.css';

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]

class RollList extends Component {
  constructor(props) {
    super(props)
    this.ref1 = createRef();
    this.ref2 = createRef();
  }


  componentDidMount() {
    const height = this.ref1.current.clientHeight;
   	this.ref1.current.style.animation = `animateName1 ${60* height}ms infinite linear`
    this.ref2.current.style.animation = `animateName1 ${60* height}ms infinite linear`
  }

  onMouseEnter = () => {
     this.ref1.current.style.animationPlayState = 'paused'
     this.ref2.current.style.animationPlayState = 'paused'
  }

  onMouseLeave = () => {
   	this.ref1.current.style.animationPlayState = ''
    this.ref2.current.style.animationPlayState = ''
  }


  render() {
    return (
      <div className='boxOut' onMouseEnter ={this.onMouseEnter } onMouseLeave ={this.onMouseLeave }>
        <div className='box ' ref={this.ref1}>
          {arr.map(item => (
            <div className='items'>{item}</div>
          ))}
        </div>
        <div className='box' ref={this.ref2}>
          {arr.map(item => (
            <div className='items'>{item}</div>
          ))}
        </div>
      </div>
    )
  }
}

export default RollList;
========================================================================================================
// index.css
.boxOut {
  width: 600px;
  height: 300px;
  background: yellowgreen;
  overflow: hidden;
  position: relative;
}

.items {
  width: 600px;
  height: 40px;
}

.box {
  position: absolute;
}

// 动画1
@keyframes animateName1 {
	from{
		transform:translateY(0)
	}
	to{
		transform:translateY(-100%)
	}
}
// 动画2
@keyframes animateName2 {
	from{
		transform:translateY(100%)
	}
	to{
		transform:translateY(0)
	}
}

代码解析
1、这里应用到了animationPlayState 属性来控制元素的动画是否暂停和播放,paused为暂停,“”为执行动画
2、还有一个点就是animation属性我我们通过js动态设置上去的,而不是在css中直接写死,这是因为这个animation属性在IE10-11浏览器中一旦被设置赋值,就不能再改变,所以我们就不在css中初始设置了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ronychen’s blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值