How to keep :active css style after clicking an element

后端 未结 5 899
忘了有多久
忘了有多久 2020-12-03 01:52

I use anchor as my site navigation.

about
<
相关标签:
5条回答
  • 2020-12-03 01:59

    If you want to keep your links to look like they are :active class, you should define :visited class same as :active so if you have a links in .example then you do something like this:

    a.example:active, a.example:visited {
    /* Put your active state style code here */ }
    

    The Link visited Pseudo Class is used to select visited links as says the name.

    0 讨论(0)
  • 2020-12-03 02:01

    Combine JS & CSS :

    button{
      /* 1st state */
    }
    
    button:hover{
      /* hover state */
    }
    
    button:active{
      /* click state */
    }
    
    button.active{
      /* after click state */
    }
    
    
    jQuery('button').click(function(){
       jQuery(this).toggleClass('active');
    });
    
    0 讨论(0)
  • 2020-12-03 02:08

    You can use a little bit of Javascript to add and remove CSS classes of your navitems. For starters, create a CSS class that you're going to apply to the active element, name it ie: ".activeItem". Then, put a javascript function to each of your navigation buttons' onclick event which is going to add "activeItem" class to the one activated, and remove from the others...

    It should look something like this: (untested!)

    /*In your stylesheet*/
    .activeItem{
       background-color:#999; /*make some difference for the active item here */
    }
    
    
    /*In your javascript*/
    var prevItem = null;
    function activateItem(t){
       if(prevItem != null){
          prevItem.className = prevItem.className.replace(/{\b}?activeItem/, "");
       }
       t.className += " activeItem";
       prevItem = t;
    }
    
    <!-- And then your markup -->
    <div id='nav'>
    <a href='#abouts' onClick="activateItem(this)">
    <div class='navitem about'>
    about
    </div>
    </a>
    <a href='#workss' onClick="activateItem(this)">
    <div class='navitem works'>
    works
    </div>
    </a>
    </div>
    
    0 讨论(0)
  • 2020-12-03 02:13

    I FIGURED IT OUT. SIMPLE, EFFECTIVE NO jQUERY

    We're going to to be using a hidden checkbox.
    This example includes one "on click - off click 'hover / active' state"

    --

    To make content itself clickable:

    #activate-div{display:none}
    
    .my-div{background-color:#FFF}
    
    #activate-div:checked ~ label
    .my-div{background-color:#000}
    <input type="checkbox" id="activate-div">
    <label for="activate-div">
      <div class="my-div">
        //MY DIV CONTENT
      </div>
    </label>

    To make button change content:

    #activate-div{display:none}
    
    .my-div{background-color:#FFF}
    
    #activate-div:checked +
    .my-div{background-color:#000}
    <input type="checkbox" id="activate-div">
    <div class="my-div">
      //MY DIV CONTENT
    </div>
    
    <label for="activate-div">
      //MY BUTTON STUFF
    </label>

    Hope it helps!!

    0 讨论(0)
  • 2020-12-03 02:21

    The :target-pseudo selector is made for these type of situations: http://reference.sitepoint.com/css/pseudoclass-target

    It is supported by all modern browsers. To get some IE versions to understand it you can use something like Selectivizr

    Here is a tab example with :target-pseudo selector.

    0 讨论(0)
提交回复
热议问题