How do you create a toggle button?

后端 未结 15 2883
名媛妹妹
名媛妹妹 2020-11-27 11:46

I want to create a toggle button in html using css. I want it so that when you click on it , it stays pushed in and than when you click it on it again it pops out.

相关标签:
15条回答
  • 2020-11-27 12:17

    I don't think using JS for creating a button is good practice. What if the user's browser deactivates JavaScript ?

    Plus, you can just use a checkbox and a bit of CSS to do it. And it easy to retrieve the state of your checkbox.

    This is just one example, but you can style it how want.

    http://jsfiddle.net/4gjZX/

    HTML

    <fieldset class="toggle">
      <input id="data-policy" type="checkbox" checked="checked" />
      <label for="data-policy">
      <div class="toggle-button">
        <div class="toggle-tab"></div>
      </div>
      Toggle
      </label>
    </fieldset>​
    

    CSS

    .toggle label {
      color: #444;
      float: left;
      line-height: 26px;
    }
    .toggle .toggle-button {
      margin: 0px 10px 0px 0px;
      float: left;
      width: 70px;
      height: 26px;
      background-color: #eeeeee;
      background-image: -webkit-gradient(linear, left top, left bottom, from(#eeeeee), to(#fafafa));
      background-image: -webkit-linear-gradient(top, #eeeeee, #fafafa);
      background-image: -moz-linear-gradient(top, #eeeeee, #fafafa);
      background-image: -o-linear-gradient(top, #eeeeee, #fafafa);
      background-image: -ms-linear-gradient(top, #eeeeee, #fafafa);
      background-image: linear-gradient(top, #eeeeee, #fafafa);
      filter: progid:dximagetransform.microsoft.gradient(GradientType=0, StartColorStr='#eeeeee', EndColorStr='#fafafa');
      border-radius: 4px;
      -webkit-border-radius: 4px;
      -moz-border-radius: 4px;
      border: 1px solid #D1D1D1;
    }
    .toggle .toggle-button .toggle-tab {
      width: 30px;
      height: 26px;
      background-color: #fafafa;
      background-image: -webkit-gradient(linear, left top, left bottom, from(#fafafa), to(#eeeeee));
      background-image: -webkit-linear-gradient(top, #fafafa, #eeeeee);
      background-image: -moz-linear-gradient(top, #fafafa, #eeeeee);
      background-image: -o-linear-gradient(top, #fafafa, #eeeeee);
      background-image: -ms-linear-gradient(top, #fafafa, #eeeeee);
      background-image: linear-gradient(top, #fafafa, #eeeeee);
      filter: progid:dximagetransform.microsoft.gradient(GradientType=0, StartColorStr='#fafafa', EndColorStr='#eeeeee');
      border: 1px solid #CCC;
      margin-left: -1px;
      margin-top: -1px;
      border-radius: 4px;
      -webkit-border-radius: 4px;
      -moz-border-radius: 4px;
      -webkit-box-shadow: 5px 0px 4px -5px #000000, 0px 0px 0px 0px #000000;
      -moz-box-shadow: 5px 0px 4px -5px rgba(0, 0, 0, 0.3), 0px 0px 0px 0px #000000;
      box-shadow: 5px 0px 4px -5px rgba(0, 0, 0, 0.3), 0px 0px 0px 0px #000000;
    }
    .toggle input[type=checkbox] {
      display: none;
    }
    .toggle input[type=checkbox]:checked ~ label .toggle-button {
      background-color: #2d71c2;
      background-image: -webkit-gradient(linear, left top, left bottom, from(#2d71c2), to(#4ea1db));
      background-image: -webkit-linear-gradient(top, #2d71c2, #4ea1db);
      background-image: -moz-linear-gradient(top, #2d71c2, #4ea1db);
      background-image: -o-linear-gradient(top, #2d71c2, #4ea1db);
      background-image: -ms-linear-gradient(top, #2d71c2, #4ea1db);
      background-image: linear-gradient(top, #2d71c2, #4ea1db);
      filter: progid:dximagetransform.microsoft.gradient(GradientType=0, StartColorStr='#2d71c2', EndColorStr='#4ea1db');
    }
    .toggle input[type=checkbox]:checked ~ label .toggle-button .toggle-tab {
      margin-left: 39px;
      -webkit-box-shadow: -5px 0px 4px -5px #000000, 0px 0px 0px 0px #000000;
      -moz-box-shadow: -5px 0px 4px -5px rgba(0, 0, 0, 0.3), 0px 0px 0px 0px #000000;
      box-shadow: -5px 0px 4px -5px rgba(0, 0, 0, 0.3), 0px 0px 0px 0px #000000;
    }​
    

    Hope this helps

    0 讨论(0)
  • 2020-11-27 12:17

    Here is one of the example/variant (more detail described) of ToggleButton using jQuery with <label for="input"> implementation.

    1st we will create container for our ToggleButton using classic HTML <input> and <label>

    <span>
      <input type="checkbox" value="1" name="some_feature_to_select" id="feature_cb" style="display: none;"> <!-- We can hide checkbox bec. we want <label> to be a ToggleButton, so we don't need to show it. It is used as our value holder -->
      <label for="feature_cb" id="label_for_some_feature">
        <img alt="Stylish image" src="/images/icons/feature.png">
      </label>
    </span>
    

    Next we will define function for toggling our button. Our button actually is the usual <label> which we will be styling to represent value toggling.

    function toggleButton(button) {
    
    var _for = button.getAttribute('for'); // defining for which element this label is (suppose element is a checkbox (bec. we are making ToggleButton ;) )
    var _toggleID = 'input#'+_for; // composing selector ID string to select our toggle element (checkbox)
    var _toggle = $( _toggleID ); // selecting checkbox to work on
    var isChecked = !_toggle.is(':checked'); // defining the state with negation bec. change value event will have place later, so we negating current state to retrieve inverse (next).
    
    if (isChecked)
        $(button).addClass('SelectedButtonClass'); // if it is checked -> adding nice class to our button (<label> in our case) to show that value was toggled
    else
        $(button).removeClass('SelectedButtonClass'); // if value (or feature) was unselected by clicking the button (<label>) -> removing .SelectedButtonClass (or simply removing all classes from element)
    }
    

    Function is implemented in a reusable way. You can use it for more than one, two or even three ToggleButtons you've created.

    ... and finally ... to make it work as expected, we should bind toggle function to an event ("change" event) of our improvised <label> button (it will be click event bec. we are not altering the checkbox directly, so no change event can be fired for <label>).

    $(document).ready(function(){
    
        $("#some_feature_label").click(function () {
            toggleButton(this); // call function with transmitting instance of a clicked label and let the script decide what was toggled and what to do with it
        });
    
        $("#some_other_feature_label").click(function () {
            toggleButton(this); // doing the same for any other feature we want to represent in a way of ToggleButton
        });
    
    });
    

    With CSS we can define backgorund-image or some border to represent the change in value whilst <label> will do the job for us in altering the value of a checkbox ;).

    Hope this helps someone.

    0 讨论(0)
  • 2020-11-27 12:18

    In combination with this answer, you can also use this kind of style that is like mobile settings toggler.

    togglers

    HTML

    <a href="#" class="toggler">&nbsp;</a>
    <a href="#" class="toggler off">&nbsp;</a>
    <a href="#" class="toggler">&nbsp;</a>
    

    CSS

    a.toggler {
        background: green;
        cursor: pointer;
        border: 2px solid black;
        border-right-width: 15px;
        padding: 0 5px;
        border-radius: 5px;
        text-decoration: none;
        transition: all .5s ease;
    }
    
    a.toggler.off {
        background: red;
        border-right-width: 2px;
        border-left-width: 15px;
    }
    

    jQuery

    $(document).ready(function(){
        $('a.toggler').click(function(){
            $(this).toggleClass('off');
        });
    });
    

    Could be much prettier, but gives the idea.
    One advantage is that it can be animated with CSS
    Fiddler

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