视觉艺术页面特效

CSS+HTML<倾斜滚动效果>

2019-11-25  本文已影响0人  誰在花里胡哨
效果图:
roll.gif
此篇文章有用到 window.requestAnimationFrame,详情可参考https://www.jianshu.com/p/f6d933670617
代码如下:
<!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>Document</title>
    <style>
        body,
        html {
            height: 100%;
        }

        body {
            width: 100%;
            margin: 0;
        }

        .box {
            width: 300px;
            margin: 0 auto;
        }

        .box section {
            width: 100%;
            height: 200px;
            margin: 50px 0;
            background: rgb(224, 224, 224);
            text-align: center;
            line-height: 200px;
            font-size: 100px;
            font-weight: bold;
        }
    </style>
</head>

<body>
    <div class="box">
        <section>▲</section>
        <section>△</section>
        <section>▲</section>
        <section>△</section>
        <section>▲</section>
        <section>△</section>
    </div>
</body>
<script>
    var box = document.querySelector('.box');
    var currentPos = window.pageYOffset; //滚动条距离顶部高度
    var update = function () {
        var newPos = window.pageYOffset;
        var diff = newPos - currentPos; //判断是向下滚动还是向上滚动
        var speed = diff * 0.2; //倾斜幅度
        box.style.transform = 'skewY(' + speed + 'deg)'; //倾斜角度
        currentPos = newPos;
        window.requestAnimationFrame(update);
    };
    update();
</script>

</html>
上一篇 下一篇

猜你喜欢

热点阅读