更改悬停CSS上的内容

时间:2014-10-18 06:18:44

标签: css css-counter

我想在悬停时更改列表内容,即罗马变为数字1,2,3

我尝试了li:hover li:hover:after,但两者都会添加内容和现有内容



ul {
  min-height: 30px;
  width: 180px;
  margin-left:-30px;
  list-style-type: none;
  counter-reset: chrome-counter;
}

li:before {
  content: counter(chrome-counter, lower-roman);
  padding: 1px 2px;
  margin-right: 0.2em;
  font-weight: bold;
}

li {
  margin: 2px 0;
  padding: 1px 4px;
  font: normal bold 18px/32px  ms-sans, geneva, sans-serif;
  display: inline-block;
  width: 40px;
  text-align: center;
  color: #FFF;
  counter-increment: chrome-counter;
}

li:hover {
  padding: 4px 8px;
  color: #333;
  /*content : '..';*/
  counter-reset : counter(chrome-counter, upper-roman);
  box-shadow: 0px 0px 4px #222;
  border-radius: 1.5em .5em / 2.5em 2em;
}

.cc1 {
  background-color: #FF6766;
  color: #FFF;
}

 <ul class="scheme">
      <li class="cc1" ></li>
     <li class="cc1" ></li>
     <li class="cc1" ></li>
</ul>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

使用此选择器:

li:hover:before {
    content: counter(chrome-counter, decimal);
}

&#13;
&#13;
ul {
  min-height: 30px;
  width: 180px;
  margin-left:-30px;
  list-style-type: none;
  counter-reset: chrome-counter;
}

li:before {
  content: counter(chrome-counter, lower-roman);
  padding: 1px 2px;
  margin-right: 0.2em;
  font-weight: bold;
}

li {
  margin: 2px 0;
  padding: 1px 4px;
  font: normal bold 18px/32px  ms-sans, geneva, sans-serif;
  display: inline-block;
  width: 40px;
  text-align: center;
  color: #FFF;
  counter-increment: chrome-counter;
}

li:hover {
  padding: 4px 8px;
  color: #333;
  /*content : '..';*/
  counter-reset : counter(chrome-counter, upper-roman);
  box-shadow: 0px 0px 4px #222;
  border-radius: 1.5em .5em / 2.5em 2em;
  }

li:hover:before {
  content: counter(chrome-counter, decimal);
}

.cc1 {
  background-color: #FF6766;
  color: #FFF;
}
&#13;
<ul class="scheme">
      <li class="cc1" ></li>
     <li class="cc1" ></li>
     <li class="cc1" ></li>
</ul>
&#13;
&#13;
&#13;

答案 1 :(得分:3)

如果您正在使用:before伪元素,则需要将所有状态应用于该确切元素...所以不应该对主元素应用,也不要对:after元素,而是需要的元素要更改,在您的情况下为li:before

&#13;
&#13;
ul {
  min-height: 30px;
  width: 180px;
  margin-left:-30px;
  list-style-type: none;
  counter-reset: chrome-counter;
}

li:before {
  content: counter(chrome-counter, lower-roman);
  padding: 1px 2px;
  margin-right: 0.2em;
  font-weight: bold;
}

li {
  margin: 2px 0;
  padding: 1px 4px;
  font: normal bold 18px/32px  ms-sans, geneva, sans-serif;
  display: inline-block;
  width: 40px;
  text-align: center;
  color: #FFF;
  counter-increment: chrome-counter;
}

li:hover {
  padding: 4px 8px;
  color: #333;
  /*content : '..';*/
  counter-reset : counter(chrome-counter, upper-roman);
  box-shadow: 0px 0px 4px #222;
  border-radius: 1.5em .5em / 2.5em 2em;
}

li:hover:before {
  content: counter(chrome-counter, upper-roman);
  padding: 1px 2px;
  margin-right: 0.2em;
  font-weight: bold;
}

.cc1 {
  background-color: #FF6766;
  color: #FFF;
}
&#13;
 <ul class="scheme">
     <li class="cc1" ></li>
     <li class="cc1" ></li>
     <li class="cc1" ></li>
</ul>
&#13;
&#13;
&#13;

以下是您在评论中要求更改为HTML内容的示例:

&#13;
&#13;
ul {
  min-height: 30px;
  width: 180px;
  margin-left:-30px;
  list-style-type: none;
  counter-reset: chrome-counter;
}

li:before {
  content: counter(chrome-counter, lower-roman);
  padding: 1px 2px;
  margin-right: 0.2em;
  font-weight: bold;
}

li {
  margin: 2px 0;
  padding: 1px 4px;
  font: normal bold 18px/32px  ms-sans, geneva, sans-serif;
  display: inline-block;
  width: 40px;
  text-align: center;
  color: #FFF;
  counter-increment: chrome-counter;
}

li:hover {
  padding: 4px 8px;
  color: #333;
  /*content : '..';*/
  counter-reset : counter(chrome-counter, upper-roman);
  box-shadow: 0px 0px 4px #222;
  border-radius: 1.5em .5em / 2.5em 2em;
}

li:hover:before {
  content: "";
  padding: 1px 2px;
  margin-right: 0.2em;
  font-weight: bold;
}

li span {display: none}
li:hover span {display: initial}

.cc1 {
  background-color: #FF6766;
  color: #FFF;
}
&#13;
 <ul class="scheme">
      <li class="cc1" ><span>#</span></li>
     <li class="cc1" ><span>0</span></li>
     <li class="cc1" ><span>*</span></li>
</ul>
&#13;
&#13;
&#13;