【问题标题】:How to do this kind of Button in CSS?如何在 CSS 中做这种 Button?
【发布时间】:2015-11-06 21:47:35
【问题描述】:

我已经在互联网上搜索了一段时间,但一无所获,所以我向你们求助。

我想知道如何使用 CSS 制作这样的按钮(开/关按钮示例):

我已经尝试过这样的事情:

HTML:

                <a class="button_tooltip" href="#">On</a>
                <a class="button_tooltip" href="#">Off</a>

CSS:

a {
    color: #76daff;
    display: inline-block;
    padding: 8px 16px;
    position: relative;
    text-decoration: none;
}
a {
    color: #76daff;
}
a.button_tooltip {
    background: #ccc none repeat scroll 0 0;
    color: black;
}
a.button_tooltip::after {
    border-top-color: #cccccc;
}
a.button_tooltip::after {
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    border-top: 8px solid #ccc;
    content: "";
    margin-left: -8px;
}
a.button_tooltip {
    background: #cccccc none repeat scroll 0 0;
    color: #000000;
}
a.button_tooltip::after {
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    border-top: 8px solid #ccc;
    content: "";
}
a.button_tooltip::after {
    border-top-color: #cccccc;
}

但只给了我下面没有小三角形的正方形。

【问题讨论】:

  • 您还应该提供您的标记:这些是链接/列表项/按钮吗?

标签: css button css-shapes


【解决方案1】:

这很容易通过 2 个元素和使用伪元素来显示下方的箭头。

它不需要大量的 HTML/CSS 即可完成,并且可以轻松更改为作为输入或 &lt;a&gt; 标签工作。无论您的要求是什么。

