CSS之色块滑动

4 篇文章 0 订阅

效果演示

图片

Code

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./01-色块滑动.css">
    <title>01-色块滑动</title>
</head>

<body>
    <a href="#">Hover Me</a>
</body>

</html>
body {
    height: 400px;
    display: flex;
    justify-content: center;
    align-items: center;
}
a {
    text-decoration: none;
    color: black;
    font-family: fans-serif;
    font-size: 40px;
    font-weight: bold;
    border: 2px solid black;
    padding: 40px 80px;
    position: relative;
    overflow: hidden;
}
a:before {
    content: "若冰说CSS";
    font-size: 20px;
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: 100%;
    background-color: black;
    transform: translate(-100%);
    transition: all 1s;
    z-index: -1;
}
a:hover:before {
    transform: translateX(0);
}
a:hover {
    color: white;
}

实现思路拆分

① 基本结构
<a href="#">Hover Me</a>
② 布局
body {
    /* 居中 */
    height: 400px;
    display: flex;
    justify-content: center;
    align-items: center;
}
③ 设置色块样式
a {
    /* 去除下划线 */
    text-decoration: none;

    /* 设置颜色 */
    color: black;

    /* 设置字体 */
    font-family: fans-serif;
    font-size: 40px;
    font-weight: bold;

    /* 设置边框 */
    border: 2px solid black;

    /* 内边距 */
    padding: 40px 80px;

    /* 定位 */
    position: relative;
}
④ 设置悬停样式
a:before {
    /* 内容 */
    content: "若冰说CSS";
    
    /* 文字大小 */
    font-size: 20px;
    
    /* 绝对定位 */
    position: absolute;

    /* 大小铺满 */
    left: 0;
    top: 0;
    height: 100%;
    width: 100%;
  
    /* 背景色 */
    background-color: black;
  
    /* 变化样式 */
    transform: translate(-100%);
    transition: all 1s;
    z-index: -1;
}
⑤ 隐藏悬停样式
a {
    /* 隐藏 */
    overflow: hidden;
}
⑥ 取消悬停恢复样式
a:hover:before {
    /* 取消悬停后样式 */
    transform: translateX(0);
}
a:hover {
    /* 颜色 */
    color: white;
}

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值