如何为CSS加载器设置彩色环形动画样式?
如何给CSS涟漪预加载器设置彩色环形
你当前的预加载器环形是白色,因为代码里.lds-ripple div的border颜色被硬编码为#fff,直接给.lds-ripple加color: red !important不会生效——color属性默认不会影响边框颜色,除非明确引用它。
有两种简单的修改方式:
方法一:直接指定边框颜色
直接修改.lds-ripple div的border颜色值为你想要的色彩,比如红色:
.lds-ripple div { position: absolute; border: 4px solid red; /* 替换原来的#fff */ opacity: 1; border-radius: 50%; animation: lds-ripple 1s cubic-bezier(0, 0.2, 0.8, 1) infinite; }
方法二:用currentColor继承父元素颜色
这种方式更灵活,后续只需修改.lds-ripple的color就能改变环形颜色:
- 给
.lds-ripple添加color属性:
.lds-ripple { display: inline-block; position: relative; width: 80px; height: 80px; color: red; /* 设置想要的颜色 */ }
- 将
.lds-ripple div的边框颜色改为currentColor:
.lds-ripple div { position: absolute; border: 4px solid currentColor; /* 替换原来的#fff */ opacity: 1; border-radius: 50%; animation: lds-ripple 1s cubic-bezier(0, 0.2, 0.8, 1) infinite; }
修改后的完整代码示例
<button id="btnShow" onclick="showLoader()">Show Loader</button> <div id="loader"> <div class="lds-ripple"> <div></div> <div></div> </div> </div> <style> #loader { display: none; justify-content: center; align-items: center; width:100%; height:100%; border:green solid 1px; opacity:0.9; background-color:whitesmoke; position:absolute; left:0; top:0; } .lds-ripple { display: inline-block; position: relative; width: 80px; height: 80px; color: red; /* 这里控制颜色 */ } .lds-ripple div { position: absolute; border: 4px solid currentColor; opacity: 1; border-radius: 50%; animation: lds-ripple 1s cubic-bezier(0, 0.2, 0.8, 1) infinite; } .lds-ripple div:nth-child(2) { animation-delay: -0.5s; } @keyframes lds-ripple { 0% { top: 36px; left: 36px; width: 0; height: 0; opacity: 0; } 4.9% { top: 36px; left: 36px; width: 0; height: 0; opacity: 0; } 5% { top: 36px; left: 36px; width: 0; height: 0; opacity: 1; } 100% { top: 0px; left: 0px; width: 72px; height: 72px; opacity: 0; } } </style> <script> function showLoader() { document.getElementById("loader").style.display = "flex"; } </script>
内容的提问来源于stack exchange,提问作者Sunil
相关产品推荐
相关产品推荐

