简体   繁体   English

单击CSS中的图标时如何更改颜色

[英]How to change color when click icon in css

I'm facing about the problem of change color when click on icon heart in css and jquery 我在单击CSS和jquery中的icon heart时遇到颜色更改的问题

css: CSS:

when not yet click icon 当尚未点击图标时

.iconHeartInactive::before {
   content: '\e841';
   color: rgba(71,78,82,.4);
 }

when clicked icon: 单击图标时:

.iconHeartActive::after{
    content: '\e845';
    color: #ed1b77;
 }

HTML: HTML:

<span>
<button class="saveHome hoverPulse pan typeReversed">
    <span class="stackIcons">
        <i class="iconHeartActive iconOnly"></i>
        <i class="iconHeartEmpty typeReversed iconOnly"></i>
    </span>
</button>
</span> 
<script>
   $( "iconHeartInactive" ).click(function() {
   $( this ).toggleClass( "iconHeartActive" );
 });
 </script>

How can I click on icon and it change color? 如何单击图标,它会改变颜色? thank so much ! 非常感谢 !

First of all: put your JS code in a ready wrapper: 首先:将您的JS代码放入一个包装好的包装中:

$(document).ready(function(){
  //your code here
});

Second: We use toggleClass to set and unset a class on an object so you have 2 option. 第二:我们使用toggleClass在对象上设置和取消设置类,因此您有2个选项。 1- change your CSS so in default it get your inactive style and when clicked on it it get the active styling. 1-更改您的CSS,以便默认情况下它会获得您的非活动样式,并且在单击它时会获得活动的样式。 2- change the js to check if it has the active class or not so based on THAT statement you decied wheter or not to set one of your classes. 2-更改js以检查它是否具有活动的类,因此根据您认为是否不设置其中一个类的THAT语句进行更改。

since it seems you don't want to change your styling: 因为您似乎不想更改样式:

<span>
<button class="saveHome hoverPulse pan typeReversed">
    <span class="stackIcons">
        <i class="iconHeartActive chosenHeartIcon iconOnly"></i>
        <i class="iconHeartEmpty typeReversed iconOnly"></i>
    </span>
</button>
</span> 
<script>
    $(document).ready(function(){
       $( ".chosenHeartIcon" ).click(function() {
        if($(this).hasClass('iconHeartActive')) {
            $( this ).removeClass( "iconHeartActive" );
            $( this ).addClass( "iconHeartInactive" );
        } else {
            $( this ).removeClass( "iconHeartInactive" );
            $( this ).addClass( "iconHeartActive" );
        }

     });
 });
 </script>

1 Fiddle Link http://jsfiddle.net/JfGVE/2029/ 1小提琴链接http://jsfiddle.net/JfGVE/2029/

<span>
<button class="saveHome hoverPulse pan typeReversed">
    <span class="stackIcons">
        <i class="iconHeartActive iconOnly"></i>
<i class="iconHeartEmpty typeReversed iconOnly"></i> 
    </span>
</button>
</span> 



//CSS Code

button{
  width: 100px;
  height: 50px;
  position: relative;
}

 .iconHeartEmpty::before{
   content: "\f001";
    font-family: FontAwesome;
    font-style: normal;
    font-weight: normal;
    text-decoration: inherit;
/*--adjust as necessary--*/
    color: #000;
    font-size: 18px;
    padding-right: 0.5em;
    position: absolute;
    top: 35%;
    left: 5%;
 }
 .iconHeartActive::after{
      content: "\f001";
    font-family: FontAwesome;
    font-style: normal;
    font-weight: normal;
    text-decoration: inherit;
/*--adjust as necessary--*/
    color: red;
    font-size: 18px;
    padding-right: 0.5em;
    position: absolute;
    top: 35%;
    left: 5%;
 }
 .hide{
   display: none;
 }



//Script

 $( ".saveHome" ).click(function() {
   $(".stackIcons i" ).toggleClass( "hide" );
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM