点击查看html编辑器说明文档

使用React制作的CSS 3D鼠标追踪edit icon

|
|
Fork(复制)
|
|
作者:
穿越者X57

👉 新版编辑器已上线,点击进行体验吧!

BUG反馈
嵌入
设置
下载
HTML
格式化
支持Emmet,输入 p 后按 Tab键试试吧!
<head> ...
展开
</head>
<body>
            
            <!-- 引入React -->
<script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/react/16.14.0/umd/react.production.min.js"></script>
<script src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-y/react-dom/16.14.0/umd/react-dom.production.min.js" type="application/javascript"></script>

<div id="app"></div>
        
</body>
Sass
格式化
            
            @import url('https://fonts.googleapis.cn/css?family=Montserrat')

body
  box-sizing: border-box
  background: #f1faee
  font-family: 'Montserrat', sans-serif

#app
  width: 100vw
  height: 100vh
  
.wrapper
  position: absolute
  top: 50%
  left: 50%
  transform: translate(-50%, -50%) perspective(600px) rotateY(20deg) rotateX(10deg)
  background: #1d3557
  transform-style: preserve-3d
  width: 200px
  height: 300px
  border-radius: 10px

  & .shape
    background: #457b9d
    border-radius: 10px
    width: 200px
    height: 300px
    transform: translateZ(50px) scale(.85)
    opacity: 0.8

  & .shape2
    position: absolute
    top: 0
    left: 0
    z-index: 2
    width: 250px
    height: 350px
    transform: translateZ(150px) translateX(-27px) translateY(-26px) scale(.55)
    border: 3px solid rgba(255, 255, 255, 1)
    background: #a8dadc
    opacity: 0.3
    border-radius: 8px
  & .shape3
    position: absolute
    top: 0
    left: 0
    z-index: 2
    width: 150px
    height: 250px
    background: pink
    opacity: 0.5
    border-radius: 8px
    border: 3px solid hotpink
    transform: translateZ(200px) translateX(25px) translateY(25px) scale(.65)


  & p
    text-align: center
        
JS
格式化
            
            class App extends React.Component {
  constructor(props) {
    super(props);
    this.init();
  }
  init() {
    this.state = {
      offsetX: '',
      offsetY: '',
      friction: 1 / 32
    }
    this.mouseMove = this.mouseMove.bind(this);
  }
  componentDidMount() {
    document.addEventListener('mousemove', this.mouseMove);
  }
  componentWillUnmount() {
    document.removeEventListener('mousemove', this.mouseMove);
  }
  
  mouseMove(e) {
    let followX = (window.innerWidth / 2 - e.clientX);
    let followY = (window.innerHeight / 2 - e.clientY);

    let x = 0,
        y = 0;
    x +=( (-followX - x) * this.state.friction);
    y += (followY - y) * this.state.friction;
    this.setState({
      offsetX: x,
      offsetY: y
    });
  }
  render() {
    let offset = {
      transform: `translate(-50%, -50%) perspective(600px)
                  rotateY(${this.state.offsetX}deg)
                  rotateX(${this.state.offsetY}deg)`
                 }

    return (
      <div className='wrapper' style={offset}>
        <div className="shape">
        </div>
        <div className="shape2">
        </div>
        <div className="shape3">
        </div>
        <p>移动你的鼠标或手指</p>
      </div>
    )
  }

}
ReactDOM.render(<App />, document.getElementById('app'));
        
预览
控制台