HTML5+CSS3+JS小实例:锥形渐变彩虹按钮

HTML:


<button class="button">Light</button>
<button class="button dark">Dark</button>

 

*{
    margin: 0;
    padding: 0;
}
body{
    min-height: 100vh;
    /* 弹性布局 居中演示 */
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #242424;
}
.button{
    width: 200px;
    height: 60px;
    border: 1px solid rgba(0,0,0,0.35);
    border-radius: 8px;
    margin: 0 8px;
    background-color: #d8d8d8;
    cursor: pointer;
    font-size: 20px;
    position: relative;
    /* 加个溢出隐藏 */
    overflow: hidden;
}
.button.dark{
    border-color: rgba(255, 255, 255, 0.12);
    background-color: #2b2b2b;
    color: #d8d8d8;
}
.button::after{
    content: "";
    width: 100px;
    height: 100px;
    /* 绝对定位 */
    position: absolute;
    /* --x、--y为CSS的自定义变量,通过var函数可对其调用 */
    /* 这里可以通过js获取鼠标的坐标并赋值给自定义变量,实现跟随鼠标移动 */
    left: var(--x);
    top: var(--y);
    transform: translate(-50%,-50%);
    /* 圆锥渐变 */
    background-image: conic-gradient(#00f,#0ff,#0f0,#ff0,#f00,#f0f,#00f);
    border-radius: 50%;
    /* 模糊滤镜 */
    filter: blur(15px);
    /* 混合模式:叠加 */
    mix-blend-mode: overlay;
    /* 默认隐藏 */
    opacity: 0;
    /* 不透明度变化时的过渡效果 */
    transition: opacity 0.2s ease-in-out;
}
/* Light按钮悬停 */
.button:hover::after{
    opacity: 1;
}
/* Dark按钮悬停 */
.button.dark:hover::after{
    opacity: 0.5;
}

JS:

// 获取按钮
const buttons=document.querySelectorAll('.button');

// 循环为每一个按钮绑定鼠标移动事件
buttons.forEach((button)=>{
    button.addEventListener('mousemove',(e)=>{
        // 获取鼠标的x、y坐标
        const x=e.offsetX;
        const y=e.offsetY;
        // 将x、y坐标赋值给CSS中的自定义变量--x、--y
        button.style.setProperty('--x',`${x}px`);
        button.style.setProperty('--y',`${y}px`);
    })
})

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

耀南.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值