$(document).ready(function() {
  $('.btn').click(function() {
    $('.btn').toggleClass('active');
  });
});
.container {
  width: 200px;
  height: 100px;
}
.btn {
  width: 100px;
  height: 100px;
  box-sizing: border-box;
  border: 1px solid blue;
  float: left;
  cursor: pointer;
}
.active {
  background: blue;
  position: relative;
}
.active:before {
  content: '';
  position: absolute;
  width: 50px;
  height: 50px;
  transform: rotate(45deg);
  bottom: -10px;
  left: 25px;
  background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="btn"></div>
  <div class="btn active"></div>
</div>

【讨论】:

    【解决方案2】:

    $(document).ready(function() {
      $('.toggle-btn button').click(function() {
        $(this).parent('.toggle-btn').children('button').removeClass('active');
        $(this).addClass('active');
      });
    });
    .toggle-btn {
      border: 1px solid #4c8cca;
      display: inline-block;
      line-height: 1;
      width: 100px;
    }
    
    button {
      line-height: 1;
      float: left;
      height: 30px;
      background: none;
      background-color: transparent;
      border: none;
      padding: 0;
      position: relative;
      outline: 0;
      width: 50%;
      cursor: pointer;
    }
    
    button:hover {
      background-color: #EEE;
    }
    
    button.active {
      background: #4c8cca;
      background-color: #4c8cca; 
      color: #FFF;
    }
    
    button.active:after {
      content: '';
      position: absolute;
      width: 10px;
      height: 10px;
      top: 25px;
      transform: rotate(45deg);
      background-color: #4c8cca;
      left: 50%;
      margin-left: -4px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="toggle-btn">
      <button>Yes</button>
      <button class="active">No</button>
    </div>

    【讨论】:

      【解决方案3】:

      给你: http://jsfiddle.net/es_kaija/negzqemp/

      <div class="switch off">
          <div class="toggle"></div>
      <span class="on">ON</span>
      <span class="off">OFF</span>
      </div>
      
      
      <style>
      .switch {
      position: relative;
      display: inline-block;
      font-size: 1em;
      font-weight: bold;
      color: #ccc;
      text-shadow: 0px 1px 1px rgba(255,255,255,0.8);
      height: 18px;
      padding: 6px 6px 5px 6px;
      border: 1px solid #ccc;
      border: 1px solid rgba(0,0,0,0.2);
      border-radius: 4px;
      background: #ececec;
      box-shadow: 0px 0px 4px rgba(0,0,0,0.1), inset 0px 1px 3px 0px rgba(0,0,0,0.1);
      cursor: pointer;
      }
      
      .switch span { display: inline-block; width: 35px; }
      .switch span.On { color: #33d2da; }
      
      .switch .toggle {
      position: absolute;
      top: 1px;
      width: 37px;
      height: 25px;
      border: 1px solid #ccc;
      border: 1px solid rgba(0,0,0,0.3);
      border-radius: 4px;
      background: #fff;
      background: -moz-linear-gradient(top,  #ececec 0%, #ffffff 100%);
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ececec), color-stop(100%,#ffffff));
      background: -webkit-linear-gradient(top,  #ececec 0%,#ffffff 100%);
      background: -o-linear-gradient(top,  #ececec 0%,#ffffff 100%);
      background: -ms-linear-gradient(top,  #ececec 0%,#ffffff 100%);
      background: linear-gradient(top,  #ececec 0%,#ffffff 100%);
      
      box-shadow: inset 0px 1px 0px 0px rgba(255,255,255,0.5);
      z-index: 999;
      
      -webkit-transition: all 0.15s ease-in-out;
      -moz-transition: all 0.15s ease-in-out;
      -o-transition: all 0.15s ease-in-out;
      -ms-transition: all 0.15s ease-in-out;
      }
      
      .switch.on .toggle { left: 2%; }
      .switch.off .toggle { left: 54%; }
      
      
      </style>
      
      <script>
      $(document).ready(function() {
      
          // Switch toggle
          $('.switch').click(function() {
              $(this).toggleClass('on').toggleClass('off');
          });
      
      });
      </script>
      

      【讨论】:

        【解决方案4】:

        您可以使用 css 本身来做到这一点。这是示例 - https://jsfiddle.net/p653bpbe/

        html

        <div class="onoffswitch">
            <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
            <label class="onoffswitch-label" for="myonoffswitch">
                <span class="onoffswitch-inner"></span>
                <span class="onoffswitch-switch"></span>
            </label>
        </div>
        

        css

        .onoffswitch {
            position: relative; width: 90px;
            -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
        }
        .onoffswitch-checkbox {
            display: none;
        }
        .onoffswitch-label {
            display: block; overflow: hidden; cursor: pointer;
            border: 1px solid #2E8DEF; border-radius: 0px;
        }
        .onoffswitch-inner {
            display: block; width: 200%; margin-left: -100%;
            transition: margin 0.3s ease-in 0s;
        }
        .onoffswitch-inner:before, .onoffswitch-inner:after {
            display: block; float: left; width: 50%; height: 30px; padding: 0; line-height: 26px;
            font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
            box-sizing: border-box;
            border: 2px solid transparent;
            background-clip: padding-box;
        }
        .onoffswitch-inner:before {
            content: "";
            padding-left: 10px;
            background-color: #FFFFFF; color: #FFFFFF;
        }
        .onoffswitch-inner:after {
            content: "";
            padding-right: 10px;
            background-color: #FFFFFF; color: #333333;
            text-align: right;
        }
        .onoffswitch-switch {
            display: block; width: 45px; margin: 0px;
            background: #2E8DEF;
            position: absolute; top: 0; bottom: 0;
        
            transition: all 0.3s ease-in 0s; 
        }
        .onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
            margin-left: 0;
        }
        .onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
            right: 0px; 
        }
        

        【讨论】:

          【解决方案5】:
            <a href="#">Button</a>
          
            a { font-size: 0; line-height: 0; text-indent: -4000px; background: #fff; border: 1px solid #007bff; width: 30px; height: 30px; display: block; position: relative; }
            a:after { position: absolute; top: -1px; left: 30px; width: 30px; height: 30px; content: ''; display: block; background: #007bff; border: 1px solid #007bff; }
            a:before { content: ''; display: block; width: 0; height: 0; border-style: solid; border-width: 10px 10px 0 10px; border-color: #007bff transparent transparent transparent; position: absolute; bottom: -10px; right: -25px; }
          

          【讨论】:

            猜你喜欢
            • 2021-01-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-09-03
            • 1970-01-01
            相关资源
            最近更新 更多