css实现动态水波纹效果

效果如下:

  1. 外层容器 (shop_wrap):

    • 设置外边距 (padding) 提供一些间距和边距
  2. 圆形容器 (TheCircle):

    • 使用相对定位 (position: relative),宽度和高度均为 180px,形成一个圆形按钮
    • 圆角半径 (border-radius) 设置为 50%,使其呈现圆形
    • 边框 (border) 和阴影 (box-shadow) 提供边框和轻微的立体感
    • 设置溢出隐藏 (overflow: hidden),确保水波纹效果在容器内显示
    • 鼠标悬停时显示手型光标 (cursor: pointer)
  3. 水波纹容器 (Water):

    • 绝对定位 (position: absolute),覆盖在圆形容器上
    • 设置宽度和高度为 100%,形成一个完整的圆形水波纹效果
    • 设置背景颜色 (background-color) 为水波纹的颜色
    • 圆角半径 (border-radius) 同样设置为 50%
    • 溢出隐藏 (overflow: hidden),确保水波纹效果不超出容器
  4. 文字居中显示 (CenteredText):

    • 绝对定位 (position: absolute),位于水波纹容器中心
    • 使用 transform 属性将文字居中显示
  5. 水波纹效果 (Water::afterWater::before):

    • 使用 ::after::before 伪元素创建水波纹效果
    • 设置宽度和高度为 150%,略大于容器,以确保水波纹效果覆盖整个容器
    • 设置圆角半径,形成圆形效果
    • 设置动画 (animation),通过关键帧 (@keyframes) 实现水波纹的旋转和缩放效果

源码如下: 

<template>
    <div class="shop_wrap">
        <div class="TheCircle">
            <div class="Water">
                <span class="CenteredText">上传图片</span>
            </div>
        </div>
    </div>
</template>

<script setup>
</script>

<style lang="scss" scoped>
.shop_wrap {
    padding: 50px; /* 设置外层容器的内边距 */
    
    .TheCircle {
        position: relative;
        width: 180px; /* 设置圆形容器的宽度 */
        height: 180px; /* 设置圆形容器的高度 */
        border-radius: 50%; /* 圆形容器的圆角半径 */
        border: 1px solid #38b973; /* 圆形容器的边框样式 */
        box-shadow: 0 0 0 1px #38b973; /* 圆形容器的阴影样式 */
        overflow: hidden; /* 确保容器裁剪水波纹效果 */
        cursor: pointer; /* 鼠标悬停时显示手型光标 */
    }

    .Water {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-color: #38b973; /* 水波纹的颜色 */
        border-radius: 50%;
        overflow: hidden;
        z-index: 1; /* 确保水波纹在文字之上 */
    }

    .CenteredText {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%); /* 居中显示文字 */
        color: #333; /* 文字颜色 */
        z-index: 2; /* 确保文字在水波纹之上 */
    }

    .Water::after {
        content: '';
        position: absolute;
        top: 0;
        left: 50%;
        width: 150%;
        height: 150%;
        border-radius: 40%;
        background-color: rgb(240, 228, 228); /* 水波纹内部颜色 */
        animation: real 5s linear infinite; /* 实际水波纹的动画效果 */
    }

    @keyframes real {
        0% {
            /* 初始状态:向上平移50%、左平移65%并旋转0度 */
            transform: translate(-50%, -65%) rotate(0deg); 
        }
        100% {
            /* 终止状态:向上平移50%、左平移65%并旋转360度,形成旋转一周的效果 */
            transform: translate(-50%, -65%) rotate(360deg); 
        }
    }

    .Water::before {
        content: '';
        position: absolute;
        top: 0;
        left: 50%;
        width: 150%;
        height: 150%;
        border-radius: 42%;
        background-color: rgb(240, 228, 228, 0.2); /* 水波纹外部颜色及透明度 */
        animation: virtual 7s linear infinite; /* 虚拟水波纹的动画效果 */
    }

    @keyframes virtual {
        0% {
            /* 初始状态:向上平移50%、左平移60%,不进行缩放,旋转0度 */
            transform: translate(-50%, -60%)  scale(1)  rotate(0deg); 
        }

        100% {
            /* 终止状态:向上平移50%、左平移60%,进行1.1倍的缩放,旋转360度,
            形成旋转一周的效果并放大水波纹 */
            transform: translate(-50%, -60%) scale(1.1) rotate(360deg); 
            
        }
    }
}
</style>
  • 14
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值