最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change inner color of glyphicons in Bootstrap? - Stack Overflow

programmeradmin0浏览0评论

I have an empty-star button like this,

<button type="button" class="btn btn-default btn-lg" id="refresh">
      <span class="glyphicon glyphicon-star-empty"></span>
</button>

When I click it, I want the inner color to change to yellow like shown here - .png

How can we achieve this using Bootstrap and Javascript?

Here's a link to what I've got so far - ,output

I have an empty-star button like this,

<button type="button" class="btn btn-default btn-lg" id="refresh">
      <span class="glyphicon glyphicon-star-empty"></span>
</button>

When I click it, I want the inner color to change to yellow like shown here - http://s21.postimg/3qwgsnrcj/star.png

How can we achieve this using Bootstrap and Javascript?

Here's a link to what I've got so far - http://jsbin./refatelefi/edit?html,output

Share Improve this question asked Apr 2, 2016 at 19:40 Kemat RochiKemat Rochi 9523 gold badges23 silver badges37 bronze badges 3
  • 1 @Paulie_D It's possible, but it's a bit nasty. What OP has to do: add another star (with JS) when clicked on the button. See jsfiddle/066bkv87 – Roy Commented Apr 2, 2016 at 19:48
  • 1 I meant with CSS...anything is possible with JS – Paulie_D Commented Apr 2, 2016 at 19:51
  • @Paulie_D Check. Luckily OP can use JS. – Roy Commented Apr 2, 2016 at 19:55
Add a ment  | 

3 Answers 3

Reset to default 5

A glyphicon is like a character. You can change it's color by changing the color property of the span style.

<button type="button" class="btn btn-default btn-lg" id="refresh">
   <span class="glyphicon glyphicon-star-empty"></span>
</button>

.glyphicon-star-empty {
   color: yellow;
}

However, the icon you picked is not full, so you can't change the inside as you asked. I would remend using the "glyphicon glyphicon-star" instead.

<button type="button" class="btn btn-default btn-lg" id="refresh">
   <span class="glyphicon glyphicon-star"></span>
</button>

.glyphicon-star {
   color: yellow;
}

You may use a full icon and color then redraw the outside via text-shadow with black or any other color.

.glyphicon-star {
   color: yellow;
  text-shadow:0 0  black,0 0  black,0 0  black,0 0  black,0 0  black;
}
.bis.glyphicon-star {
  text-shadow:0 0 1px  black,0 0 1px  black,0 0 1px  black,0 0 1px  black ;
}
<link href="https://netdna.bootstrapcdn./bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<button type="button" class="btn btn-default btn-lg" id="refresh">
   <span class="glyphicon glyphicon-star"></span>
</button>
<button type="button" class="btn btn-default btn-lg" id="refresh">
   <span class="glyphicon glyphicon-star bis"></span>
</button>

I am not sure about getting it exactly similar to your star.png, while you can use javascript to set css color property on the button or you might want to use background-color property. Here is a simple demo http://jsbin./netufaruxa/1/edit?html,js,output

HTML:

<body>
 <button id="myBtn" type="button" class="btn btn-default btn-lg" id="refresh">
      <span class="glyphicon glyphicon-star-empty"></span>
 </button>
</body>

JS:

function colorChange(){
  if(document.getElementById("myBtn").style.color==""){
    document.getElementById("myBtn").style.color="yellow";
  }else{
    document.getElementById("myBtn").style.color="";
  }
}
(function() {
document.getElementById("myBtn").addEventListener("click", colorChange);
})();
发布评论

评论列表(0)

  1. 暂无评论