背景色镂空技术之color属性实现颜色可控图标实现实例页面

回到相关文章 »

代码:

CSS代码:
.icon {
    display: inline-block;
    width: 16px; height: 20px;
    overflow: hidden;
    position: relative;
}
.icon:before {
    content: '█';
    margin: -5px 0 0 -5px;
    font-size: 30px;
    position: absolute;    
}
.icon:after {
    content: '';
    display: block;
    width: 100%; height: 100%;
    background-image: url(sprite_icons.png);
    position: relative;
}
.icon1:after { background-position: 0 0; }
.icon2:after { background-position: -20px 0; }
.icon3:after { background-position: -40px 0; }
.icon4:after { background-position: -60px 0; }
.link { margin-right: 15px; }
.out { color: #34538b; /* 该颜色控制图标的颜色 */}
                
HTML代码:
更改颜色:<input id="colorInput" type="color" value="#34538b" autocomplete="off">
<p id="colorP" class="out">
    <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"),
    eleP = document.getElementById("colorP");
eleInput.onchange = function() {
    eleP.style.color = this.value;
};
                

效果:

更改颜色:

返回 刷新 收藏 展开图片