哔哩哔哩冬季头图小案例

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        header{
            height: 160px;
            width: 100%;
            position: relative;
            overflow: hidden;
            --percentage:0.5;
        }
        header .view,header .tree{
            width: 100%;
            height: 160px;
            position: absolute;
            top: 0;
            left: 0;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        img,video{
            height: 100%;
            width: 120%;
            position: absolute;
            object-fit: cover;/* 这里不加会出现白边是因为evening的宽度不够达不到120%,要加object填满 */
        }
        .morning{
            z-index: 20;
            opacity: calc(1 - (var(--percentage) - 0.25) / 0.25);
        }
        .afternoon{
            z-index: 10;
            opacity: calc(1 - (var(--percentage) - 0.5) / 0.5);
        }
        .view{
            transform: translate(calc(var(--percentage) * 100px));
        }
        .tree{
            transform: translate(calc(var(--percentage) * 50px));
            filter: blur(3px);
        }
        header .tree,
        header .view,
        header .morning,
        header .afternoon{
            /* 中间不加空格无效果 */
            transition:  .2s all ease-in;
           /*  transition: .2s all ease-in; */

        }
        
        header.moving .tree,
        header.moving .view,
        header.moving .morning,
        header.moving .afternoon{
            /* header与moving之间不能加空格? */
            transition: none;
        }
        header .window-cover{
            opacity: calc((var(--percentage) - 0.9) / 0.1);
        }
    </style>
</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>
        var header=document.querySelector('header');
        var startpoint;
        header.addEventListener('mousemove',(e)=>{
            let percentage=(e.clientX - startpoint) / window.outerWidth + 0.5;
            header.style.setProperty('--percentage',percentage);
        })
        header.addEventListener('mouseenter',(e)=>{
            header.classList.toggle('moving')
            startpoint=e.clientX;
        })
        header.addEventListener('mouseout',()=>{
        header.classList.toggle('moving')
        header.style.setProperty('--percentage',0.5)
        })
    </script>
</body>
</html>

效果:
不知道为啥总是上传失败,只能自己打开看了
解释:
1.有关选择器中加空格导致样式无效的问题:
header .view中header和.view具有上下关系,.view是header的后代。(有空格,其实就相当于A B{})
2.setProperty()是设置内联样式
3.var、let、const的区别
4.css中var()可以获取自定义属性值,不需要加引号。相关链接:css的var()函数
5.filter:blur()值越大产生更多的模糊
6.img父元素无宽高只设置width百分比,height也会变化
7.display:flex
8.classList是对类名进行操作(运行代码查看开发者工具中标签样式以获得更好理解)
9.transition: .2s all ease-in;分别对应了过渡时间、有过渡效果的属性、过渡效果的速度曲线(贝塞尔曲线)
10.opacity规定了不透明度(0~1,其中1为不透明)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值