CSS HTML jQuery 实现哔哩哔哩头部移动冬.日夜时差图

CSS HTML jQuery 实现哔哩哔哩头部移动冬.日夜时差图

CSS HTML jQuery 实现哔哩哔哩头部移动冬.日夜时差图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
HTML部分

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./css/bz.css">
    <script src="./js/jquery-3.5.1.js"></script>
</head>
<body>
    <header>
        <div class="view">
            <img src="https://assets.codepen.io/2002878/bilibili-winter-view-1.jpg" class="morning" alt="">
            <img src="https://assets.codepen.io/2002878/bilibili-winter-view-2.jpg" class="afternoon" alt="">
            <video autoplay loop muted class="evening">
            <source src="https://assets.codepen.io/2002878/bilibili-winter-view-3.webm" type="video/webm" />
          </video>
            <img src="https://assets.codepen.io/2002878/bilibili-winter-view-3-snow.png" class="window-cover" alt="">
        </div>
        <div class="tree">
            <img src="https://assets.codepen.io/2002878/bilibili-winter-tree-1.png" class="morning" alt="">
            <img src="https://assets.codepen.io/2002878/bilibili-winter-tree-2.png" class="afternoon" alt="">
            <img src="https://assets.codepen.io/2002878/bilibili-winter-tree-3.png" class="evening" alt="">
        </div>
    </header>
    <script src="./js/bz.js"></script>
</body>

</html>

CSS部分

        body {
            margin: 0;
            padding: 0;
        }
        
        header {
            height: 160px;
            position: relative;
            overflow: hidden;
            --percentage: 0.5;
        }
        
        header .view,
        header .tree {
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        header img,
        header video {
            position: absolute;
            display: block;
            width: 120%;
            height: 100%;
            object-fit: cover;
        }
        
        header .morning {
            z-index: 20;
            opacity: calc(1 - (var(--percentage) - 0.25) / 0.25);
        }
        
        header .afternoon {
            z-index: 10;
            opacity: calc(1 - (var(--percentage) - 0.5) / 0.5);
        }
        
        header .view {
            transform: translateX(calc(var(--percentage) * 100px));
        }
        
        header .tree {
            transform: translateX(calc(var(--percentage) * 50px));
            filter: blur(3px);
        }
        
        header .morning .view,
        header .morning .tree,
        header .morning .morning,
        header .morning .afternoon {
            transition: none;
        }
        
        header .window-cover {
            opacity: calc((var(--percentage)-0.9)/0.1);
            /*calc( )用于动态计算长度值
            可以用在任何长度,数值,时间,角度,频率等处运算符两边需要有空格
            使用 * / 运算符时,必须保证有一个值为数值类型*/
        }

jQuery部分

$(function() {

    let startingPoint;
    $("header").on("mouseout", (e) => {
        $("header").css("--percentage", 0.5)
    });
    $("header").on("mouseenter", (e) => {
        startingPoint = e.clientX

        $("header").addClass("moving");
    })
    $("header").on("mousemove", (e) => {
        let percentage = (e.clientX - startingPoint) / window.outerWidth + 0.5;
        $("header").removeClass("moving");

        console.log(percentage);
        $("header").css("--percentage", percentage)
        $("header").removeClass("moving");
    })

})
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值