如何在CSS中使用这种Button?

时间:2015-10-23 09:13:13

标签: css button css-shapes

我一直在互联网上搜索一段时间没找到,所以我转向你们。

我想知道如何使用CSS(开/关按钮示例)来执行此类按钮:

Button On/Off

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

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;
}

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

5 个答案:

答案 0 :(得分:2)

使用2个元素很容易实现,并使用伪元素获取下面的箭头位置。

它不需要完成大量的HTML / CSS,并且可以轻松更改为输入或<a>标记。无论您的要求是什么。

&#13;
&#13;
$(document).ready(function() {
  $('.btn').click(function() {
    $('.btn').toggleClass('active');
  });
});
&#13;
.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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

$(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>

答案 2 :(得分:0)

你走了: 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>

答案 3 :(得分:0)

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

html

PassportJS

CSS

<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>

答案 4 :(得分:0)

  <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; }