Can I have an onclick effect in CSS?

后端 未结 12 1783
悲&欢浪女
悲&欢浪女 2020-11-21 06:42

I have an image element that I want to change on click.


This works:

#btnLeft:hover {
    width:7         


        
相关标签:
12条回答
  • 2020-11-21 07:29

    you can use :target

    or to filter by class name, use .classname:target

    or filter by id name using #idname:target

    #id01:target {      
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .msg {
        display:none;
    }
    
    .close {        
        color:white;        
        width: 2rem;
        height: 2rem;
        background-color: black;
        text-align:center;
        margin:20px;
    }
      
    <a href="#id01">Open</a>
    
    <div id="id01" class="msg">    
        <a href="" class="close">&times;</a>
        <p>Some text. Some text. Some text.</p>
        <p>Some text. Some text. Some text.</p>
    </div>

    0 讨论(0)
  • 2020-11-21 07:33

    I had a problem with an element which had to be colored RED on hover and be BLUE on click while being hovered. To achieve this with css you need for example:

    h1:hover { color: red; } 
    h1:active { color: blue; }
    
    <h1>This is a heading.</h1>
    

    I struggled for some time until I discovered that the order of CSS selectors was the problem I was having. The problem was that I switched the places and the active selector was not working. Then I found out that :hover to go first and then :active.

    0 讨论(0)
  • 2020-11-21 07:35

    Answer as of 2020:

    The best way (actually the only way*) to simulate an actual click event using only CSS (rather than just hovering on an element or making an element active, where you don't have mouseUp) is to use the checkbox hack. It works by attaching a label to an <input type="checkbox"> element via the label's for="" attribute.

    This feature has broad browser support (the :checked pseudo-class is IE9+).

    Apply the same value to an <input>'s ID attribute and an accompanying <label>'s for="" attribute, and you can tell the browser to re-style the label on click with the :checked pseudo-class, thanks to the fact that clicking a label will check and uncheck the "associated" <input type="checkbox">.

    * You can simulate a "selected" event via the :active or :focus pseudo-class in IE7+ (e.g. for a button that's normally 50px wide, you can change its width while active: #btnControl:active { width: 75px; }), but those are not true "click" events. They are "live" the entire time the element is selected (such as by Tabbing with your keyboard), which is a little different from a true click event, which fires an action on - typically - mouseUp.


    Basic demo of the checkbox hack (the basic code structure for what you're asking):

    label {
        display: block;
        background: lightgrey;
        width: 100px;
        height: 100px;
    }
    
    #demo:checked + label {
        background: blue;
        color: white;
    }
    <input type="checkbox" id="demo"/>
    <label for="demo">I'm a square. Click me.</label>

    Here I've positioned the label right after the input in my markup. This is so that I can use the adjacent sibling selector (the + key) to select only the label that immediately follows my #demo checkbox. Since the :checked pseudo-class applies to the checkbox, #demo:checked + label will only apply when the checkbox is checked.

    Demo for re-sizing an image on click, which is what you're asking:

    #btnControl {
        display: none;
    }
    
    #btnControl:checked + label > img {
        width: 70px;
        height: 74px;
    }
    <input type="checkbox" id="btnControl"/>
    <label class="btn" for="btnControl"><img src="https://placekitten.com/200/140" id="btnLeft" /></label>

    With that being said, there is some bad news. Because a label can only be associated with one form control at a time, that means you can't just drop a button inside the <label></label> tags and call it a day. However, we can use some CSS to make the label look and behave fairly close to how an HTML button looks and behaves.

    Demo for imitating a button click effect, above and beyond what you're asking:

    #btnControl {
        display: none;
    }
    
    .btn {
        width: 60px;
        height: 20px;
        background: silver;
        border-radius: 5px;
        padding: 1px 3px;
        box-shadow: 1px 1px 1px #000;
        display: block;
        text-align: center;
        background-image: linear-gradient(to bottom, #f4f5f5, #dfffffdd);
        font-family: arial;
        font-size: 12px;
        line-height:20px;
    }
    
    .btn:hover {
        background-image: linear-gradient(to bottom, #c3e3fa, #a5defb);
    }
    
    
    .btn:active {
        margin-left: 1px 1px 0;
        box-shadow: -1px -1px 1px #000;
        outline: 1px solid black;
        -moz-outline-radius: 5px;
        background-image: linear-gradient(to top, #f4f5f5, #dfffffdd);
    }
    
    #btnControl:checked + label {
        width: 70px;
        height: 74px;
        line-height: 74px;
    }
    <input type="checkbox" id="btnControl"/>
    <label class="btn" for="btnControl">Click me!</label>

    Most of the CSS in this demo is just for styling the label element. If you don't actually need a button, and any old element will suffice, then you can remove almost all of the styles in this demo, similar to my second demo above.

    You'll also notice I have one prefixed property in there, -moz-outline-radius. A while back, Mozilla added this awesome non-spec property to Firefox, but the folks at WebKit decided they aren't going to add it, unfortunately. So consider that line of CSS just a progressive enhancement for people who use Firefox.

    0 讨论(0)
  • 2020-11-21 07:36

    You can use pseudo class :target to mimic on click event, let me give you an example.

    #something {
      display: none;
    }
    
    #something:target {
      display: block;
    }
    <a href="#something">Show</a>
    <div id="something">Bingo!</div>

    Here's how it looks like: http://jsfiddle.net/TYhnb/

    One thing to note, this is only limited to hyperlink, so if you need to use on other than hyperlink, such as a button, you might want to hack it a little bit, such as styling a hyperlink to look like a button.

    0 讨论(0)
  • 2020-11-21 07:39

    TylerH made a really good answer, I just had to give that last button a visual update.

    .btn {
        border-radius: 5px;
        padding: 10px 30px;
        box-shadow: 1px 1px 1px #000;
        background-image: linear-gradient(to bottom, #eee, #ffffd);
    }
    
    .btn:hover {
        background-image: linear-gradient(to top, #adf, #8bf);
    }
    
    .btn:active {
        margin: 1px 1px 0;
        box-shadow: -1px -1px 1px #000;
    }
    
    #btnControl {
        display: block;
        visibility: hidden;
    }
    <input type="checkbox" id="btnControl"/>
    <label class="btn" for="btnControl">Click me!</label>

    0 讨论(0)
  • 2020-11-21 07:39

    How about a pure CSS solution without being (that) hacky?

    .page {
      position: fixed;
      top: 0;
      bottom: 0;
      right: 0;
      left: 0;
      background-color: #121519;
      color: whitesmoke;
    }
    
    .controls {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100%;
      width: 100%;
    }
    
    .arrow {
      cursor: pointer;
      transition: filter 0.3s ease 0.3s;
    }
    
    .arrow:active {
      filter: drop-shadow(0 0 0 steelblue);
      transition: filter 0s;
    }
    <body class="page">
      <div class="controls">
        <div class="arrow">
          <img src="https://i.imgur.com/JGUoNfS.png" />
        </div>
      </div>
    </body>

    @TylerH has a great response but its a pretty complex solution. I have a solution for those of you that just want a simple "onclick" effect with pure css without a bunch of extra elements.

    We will simply use css transitions. You could probably do similar with animations.

    The trick is to change the delay for the transition so that it will last when the user clicks.

    .arrowDownContainer:active,
    .arrowDownContainer.clicked {
      filter: drop-shadow(0px 0px 0px steelblue);
      transition: filter 0s;
    }
    

    Here I add the "clicked" class as well so that javascript can also provide the effect if it needs to. I use 0px drop-shadow filter because it will highlight the given transparent graphic blue this way for my case.

    I have a filter at 0s here so that it wont take effect. When the effect is released I can then add the transition with a delay so that it will provide a nice "clicked" effect.

    .arrowDownContainer {
      cursor: pointer;
      position: absolute;
      bottom: 0px;
      top: 490px;
      left: 108px;
      height: 222px;
      width: 495px;
      z-index: 3;
      transition: filter 0.3s ease 0.3s;
    }
    

    This allows me to set it up so that when the user clicks the button, it highlights blue then fades out slowly (you could, of course, use other effects as well).

    While you are limited here in the sense that the animation to highlight is instant, it does still provide the desired effect. You could likely use this trick with animation to produce a smoother overall transition.

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