css 如何将涟漪效应仅限制在按钮内

wko9yo5t  于 2023-03-14  发布在  其他
关注(0)|答案(1)|浏览(101)

我试图使按钮的涟漪效应,但我的涟漪是走出按钮,你可以看到在下面给出的片段。我甚至设置
溢出:隐藏;
还是在熄灭。2请告诉我怎样把涟漪限制在按钮内。

document.getElementById("btn").addEventListener("mousedown",(e)=>{
var btn = document.getElementById("btn")
var ripple = document.createElement("span")
            ripple.classList.add("ripple")
            btn.appendChild(ripple)
            ripple.style.animation = "ripple 500ms linear forwards"
            ripple.style.top = e.clientY - 15 + "px";
            ripple.style.left = e.clientX - 15 + "px";
            setTimeout(()=>{
              ripple.remove()
            },500)

})
.modaljs-btn-green {
    /* position: relative; */
    padding: 10px;
    font-size: 15px;
    color: #28b32f;
    font-weight: 600;
    border: none;
    outline: none;
    border-radius: 2px;
    transition: all 325ms;
    cursor: pointer;
    margin-left: 10px;
    overflow: hidden;
}
.modaljs-btn-green:hover {
    background-color: rgb(198, 255, 196);
}

.ripple{
    border-radius: 50%;
    position: absolute;
    background-color: grey;
    width: 50px;
    height: 50px;
    opacity: 0.5;
}

@keyframes ripple{
    0%{
        transform: scale(0);
        opacity: 0.5;
    }
    100%{
        transform: scale(3);
        opacity: 0;
    }
}
<button id="btn" class="modaljs-btn-green">LOGIN</button>
kt06eoxx

kt06eoxx1#

尝试将position: relative;添加到.modaljs-btn-green

document.getElementById("btn").addEventListener("mousedown",(e)=>{
var btn = document.getElementById("btn")
var ripple = document.createElement("span")
            ripple.classList.add("ripple")
            btn.appendChild(ripple)
            ripple.style.animation = "ripple 500ms linear forwards"
            ripple.style.top = e.clientY - 15 + "px";
            ripple.style.left = e.clientX - 15 + "px";
            setTimeout(()=>{
              ripple.remove()
            },500)

})
.modaljs-btn-green {
    position: relative;
    padding: 10px;
    font-size: 15px;
    color: #28b32f;
    font-weight: 600;
    border: none;
    outline: none;
    border-radius: 2px;
    transition: all 325ms;
    cursor: pointer;
    margin-left: 10px;
    overflow: hidden;
}
.modaljs-btn-green:hover {
    background-color: rgb(198, 255, 196);
}

.ripple{
    border-radius: 50%;
    position: absolute;
    background-color: grey;
    width: 50px;
    height: 50px;
    opacity: 0.5;
}

@keyframes ripple{
    0%{
        transform: scale(0);
        opacity: 0.5;
    }
    100%{
        transform: scale(3);
        opacity: 0;
    }
}
<button id="btn" class="modaljs-btn-green">LOGIN</button>

相关问题