记录一些有趣的前端技巧(css)

一、九宫格图片

需求场景:

实现一个图片就行九宫格展示,hover可以过渡进行组合,形成完整图片,示例如下。

在这里插入图片描述在这里插入图片描述


实现方案:

对每个图片元素进行background-position展示部分图片

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>九宫格</title>
</head>
<body>
<div class="app-container">
    <div class="img-swapper">
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
    </div>
</div>
</body>
<style>
    body {
        margin: 0;
        padding: 0;
    }

    .app-container {
        position: absolute;
        inset: 0;
        display: flex;
        justify-content: center;
        align-items: center;
        background: black;
    }

    .img-swapper {
        width: 300px;
        height: 300px;
        display: flex;
        flex-wrap: wrap;
        position: relative;
    }

    .img {
        width: 100px;
        height: 100px;
        background-image: url("../assets/img/lufei.png");
        background-size: 300px 300px;
        transition: all 0.5s linear;
        box-shadow: 0 0 8px 2px lightgray;
        border-radius: 50%;

    }

    .img:nth-child(3n) {
        position: relative;
        left: 20px;
        background-position-x: -200%;
    }

    .img:nth-child(3n+1) {
        position: relative;
        left: -20px;
        background-position-x: 0;
    }

    .img:nth-child(3n+2) {
        position: relative;
        left: 0;
        background-position-x: -100%;
    }

    .img:nth-child(n+7) {
        position: relative;
        top: 20px;
        background-position-y: -200%;
    }

    .img:nth-child(-n+6) {
        position: relative;
        top: 0;
        background-position-y: -100%;
    }

    .img:nth-child(-n+3) {
        position: relative;
        top: -20px;
        background-position-y: 0;
    }
    .img-swapper:hover .img{
        top: 0;
        left: 0;
        box-shadow: none;
        border-radius: 0;
    }
</style>
</html>

二、边框按钮

需求场景:

边框颜色旋转按钮
在这里插入图片描述在这里插入图片描述


实现方案:

对每个图片元素进行background-position展示部分图片

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ButtonBorder</title>
</head>

<body>
    <div class="btn-wrapper">
        <div class="btn">边框按钮</div>
    </div>
</body>

</html>

<style>
    body {
        margin: 0;
        padding: 0;
    }

    .btn-wrapper {
        background-color: black;
        height: 100%;
        position: absolute;
        inset: 0;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .btn {
        color: white;
        background-color: red;
        font-size: 20px;
        font-weight: 700;
        padding: 8px 12px;
        border-radius: 8px;
        position: relative;
        z-index: 200;
        overflow: hidden;

    }

    .btn::before {
        content: ' ';
        width: 300%;
        height: 300%;
        position: absolute;
        background-color: #0087ff;
        border-radius: 8px;
        z-index: -100;
        left: 50%;
        top: 50%;
        transform-origin: 0 0;
        animation: identifier 1.5s infinite linear;
    }

    .btn::after {
        content: '';
        position: absolute;
        top: 2px;
        left: 2px;
        border-radius: 8px;
        width: calc(100% - 4px);
        height: calc(100% - 4px);
        background-color: black;
        z-index: -2;
    }

    @keyframes identifier {
        to {
            transform: rotateZ(360deg);
        }
    }
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值