背景色镂空技术之颜色可控图标效果实现实例页面

回到相关文章 »

代码:

CSS代码:
.icon {
    display: inline-block;
    width: 16px; height: 20px;
    background-image: url(sprite_icons.png);
    background-color: #34538b; /* 该颜色控制图标的颜色 */
}
.icon1 { background-position: 0 0; }
.icon2 { background-position: -20px 0; }
.icon3 { background-position: -40px 0; }
.icon4 { background-position: -60px 0; }
.link { margin-right: 15px; }
                
HTML代码:
更改颜色:<input id="colorInput" type="color" value="#34538b" autocomplete="off">
<p>
    <i class="icon icon1"></i><a href="##" class="link">返回</a>
    <i class="icon icon2"></i><a href="##" class="link">刷新</a>
    <i class="icon icon3"></i><a href="##" class="link">收藏</a>
    <i class="icon icon4"></i><a href="##" class="link">展开图片</a>
</p>
                
JS代码:
var eleInput = document.getElementById("colorInput"),
    eleIcons = document.getElementsByTagName("i");
eleInput.onchange = function() {
    var i = 0, l = eleIcons.length;
    for (; i<l; i+=1) {
        eleIcons[i].style.backgroundColor = this.value;
    }
};
                

效果:

更改颜色:

返回 刷新 收藏 展开图片