HTML5+CSS3+JS小实例:文字阴影还能这么玩

HTML:


<div class="container">
    <h1>HELLO WORLD!</h1>
</div>

 CSS:

/* 引入谷歌字体 */
@import url("http://fonts.googleapis.com/css?family=Righteous");

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
body{
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: 'Righteous';
    font-weight: bold;
    --bg:#131313;
    background-color: var(--bg);
}
.container{
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    --x:1;
    --y:1;
}
h1{
    font-size: 5vw;
    color: var(--bg);
    text-align: center;
    text-shadow: -1px -1px 0 #fff,1px 1px 0 #fff,1px -1px 0 #fff,-1px 1px 0 #fff,
    calc(var(--x) * -0.1vw) calc(var(--y) * -0.1vw) 0px hsl(calc(var(--x) + var(--y) * 11) 30% 50% / 40%),
    calc(var(--x) * -0.2vw) calc(var(--y) * -0.2vw) 1px hsl(calc(var(--x) + var(--y) * 12) 30% 50% / 36%),
    calc(var(--x) * -0.3vw) calc(var(--y) * -0.3vw) 2px hsl(calc(var(--x) + var(--y) * 13) 30% 50% / 32%),
    calc(var(--x) * -0.4vw) calc(var(--y) * -0.4vw) 3px hsl(calc(var(--x) + var(--y) * 14) 30% 50% / 28%),
    calc(var(--x) * -0.5vw) calc(var(--y) * -0.5vw) 4px hsl(calc(var(--x) + var(--y) * 15) 30% 50% / 24%),
    calc(var(--x) * -0.6vw) calc(var(--y) * -0.6vw) 5px hsl(calc(var(--x) + var(--y) * 16) 30% 50% / 20%),
    calc(var(--x) * -0.7vw) calc(var(--y) * -0.7vw) 6px hsl(calc(var(--x) + var(--y) * 17) 30% 50% / 16%),
    calc(var(--x) * -0.8vw) calc(var(--y) * -0.8vw) 7px hsl(calc(var(--x) + var(--y) * 18) 30% 50% / 12%);
}

JS:

// 容器
const container=document.querySelector('.container');

// 获取当前坐标
function getCursorPosition(event,rect,divider=20){
    const x=event.clientX - rect.left;
    const y=event.clientY - rect.top;
    const res={
        x:(x - rect.width / 2) / divider,
        y:(y - rect.height / 2) / divider
    };
    return res;
}

// 根据日期计算获取数字
function getNumbersFromDate(){
    const startDate=new Date(); //存储初始日期

    function updateNumbers(){
        const currentDate=new Date();
        const timeDiff=currentDate - startDate;

        // 根据时差计算出数字
        const number1=Math.sin(timeDiff / 400);
        const number2=Math.cos(timeDiff / 400);

        // 将数字作为对象返回
        return {
            number1,
            number2
        }
    }

    // 更新坐标
    function updateElementPosition(x,y){
        container.style.setProperty('--x',x);
        container.style.setProperty('--y',y);
    }

    // 绘制动画
    function animationLoop(){
        // 判断当前DOM节点是否能完全匹配对应的CSS的hover选择器
        const isHovered=container.matches(':hover');
        const rect=container.getBoundingClientRect();

        if(isHovered){
            // 鼠标移入
            // 绑定指针移动事件
            document.body.addEventListener('pointermove',(event)=>{
                const cursorPosition=getCursorPosition(event,rect);
                // 更新坐标
                updateElementPosition(cursorPosition.x,cursorPosition.y);
            })
        }else{
            // 鼠标移出,初始化坐标
            const numbers=updateNumbers();
            updateElementPosition(numbers.number1 * 10,numbers.number2 * 10);
        }
        // 逐帧绘制
        requestAnimationFrame(animationLoop);
    }
    animationLoop();
}

getNumbersFromDate();

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

耀南.

你的鼓励将是我最最最最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值