javascript - 为什么点击这个按钮时会出现一个随机空格?

标签 javascript html css

我一直在尝试复制一些 Material 设计按钮,但遇到了为创建“涟漪”效果而生成的 div 的问题。如果你去我的codepen https://codepen.io/AlexStiles/pen/oPomzX你会看到这个问题。

这是由 div 引起的(我尝试删除它并解决了问题)。我尝试添加各种属性,例如字体大小和行高,但都无济于事。有趣的是,根据您的浏览器,这个问题似乎有不同的影响。在 safari 上,宽度会大幅增加,然后会减小到 chrome 宽度。

"use strict";

const buttons = document.getElementsByTagName("button");
const overlay = document.getElementsByClassName("overlay");
const animationTime = 600;

for (let i = 0; i < buttons.length; i++) {
    buttons[i].addEventListener("click", createRipple);
};

let circle = document.createElement("div");

function createRipple(e) {

    this.appendChild(circle);

    var d = Math.max(this.scrollWidth, this.scrollHeight);

    circle.style.width = circle.style.height = d + "px";

    circle.style.left = e.clientX - this.offsetLeft - d / 2 + "px";
    circle.style.top = e.clientY - this.offsetTop -  d / 2 + "px";

    circle.classList.add("ripple");
    // setTimeout(function(){
    //     for (let i = 0; i < circle.length; i++)
    //     document.getElementsByClassName("ripple")[i].remove();
    // }, animationTime);
}
button {
    background-color: #4888f1;
    border-radius: 24px;
    display: flex;
    align-items: center;
    outline: 0;
    border: 0;
    padding: 10px 22px;
    cursor: pointer;
    overflow: hidden;
    position: relative;
}

button .ripple {
    position: absolute;
    border-radius: 50%;
    background-color: rgba(255, 255, 255, 0.5);
    transform: scale(0);
    animation: ripple 0.5s linear;
    font-size: 0;
    line-height: 0;
}

@keyframes ripple {
    to {
        transform: scale(2.5);
        opacity: 0;
    }
}

button img {
    width: 20px;
    height: 20px;
}

button *:not(:last-child) {
    margin: 0 8px 0 0;
}

button span {
    color: #fff;
    font-family: Futura;
}

@media screen and (min-width: 1280px) {
    button {
        padding: 0.8vw 1.75vw;
        border-radius: 1.9vw;
    } button img {
        width: 1.55vw;
        height: auto;
    } button span {
        font-size: 0.8vw;
    }
}
<html>
    <head>
        <title>Material Design Components</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <button>
        <span>Add to Cart</span>
    </button>
    <script src="js.js"></script>
</html>

最佳答案

改变

button *:not(:last-child) {
    margin: 0 8px 0 0;
}

为了,

button *:not(:last-child) {
    margin: 0 0 0 0;
}

在 firefox 中检查。

关于javascript - 为什么点击这个按钮时会出现一个随机空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52233816/

相关文章:

javascript - 为什么 WebGL 'clear' 绘制到前端缓冲区?

javascript - 单击弹出选项时如何获取行的 Id(使用全局变量获取 Id)?

javascript - 为什么通常使用标签 <UL> 和 <LI> 创建工具栏?

html - 使溢出内容在div内滚动

html - 在内部 iframe 上显示以 100% 宽度截断的右边框

html - 如何在 portlet jsp 上定位 html 对象?

javascript - 使用 ES6/2015 导出对象文字的最佳方法是什么?

javascript - 使用 ws ://while on https://(mixed content)

html - 连字符在 HTML 中导致换行

允许渲染后使用注入(inject)对象的Javascript模板解决方案?