美文网首页
网站标题栏随页面滑动背景渐变

网站标题栏随页面滑动背景渐变

作者: 清风昙 | 来源:发表于2022-10-30 08:42 被阅读0次

网站标题栏随页面滑动背景渐变实现,效果如下:


7.png
8.png

实现原理根据window.scrollY获取页面滑动距离,再改变标题栏背景透明度opacity,直接上源码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>test</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;        
        }
        .title{
            width: 100%;
            height: 50px;
            position: fixed;
            top: 0;
            left: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            transition:  0.5s;
        }
        .content{
            width: 100%;
            height: 1500px;
            padding-top: 50px;
            background: #D5D5D5;
        }
    </style>
</head>
<body>
   <!--  导航栏 -->
    <div class="title">
        标题栏
    </div>
    <div class="content">
        内容区域
    </div>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script>
        window.addEventListener('scroll',function(){
            let scrollTop = window.scrollY
            let opacity = Math.abs(Math.round(scrollTop)) / 50;
            $(".title").css({background: `rgba(47, 152, 234, ${opacity})`})
        })
    </script>
</body>
</html>

相关文章

网友评论

      本文标题:网站标题栏随页面滑动背景渐变

      本文链接:https://www.haomeiwen.com/subject/tosctdtx.html