涟漪效果不适用于非铬色的浏览器

时间:2018-03-06 04:42:52

标签: html css

点击按钮时,我试图设置涟漪效果。

我已经成功完成了,但它只能在谷歌浏览器的浏览器中使用,我不知道为什么。该效果未显示在Safari或FireFox中(尚未测试IE)。

演示: https://jsfiddle.net/uwa67xff/5/

HTML:

<p><b>NOTE:</b> This ripple effect is only working on Chrome browser.</p>
<button>Click Me</button>

CSS:

button {
    border: none;
    cursor: pointer;
    color: black;
    width: 130px;
    padding: 10px;
    border-radius: 2px;
    font-size: 19px;
    background: green;
    position: relative;
    overflow: hidden;
}

button:after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 5px;
    height: 5px;
    background: rgba(255, 255, 255, .5);
    opacity: 0;
    border-radius: 100%;
    transform: scale(1, 1) translate(-50%);
    transform-origin: 50% 50%;
}

@keyframes ripple {
    0% {
        transform: scale(0, 0);
        opacity: 1;
    }
    20% {
        transform: scale(25, 25);
        opacity: 1;
    }
    100% {
        opacity: 0;
        transform: scale(40, 40);
    }
}

button:focus:not(:active)::after {
    animation: ripple 1s ease-out;
}

button:focus{
    outline: none;
}

* { box-sizing:border-box; }

2 个答案:

答案 0 :(得分:0)

您需要为safari和chrome添加-webkit-,为mozila添加-moz-,为Opera浏览器添加-o-

button {
    border: none;
    cursor: pointer;
    color: black;
    width: 130px;
    padding: 10px;
    border-radius: 2px;
    font-size: 19px;
    background: green;
    position: relative;
    overflow: hidden;
}

button:after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 5px;
    height: 5px;
    background: rgba(255, 255, 255, .5);
    opacity: 0;
    border-radius: 100%;
    transform: scale(1, 1) translate(-50%);
    transform-origin: 50% 50%;
}

@keyframes ripple {
    0% {
        transform: scale(0, 0);
        opacity: 1;
    }
    20% {
        transform: scale(25, 25);
        opacity: 1;
    }
    100% {
        opacity: 0;
        transform: scale(40, 40);
    }
}
@-webkit-keyframes ripple {
    0% {
        transform: scale(0, 0);
        opacity: 1;
    }
    20% {
        transform: scale(25, 25);
        opacity: 1;
    }
    100% {
        opacity: 0;
        transform: scale(40, 40);
    }
}
@-moz-keyframes ripple {
    0% {
        transform: scale(0, 0);
        opacity: 1;
    }
    20% {
        transform: scale(25, 25);
        opacity: 1;
    }
    100% {
        opacity: 0;
        transform: scale(40, 40);
    }
}
@-o-keyframes ripple {
    0% {
        transform: scale(0, 0);
        opacity: 1;
    }
    20% {
        transform: scale(25, 25);
        opacity: 1;
    }
    100% {
        opacity: 0;
        transform: scale(40, 40);
    }
}

button:focus:not(:active)::after {
    animation: ripple 1s ease-out;
}

button:focus{
    outline: none;
}

* { box-sizing:border-box; }
<p><b>NOTE:</b> This ripple effect is only working on Chrome browser.</p>
<button>Click Me</button>

答案 1 :(得分:0)

在CSS代码中,有些属性没有工作标准,可能永远不会成为标准(transform,@ keyframes)。为了克服这个问题,我们使用Vendor Prefixes(-webkit,-moz)。

了解更多go here

使用供应商前缀代码

* { box-sizing:border-box; }

button {
    border: none;
    cursor: pointer;
    color: black;
    width: 130px;
    padding: 10px;
    border-radius: 2px;
    font-size: 19px;
    background: green;
    position: relative;
    overflow: hidden;
}

button:after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 5px;
    height: 5px;
    background: rgba(255, 255, 255, .5);
    opacity: 0;
    border-radius: 100%;
    -webkit-transform: scale(1, 1) translate(-50%);
    -moz-transform: scale(1, 1) translate(-50%);
    -ms-transform: scale(1, 1) translate(-50%);
    -o-transform: scale(1, 1) translate(-50%);
    transform: scale(1, 1) translate(-50%);
    -webkit-transform-origin: 50% 50%;
    -moz-transform-origin: 50% 50%;
    -ms-transform-origin: 50% 50%;
    -o-transform-origin: 50% 50%;
    transform-origin: 50% 50%;
}

@keyframes ripple {
    0% {
        -webkit-transform: scale(0, 0);
        -moz-transform: scale(0, 0);
        -ms-transform: scale(0, 0);
        -o-transform: scale(0, 0);
        transform: scale(0, 0);
        opacity: 1;
    }
    20% {
        -webkit-transform: scale(25, 25);
        -moz-transform: scale(25, 25);
        -ms-transform: scale(25, 25);
        -o-transform: scale(25, 25);
        transform: scale(25, 25);
        opacity: 1;
    }
    100% {
        opacity: 0;
        -webkit-transform: scale(40, 40);
        -moz-transform: scale(40, 40);
        -ms-transform: scale(40, 40);
        -o-transform: scale(40, 40);
        transform: scale(40, 40);
    }
}

button:focus:not(:active)::after {
    -webkit-animation: ripple 1s ease-out;
    -moz-animation: ripple 1s ease-out;
    -o-animation: ripple 1s ease-out;
    animation: ripple 1s ease-out;
}

button:focus{
    outline: none;
}
<p><b>NOTE:</b> This ripple effect is only working on Chrome browser.</p>
<button>Click Me</button>

较新的CSS属性需要哪些前缀。 go here