<!-- 引入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>
HTML
格式化
支持Emmet,输入 p 后按 Tab键试试吧!
<head> ... </head>
<body>
</body>
@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
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'));