点击后如何使按钮凹陷?

时间:2021-08-15 12:45:22

I have a multiple set of buttons which are clicked to show that they are selected.I have already successfully achieved that.But the problem is the buttons rise again after mouse up.

我有一组多个按钮,点击显示它们已被选中。我已经成功实现了这一点。但问题是鼠标按下后按钮再次上升。

I need the buttons to stay pressed after the mouse leaves them.

我需要按钮在鼠标离开后保持按下状态。

Screenshot :

点击后如何使按钮凹陷?

Code :

.button {
  position: absolute;
  top: 100px;
  left: 200px;
  display: inline-block;
  margin: 0 auto;
  -webkit-border-radius: 10px;
  -webkit-box-shadow: 0px 3px rgba(128, 128, 128, 1),
  /* gradient effects */
  0px 4px rgba(118, 118, 118, 1), 0px 5px rgba(108, 108, 108, 1), 0px 6px rgba(98, 98, 98, 1), 0px 7px rgba(88, 88, 88, 1), 0px 8px rgba(78, 78, 78, 1), 0px 14px 6px -1px rgba(128, 128, 128, 1);
  /* shadow */
  -webkit-transition: -webkit-box-shadow .1s ease-in-out;
}
.button span {
  background-color: #E8E8E8;
  background-image:
  /* gloss gradient */
  -webkit-gradient(linear, left bottom, left top, color-stop(50%, rgba(255, 255, 255, 0)), color-stop(50%, rgba(255, 255, 255, 0.3)), color-stop(100%, rgba(255, 255, 255, 0.2))),
  /* dark outside gradient */
  -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(210, 210, 210, 0.3)), color-stop(20%, rgba(210, 210, 210, 0)), color-stop(80%, rgba(210, 210, 210, 0)), color-stop(100%, rgba(210, 210, 210, 0.3))),
  /* light inner gradient */
  -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(20%, rgba(255, 255, 255, 0.5)), color-stop(80%, rgba(255, 255, 255, 0.5)), color-stop(100%, rgba(255, 255, 255, 0))),
  /* diagonal line pattern */
  -webkit-gradient(linear, 0% 100%, 100% 0%, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(40%, rgba(255, 255, 255, 0)), color-stop(40%, #D2D2D1), color-stop(60%, #D2D2D1), color-stop(60%, rgba(255, 255, 255, 0)), color-stop(100%, rgba(255, 255, 255, 0)));
  -webkit-box-shadow: 0px -1px #fff,
  /* top highlight */
  0px 1px 1px #FFFFFF;
  /* bottom edge */
  -webkit-background-size: 100%, 100%, 100%, 4px 4px;
  -webkit-border-radius: 10px;
  -webkit-transition: -webkit-transform .1s ease-in-out;
  display: inline-block;
  padding: 10px 40px 10px 40px;
  color: #3A474D;
  text-transform: uppercase;
  font-family: 'TradeGothicLTStd-BdCn20', 'PT Sans Narrow';
  font-weight: 700;
  font-size: 32px;
  text-shadow: 0px 1px #fff, 0px -1px #262F33;
}
.button span:hover {
  color: #AEBF3B;
  text-shadow: 0px -1px #97A63A;
  cursor: pointer;
}
.button:active {
  -webkit-box-shadow: 0px 3px rgba(128, 128, 128, 1), 0px 4px rgba(118, 118, 118, 1), 0px 5px rgba(108, 108, 108, 1), 0px 6px rgba(98, 98, 98, 1), 0px 7px rgba(88, 88, 88, 1), 0px 8px rgba(78, 78, 78, 1), 0px 10px 2px 0px rgba(128, 128, 128, .6);
  /* shadow */
}
.button:active span {
  -webkit-transform: translate(0, 5px);
  /* depth of button press */
}
.button span:after {
  content: ">";
  display: block;
  width: 10px;
  height: 10px;
  position: absolute;
  right: 14px;
  top: 12px;
  font-family: 'Cabin';
  font-weight: 700;
  color: #AEBF3B;
  text-shadow: 0px 1px #fff, 0px -1px #97A63A;
  font-size: 26px;
}
<link href="http://fonts.googleapis.com/css?family=Cabin:400,500,600,bold" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=PT+Sans+Narrow:regular,bold" rel="stylesheet" type="text/css">

<a class="button">
  <span>select me </span>
</a>

3 个解决方案

#1


3  

You can find the full code below for a version based on jQuery, one based on vanilla JS and one using only HTML & CSS to achieve the desired effect.

您可以在下面找到基于jQuery的版本的完整代码,一个基于vanilla JS的版本和一个仅使用HTML和CSS来实现所需效果的版本。

You can also play around with the code at the following Fiddles:

您还可以使用以下Fiddles中的代码:


jQuery version :

HTML :

<link href="http://fonts.googleapis.com/css?family=Cabin:400,500,600,bold" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=PT+Sans+Narrow:regular,bold" rel="stylesheet" type="text/css">

<a class="button">
  <span>select me </span>
</a>

CSS :

   .button {
  position: absolute;
  top: 100px;
  left: 200px;
  display: inline-block;
  margin: 0 auto;
  -webkit-border-radius: 10px;
  -webkit-box-shadow: 0px 3px rgba(128, 128, 128, 1),
  /* gradient effects */
  0px 4px rgba(118, 118, 118, 1), 0px 5px rgba(108, 108, 108, 1), 0px 6px rgba(98, 98, 98, 1), 0px 7px rgba(88, 88, 88, 1), 0px 8px rgba(78, 78, 78, 1), 0px 14px 6px -1px rgba(128, 128, 128, 1);
  /* shadow */
  -webkit-transition: -webkit-box-shadow .1s ease-in-out;
}

.button span {
  background-color: #E8E8E8;
  background-image:
  /* gloss gradient */
  -webkit-gradient(linear, left bottom, left top, color-stop(50%, rgba(255, 255, 255, 0)), color-stop(50%, rgba(255, 255, 255, 0.3)), color-stop(100%, rgba(255, 255, 255, 0.2))),
  /* dark outside gradient */
  -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(210, 210, 210, 0.3)), color-stop(20%, rgba(210, 210, 210, 0)), color-stop(80%, rgba(210, 210, 210, 0)), color-stop(100%, rgba(210, 210, 210, 0.3))),
  /* light inner gradient */
  -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(20%, rgba(255, 255, 255, 0.5)), color-stop(80%, rgba(255, 255, 255, 0.5)), color-stop(100%, rgba(255, 255, 255, 0))),
  /* diagonal line pattern */
  -webkit-gradient(linear, 0% 100%, 100% 0%, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(40%, rgba(255, 255, 255, 0)), color-stop(40%, #D2D2D1), color-stop(60%, #D2D2D1), color-stop(60%, rgba(255, 255, 255, 0)), color-stop(100%, rgba(255, 255, 255, 0)));
  -webkit-box-shadow: 0px -1px #fff,
  /* top highlight */
  0px 1px 1px #FFFFFF;
  /* bottom edge */
  -webkit-background-size: 100%, 100%, 100%, 4px 4px;
  -webkit-border-radius: 10px;
  -webkit-transition: -webkit-transform .1s ease-in-out;
  display: inline-block;
  padding: 10px 40px 10px 40px;
  color: #3A474D;
  text-transform: uppercase;
  font-family: 'TradeGothicLTStd-BdCn20', 'PT Sans Narrow';
  font-weight: 700;
  font-size: 32px;
  text-shadow: 0px 1px #fff, 0px -1px #262F33;
}

.button span:hover {
  color: #AEBF3B;
  text-shadow: 0px -1px #97A63A;
  cursor: pointer;
}

.button.pressed {
  -webkit-box-shadow: 0px 3px rgba(128, 128, 128, 1), 0px 4px rgba(118, 118, 118, 1), 0px 5px rgba(108, 108, 108, 1), 0px 6px rgba(98, 98, 98, 1), 0px 7px rgba(88, 88, 88, 1), 0px 8px rgba(78, 78, 78, 1), 0px 10px 2px 0px rgba(128, 128, 128, .6);
  /* shadow */
}

.button.pressed span {
  -webkit-transform: translate(0, 5px);
  /* depth of button press */
}

.button span:after {
  content: ">";
  display: block;
  width: 10px;
  height: 10px;
  position: absolute;
  right: 14px;
  top: 12px;
  font-family: 'Cabin';
  font-weight: 700;
  color: #AEBF3B;
  text-shadow: 0px 1px #fff, 0px -1px #97A63A;
  font-size: 26px;
}

JS :

$('.button').click(function(){
    $(this).toggleClass('pressed');
});

JS version without jQuery :

HTML :

<link href="http://fonts.googleapis.com/css?family=Cabin:400,500,600,bold" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=PT+Sans+Narrow:regular,bold" rel="stylesheet" type="text/css">

<a class="button">
  <span>select me </span>
</a>

CSS :

 .button {
  position: absolute;
  top: 100px;
  left: 200px;
  display: inline-block;
  margin: 0 auto;
  -webkit-border-radius: 10px;
  -webkit-box-shadow: 0px 3px rgba(128, 128, 128, 1),
  /* gradient effects */
  0px 4px rgba(118, 118, 118, 1), 0px 5px rgba(108, 108, 108, 1), 0px 6px rgba(98, 98, 98, 1), 0px 7px rgba(88, 88, 88, 1), 0px 8px rgba(78, 78, 78, 1), 0px 14px 6px -1px rgba(128, 128, 128, 1);
  /* shadow */
  -webkit-transition: -webkit-box-shadow .1s ease-in-out;
}

.button span {
  background-color: #E8E8E8;
  background-image:
  /* gloss gradient */
  -webkit-gradient(linear, left bottom, left top, color-stop(50%, rgba(255, 255, 255, 0)), color-stop(50%, rgba(255, 255, 255, 0.3)), color-stop(100%, rgba(255, 255, 255, 0.2))),
  /* dark outside gradient */
  -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(210, 210, 210, 0.3)), color-stop(20%, rgba(210, 210, 210, 0)), color-stop(80%, rgba(210, 210, 210, 0)), color-stop(100%, rgba(210, 210, 210, 0.3))),
  /* light inner gradient */
  -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(20%, rgba(255, 255, 255, 0.5)), color-stop(80%, rgba(255, 255, 255, 0.5)), color-stop(100%, rgba(255, 255, 255, 0))),
  /* diagonal line pattern */
  -webkit-gradient(linear, 0% 100%, 100% 0%, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(40%, rgba(255, 255, 255, 0)), color-stop(40%, #D2D2D1), color-stop(60%, #D2D2D1), color-stop(60%, rgba(255, 255, 255, 0)), color-stop(100%, rgba(255, 255, 255, 0)));
  -webkit-box-shadow: 0px -1px #fff,
  /* top highlight */
  0px 1px 1px #FFFFFF;
  /* bottom edge */
  -webkit-background-size: 100%, 100%, 100%, 4px 4px;
  -webkit-border-radius: 10px;
  -webkit-transition: -webkit-transform .1s ease-in-out;
  display: inline-block;
  padding: 10px 40px 10px 40px;
  color: #3A474D;
  text-transform: uppercase;
  font-family: 'TradeGothicLTStd-BdCn20', 'PT Sans Narrow';
  font-weight: 700;
  font-size: 32px;
  text-shadow: 0px 1px #fff, 0px -1px #262F33;
}

.button span:hover {
  color: #AEBF3B;
  text-shadow: 0px -1px #97A63A;
  cursor: pointer;
}

.button.pressed {
  -webkit-box-shadow: 0px 3px rgba(128, 128, 128, 1), 0px 4px rgba(118, 118, 118, 1), 0px 5px rgba(108, 108, 108, 1), 0px 6px rgba(98, 98, 98, 1), 0px 7px rgba(88, 88, 88, 1), 0px 8px rgba(78, 78, 78, 1), 0px 10px 2px 0px rgba(128, 128, 128, .6);
  /* shadow */
}

.button.pressed span {
  -webkit-transform: translate(0, 5px);
  /* depth of button press */
}

.button span:after {
  content: ">";
  display: block;
  width: 10px;
  height: 10px;
  position: absolute;
  right: 14px;
  top: 12px;
  font-family: 'Cabin';
  font-weight: 700;
  color: #AEBF3B;
  text-shadow: 0px 1px #fff, 0px -1px #97A63A;
  font-size: 26px;
}

JS :

var buttons = document.getElementsByClassName("button");
[].forEach.call(buttons, function(button) {
    button.addEventListener("click", function(e) {
        this.classList.toggle('pressed');        
    });
});

HTML/CSS only version :

HTML :

<link href="http://fonts.googleapis.com/css?family=Cabin:400,500,600,bold" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=PT+Sans+Narrow:regular,bold" rel="stylesheet" type="text/css">

<button class="button">
<input type="checkbox" id="selectme" />
<span>
  <label for="selectme">select me </label>
</span>
</button>

CSS :

.button {
  margin-left: -5000px;
}

.button span {
  position: absolute;
  top: 100px;
  left: 200px;
  display: inline-block;
  margin: 0 auto;
  -webkit-border-radius: 10px;
  -webkit-box-shadow: 0px 3px rgba(128, 128, 128, 1),
  /* gradient effects */
  0px 4px rgba(118, 118, 118, 1), 0px 5px rgba(108, 108, 108, 1), 0px 6px rgba(98, 98, 98, 1), 0px 7px rgba(88, 88, 88, 1), 0px 8px rgba(78, 78, 78, 1), 0px 14px 6px -1px rgba(128, 128, 128, 1);
  /* shadow */
  -webkit-transition: -webkit-box-shadow .1s ease-in-out;
}
.button label {
  background-color: #E8E8E8;
  background-image:
  /* gloss gradient */
  -webkit-gradient(linear, left bottom, left top, color-stop(50%, rgba(255, 255, 255, 0)), color-stop(50%, rgba(255, 255, 255, 0.3)), color-stop(100%, rgba(255, 255, 255, 0.2))),
  /* dark outside gradient */
  -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(210, 210, 210, 0.3)), color-stop(20%, rgba(210, 210, 210, 0)), color-stop(80%, rgba(210, 210, 210, 0)), color-stop(100%, rgba(210, 210, 210, 0.3))),
  /* light inner gradient */
  -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(20%, rgba(255, 255, 255, 0.5)), color-stop(80%, rgba(255, 255, 255, 0.5)), color-stop(100%, rgba(255, 255, 255, 0))),
  /* diagonal line pattern */
  -webkit-gradient(linear, 0% 100%, 100% 0%, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(40%, rgba(255, 255, 255, 0)), color-stop(40%, #D2D2D1), color-stop(60%, #D2D2D1), color-stop(60%, rgba(255, 255, 255, 0)), color-stop(100%, rgba(255, 255, 255, 0)));
  -webkit-box-shadow: 0px -1px #fff,
  /* top highlight */
  0px 1px 1px #FFFFFF;
  /* bottom edge */
  -webkit-background-size: 100%, 100%, 100%, 4px 4px;
  -webkit-border-radius: 10px;
  -webkit-transition: -webkit-transform .1s ease-in-out;
  display: inline-block;
  padding: 10px 40px 10px 40px;
  color: #3A474D;
  text-transform: uppercase;
  font-family: 'TradeGothicLTStd-BdCn20', 'PT Sans Narrow';
  font-weight: 700;
  font-size: 32px;
  text-shadow: 0px 1px #fff, 0px -1px #262F33;
}
.button label:hover {
  color: #AEBF3B;
  text-shadow: 0px -1px #97A63A;
  cursor: pointer;
}

.button input:checked + span {
  -webkit-box-shadow: 0px 3px rgba(128, 128, 128, 1), 0px 4px rgba(118, 118, 118, 1), 0px 5px rgba(108, 108, 108, 1), 0px 6px rgba(98, 98, 98, 1), 0px 7px rgba(88, 88, 88, 1), 0px 8px rgba(78, 78, 78, 1), 0px 10px 2px 0px rgba(128, 128, 128, .6);
  /* shadow */
}

.button input:checked + span label {
  -webkit-transform: translate(0, 5px);
  /* depth of button press */
}

.button label:after {
  content: ">";
  display: block;
  width: 10px;
  height: 10px;
  position: absolute;
  right: 14px;
  top: 12px;
  font-family: 'Cabin';
  font-weight: 700;
  color: #AEBF3B;
  text-shadow: 0px 1px #fff, 0px -1px #97A63A;
  font-size: 26px;
}

JS :

/* CSS only means that no JS code is required at all! */

#2


3  

You can achieve it without JavaScript. Try :checked pseudo class and adjacent sibling selector (+):

你可以在没有JavaScript的情况下实现它尝试:检查伪类和相邻的兄弟选择器(+):

label {
  width: 120px;
  height: 40px;
  background: #d00;
  display: inline-block;
  text-align: center;
  line-height: 40px;
  cursor: pointer;
  color: white;
}
#checkbox {
  display: none;
}
#checkbox:checked + label {
  background: #600;
}
<input type="checkbox" id="checkbox">
<label for="checkbox">select me </label>

I also did a little change to your fiddle

我也对你的小提琴做了一点改动

You can also make nice look single selection:

你也可以看看单一选择:

label {
  width: 120px;
  height: 40px;
  background: #d00;
  display: inline-block;
  text-align: center;
  line-height: 40px;
  cursor: pointer;
  color: white;
}
input {
  display: none;
}
input:checked + label {
  background: #600;
}
<input type="radio" name="group" id="option1">
<label for="option1">select 1</label>
<input type="radio" name="group" id="option2">
<label for="option2">select 2</label>
<input type="radio" name="group" id="option3">
<label for="option3">select 3</label>
<input type="radio" name="group" id="option4">
<label for="option4">select 4</label>

#3


3  

First, decorate your css styles that use :active attribute with an alias class like

首先,装饰你使用的css样式:带有别名类的active属性

.button:active, .button-active{
...
}

.button:active span, .button-active span{
...
}

then make a onclick event handler which manually toggles the class

然后创建一个onclick事件处理程序,手动切换类

function toggleActive(element){
   element.classList.toggle('button-active');
}

function toggleActive(element){
   element.classList.toggle('button-active');
}
body {
    background: #A7A9AC;
    background-image: -webkit-gradient(
        linear,
        left top,
        right top,
        color-stop(.34, rgba(230,237,241,.05)),
        color-stop(.67, rgba(230,237,241,0)));
    -webkit-background-size: 5px 5px;
}

#pagegradient {
  background-image:
        -webkit-gradient(
            radial, 
            50% -50, 
            300, 
            50% 0, 
            0, 
            from(rgba(230, 237, 241, 0)), 
            to(rgba(230, 237, 241, 0.8)));
    
  height:100%;
  left:0px;
  position:absolute;
  top:0;
  width: 600px;
}

.button {
    position: absolute;
    top: 100px;
    left: 200px;
    display: inline-block;
    margin: 0 auto;
    
    -webkit-border-radius: 10px;
    
    -webkit-box-shadow: 
        0px 3px rgba(128,128,128,1), /* gradient effects */
        0px 4px rgba(118,118,118,1),
        0px 5px rgba(108,108,108,1),
        0px 6px rgba(98,98,98,1),
        0px 7px rgba(88,88,88,1),
        0px 8px rgba(78,78,78,1),
        0px 14px 6px -1px rgba(128,128,128,1); /* shadow */
    
    -webkit-transition: -webkit-box-shadow .1s ease-in-out;
} 

.button span {
    background-color: #E8E8E8;
    
    background-image: 
        /* gloss gradient */
        -webkit-gradient(
            linear, 
            left bottom, 
            left top, 
            color-stop(50%,rgba(255,255,255,0)), 
            color-stop(50%,rgba(255,255,255,0.3)), 
            color-stop(100%,rgba(255,255,255,0.2))),
        
        /* dark outside gradient */
        -webkit-gradient(
            linear, 
            left top, 
            right top, 
            color-stop(0%,rgba(210,210,210,0.3)), 
            color-stop(20%,rgba(210,210,210,0)), 
            color-stop(80%,rgba(210,210,210,0)), 
            color-stop(100%,rgba(210,210,210,0.3))),
        
        /* light inner gradient */
        -webkit-gradient(
            linear, 
            left top, 
            right top, 
            color-stop(0%,rgba(255,255,255,0)), 
            color-stop(20%,rgba(255,255,255,0.5)), 
            color-stop(80%,rgba(255,255,255,0.5)), 
            color-stop(100%,rgba(255,255,255,0))),        
        
        /* diagonal line pattern */
        -webkit-gradient(
            linear, 
            0% 100%, 
            100% 0%, 
            color-stop(0%,rgba(255,255,255,0)), 
            color-stop(40%,rgba(255,255,255,0)), 
            color-stop(40%,#D2D2D1), 
            color-stop(60%,#D2D2D1), 
            color-stop(60%,rgba(255,255,255,0)), 
            color-stop(100%,rgba(255,255,255,0)));
    
        -webkit-box-shadow:
            0px -1px #fff, /* top highlight */
            0px 1px 1px #FFFFFF; /* bottom edge */
    
    -webkit-background-size: 100%, 100%, 100%, 4px 4px;
    
    -webkit-border-radius: 10px;
    -webkit-transition: -webkit-transform .1s ease-in-out;
    
    display: inline-block;
    padding: 10px 40px 10px 40px;
    
    color: #3A474D;
    text-transform: uppercase;
    font-family: 'TradeGothicLTStd-BdCn20','PT Sans Narrow';
    font-weight: 700;
    font-size: 32px;
    
    text-shadow: 0px 1px #fff, 0px -1px #262F33;
}

        .button span:hover {
            color: #AEBF3B;
            text-shadow: 0px -1px #97A63A;
            cursor: pointer;
        }

        .button:active, .button-active{
            -webkit-box-shadow: 
                0px 3px rgba(128,128,128,1),
                0px 4px rgba(118,118,118,1),
                0px 5px rgba(108,108,108,1),
                0px 6px rgba(98,98,98,1),
                0px 7px rgba(88,88,88,1),
                0px 8px rgba(78,78,78,1),
                0px 10px 2px 0px rgba(128,128,128,.6); /* shadow */
        }

        .button:active span, .button-active span{
            -webkit-transform: translate(0, 5px); /* depth of button press */
        }

    .button span:after {
        content: ">";
        display: block;
        width: 10px;
        height: 10px;
    
        position: absolute;
        right: 14px;
        top: 12px;    
        
        font-family: 'Cabin';
        font-weight: 700;
        color: #AEBF3B;
        text-shadow: 0px 1px #fff, 0px -1px #97A63A;
        font-size: 26px;
    }
<link  href="http://fonts.googleapis.com/css?family=Cabin:400,500,600,bold" rel="stylesheet" type="text/css" >

<link  href="http://fonts.googleapis.com/css?family=PT+Sans+Narrow:regular,bold" rel="stylesheet" type="text/css" >

<div id="pagegradient"></div>

    <a class="button" onclick="toggleActive(this)">
        <span>select me </span>
           </a>

#1


3  

You can find the full code below for a version based on jQuery, one based on vanilla JS and one using only HTML & CSS to achieve the desired effect.

您可以在下面找到基于jQuery的版本的完整代码,一个基于vanilla JS的版本和一个仅使用HTML和CSS来实现所需效果的版本。

You can also play around with the code at the following Fiddles:

您还可以使用以下Fiddles中的代码:


jQuery version :

HTML :

<link href="http://fonts.googleapis.com/css?family=Cabin:400,500,600,bold" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=PT+Sans+Narrow:regular,bold" rel="stylesheet" type="text/css">

<a class="button">
  <span>select me </span>
</a>

CSS :

   .button {
  position: absolute;
  top: 100px;
  left: 200px;
  display: inline-block;
  margin: 0 auto;
  -webkit-border-radius: 10px;
  -webkit-box-shadow: 0px 3px rgba(128, 128, 128, 1),
  /* gradient effects */
  0px 4px rgba(118, 118, 118, 1), 0px 5px rgba(108, 108, 108, 1), 0px 6px rgba(98, 98, 98, 1), 0px 7px rgba(88, 88, 88, 1), 0px 8px rgba(78, 78, 78, 1), 0px 14px 6px -1px rgba(128, 128, 128, 1);
  /* shadow */
  -webkit-transition: -webkit-box-shadow .1s ease-in-out;
}

.button span {
  background-color: #E8E8E8;
  background-image:
  /* gloss gradient */
  -webkit-gradient(linear, left bottom, left top, color-stop(50%, rgba(255, 255, 255, 0)), color-stop(50%, rgba(255, 255, 255, 0.3)), color-stop(100%, rgba(255, 255, 255, 0.2))),
  /* dark outside gradient */
  -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(210, 210, 210, 0.3)), color-stop(20%, rgba(210, 210, 210, 0)), color-stop(80%, rgba(210, 210, 210, 0)), color-stop(100%, rgba(210, 210, 210, 0.3))),
  /* light inner gradient */
  -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(20%, rgba(255, 255, 255, 0.5)), color-stop(80%, rgba(255, 255, 255, 0.5)), color-stop(100%, rgba(255, 255, 255, 0))),
  /* diagonal line pattern */
  -webkit-gradient(linear, 0% 100%, 100% 0%, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(40%, rgba(255, 255, 255, 0)), color-stop(40%, #D2D2D1), color-stop(60%, #D2D2D1), color-stop(60%, rgba(255, 255, 255, 0)), color-stop(100%, rgba(255, 255, 255, 0)));
  -webkit-box-shadow: 0px -1px #fff,
  /* top highlight */
  0px 1px 1px #FFFFFF;
  /* bottom edge */
  -webkit-background-size: 100%, 100%, 100%, 4px 4px;
  -webkit-border-radius: 10px;
  -webkit-transition: -webkit-transform .1s ease-in-out;
  display: inline-block;
  padding: 10px 40px 10px 40px;
  color: #3A474D;
  text-transform: uppercase;
  font-family: 'TradeGothicLTStd-BdCn20', 'PT Sans Narrow';
  font-weight: 700;
  font-size: 32px;
  text-shadow: 0px 1px #fff, 0px -1px #262F33;
}

.button span:hover {
  color: #AEBF3B;
  text-shadow: 0px -1px #97A63A;
  cursor: pointer;
}

.button.pressed {
  -webkit-box-shadow: 0px 3px rgba(128, 128, 128, 1), 0px 4px rgba(118, 118, 118, 1), 0px 5px rgba(108, 108, 108, 1), 0px 6px rgba(98, 98, 98, 1), 0px 7px rgba(88, 88, 88, 1), 0px 8px rgba(78, 78, 78, 1), 0px 10px 2px 0px rgba(128, 128, 128, .6);
  /* shadow */
}

.button.pressed span {
  -webkit-transform: translate(0, 5px);
  /* depth of button press */
}

.button span:after {
  content: ">";
  display: block;
  width: 10px;
  height: 10px;
  position: absolute;
  right: 14px;
  top: 12px;
  font-family: 'Cabin';
  font-weight: 700;
  color: #AEBF3B;
  text-shadow: 0px 1px #fff, 0px -1px #97A63A;
  font-size: 26px;
}

JS :

$('.button').click(function(){
    $(this).toggleClass('pressed');
});

JS version without jQuery :

HTML :

<link href="http://fonts.googleapis.com/css?family=Cabin:400,500,600,bold" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=PT+Sans+Narrow:regular,bold" rel="stylesheet" type="text/css">

<a class="button">
  <span>select me </span>
</a>

CSS :

 .button {
  position: absolute;
  top: 100px;
  left: 200px;
  display: inline-block;
  margin: 0 auto;
  -webkit-border-radius: 10px;
  -webkit-box-shadow: 0px 3px rgba(128, 128, 128, 1),
  /* gradient effects */
  0px 4px rgba(118, 118, 118, 1), 0px 5px rgba(108, 108, 108, 1), 0px 6px rgba(98, 98, 98, 1), 0px 7px rgba(88, 88, 88, 1), 0px 8px rgba(78, 78, 78, 1), 0px 14px 6px -1px rgba(128, 128, 128, 1);
  /* shadow */
  -webkit-transition: -webkit-box-shadow .1s ease-in-out;
}

.button span {
  background-color: #E8E8E8;
  background-image:
  /* gloss gradient */
  -webkit-gradient(linear, left bottom, left top, color-stop(50%, rgba(255, 255, 255, 0)), color-stop(50%, rgba(255, 255, 255, 0.3)), color-stop(100%, rgba(255, 255, 255, 0.2))),
  /* dark outside gradient */
  -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(210, 210, 210, 0.3)), color-stop(20%, rgba(210, 210, 210, 0)), color-stop(80%, rgba(210, 210, 210, 0)), color-stop(100%, rgba(210, 210, 210, 0.3))),
  /* light inner gradient */
  -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(20%, rgba(255, 255, 255, 0.5)), color-stop(80%, rgba(255, 255, 255, 0.5)), color-stop(100%, rgba(255, 255, 255, 0))),
  /* diagonal line pattern */
  -webkit-gradient(linear, 0% 100%, 100% 0%, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(40%, rgba(255, 255, 255, 0)), color-stop(40%, #D2D2D1), color-stop(60%, #D2D2D1), color-stop(60%, rgba(255, 255, 255, 0)), color-stop(100%, rgba(255, 255, 255, 0)));
  -webkit-box-shadow: 0px -1px #fff,
  /* top highlight */
  0px 1px 1px #FFFFFF;
  /* bottom edge */
  -webkit-background-size: 100%, 100%, 100%, 4px 4px;
  -webkit-border-radius: 10px;
  -webkit-transition: -webkit-transform .1s ease-in-out;
  display: inline-block;
  padding: 10px 40px 10px 40px;
  color: #3A474D;
  text-transform: uppercase;
  font-family: 'TradeGothicLTStd-BdCn20', 'PT Sans Narrow';
  font-weight: 700;
  font-size: 32px;
  text-shadow: 0px 1px #fff, 0px -1px #262F33;
}

.button span:hover {
  color: #AEBF3B;
  text-shadow: 0px -1px #97A63A;
  cursor: pointer;
}

.button.pressed {
  -webkit-box-shadow: 0px 3px rgba(128, 128, 128, 1), 0px 4px rgba(118, 118, 118, 1), 0px 5px rgba(108, 108, 108, 1), 0px 6px rgba(98, 98, 98, 1), 0px 7px rgba(88, 88, 88, 1), 0px 8px rgba(78, 78, 78, 1), 0px 10px 2px 0px rgba(128, 128, 128, .6);
  /* shadow */
}

.button.pressed span {
  -webkit-transform: translate(0, 5px);
  /* depth of button press */
}

.button span:after {
  content: ">";
  display: block;
  width: 10px;
  height: 10px;
  position: absolute;
  right: 14px;
  top: 12px;
  font-family: 'Cabin';
  font-weight: 700;
  color: #AEBF3B;
  text-shadow: 0px 1px #fff, 0px -1px #97A63A;
  font-size: 26px;
}

JS :

var buttons = document.getElementsByClassName("button");
[].forEach.call(buttons, function(button) {
    button.addEventListener("click", function(e) {
        this.classList.toggle('pressed');        
    });
});

HTML/CSS only version :

HTML :

<link href="http://fonts.googleapis.com/css?family=Cabin:400,500,600,bold" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=PT+Sans+Narrow:regular,bold" rel="stylesheet" type="text/css">

<button class="button">
<input type="checkbox" id="selectme" />
<span>
  <label for="selectme">select me </label>
</span>
</button>

CSS :

.button {
  margin-left: -5000px;
}

.button span {
  position: absolute;
  top: 100px;
  left: 200px;
  display: inline-block;
  margin: 0 auto;
  -webkit-border-radius: 10px;
  -webkit-box-shadow: 0px 3px rgba(128, 128, 128, 1),
  /* gradient effects */
  0px 4px rgba(118, 118, 118, 1), 0px 5px rgba(108, 108, 108, 1), 0px 6px rgba(98, 98, 98, 1), 0px 7px rgba(88, 88, 88, 1), 0px 8px rgba(78, 78, 78, 1), 0px 14px 6px -1px rgba(128, 128, 128, 1);
  /* shadow */
  -webkit-transition: -webkit-box-shadow .1s ease-in-out;
}
.button label {
  background-color: #E8E8E8;
  background-image:
  /* gloss gradient */
  -webkit-gradient(linear, left bottom, left top, color-stop(50%, rgba(255, 255, 255, 0)), color-stop(50%, rgba(255, 255, 255, 0.3)), color-stop(100%, rgba(255, 255, 255, 0.2))),
  /* dark outside gradient */
  -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(210, 210, 210, 0.3)), color-stop(20%, rgba(210, 210, 210, 0)), color-stop(80%, rgba(210, 210, 210, 0)), color-stop(100%, rgba(210, 210, 210, 0.3))),
  /* light inner gradient */
  -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(20%, rgba(255, 255, 255, 0.5)), color-stop(80%, rgba(255, 255, 255, 0.5)), color-stop(100%, rgba(255, 255, 255, 0))),
  /* diagonal line pattern */
  -webkit-gradient(linear, 0% 100%, 100% 0%, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(40%, rgba(255, 255, 255, 0)), color-stop(40%, #D2D2D1), color-stop(60%, #D2D2D1), color-stop(60%, rgba(255, 255, 255, 0)), color-stop(100%, rgba(255, 255, 255, 0)));
  -webkit-box-shadow: 0px -1px #fff,
  /* top highlight */
  0px 1px 1px #FFFFFF;
  /* bottom edge */
  -webkit-background-size: 100%, 100%, 100%, 4px 4px;
  -webkit-border-radius: 10px;
  -webkit-transition: -webkit-transform .1s ease-in-out;
  display: inline-block;
  padding: 10px 40px 10px 40px;
  color: #3A474D;
  text-transform: uppercase;
  font-family: 'TradeGothicLTStd-BdCn20', 'PT Sans Narrow';
  font-weight: 700;
  font-size: 32px;
  text-shadow: 0px 1px #fff, 0px -1px #262F33;
}
.button label:hover {
  color: #AEBF3B;
  text-shadow: 0px -1px #97A63A;
  cursor: pointer;
}

.button input:checked + span {
  -webkit-box-shadow: 0px 3px rgba(128, 128, 128, 1), 0px 4px rgba(118, 118, 118, 1), 0px 5px rgba(108, 108, 108, 1), 0px 6px rgba(98, 98, 98, 1), 0px 7px rgba(88, 88, 88, 1), 0px 8px rgba(78, 78, 78, 1), 0px 10px 2px 0px rgba(128, 128, 128, .6);
  /* shadow */
}

.button input:checked + span label {
  -webkit-transform: translate(0, 5px);
  /* depth of button press */
}

.button label:after {
  content: ">";
  display: block;
  width: 10px;
  height: 10px;
  position: absolute;
  right: 14px;
  top: 12px;
  font-family: 'Cabin';
  font-weight: 700;
  color: #AEBF3B;
  text-shadow: 0px 1px #fff, 0px -1px #97A63A;
  font-size: 26px;
}

JS :

/* CSS only means that no JS code is required at all! */

#2


3  

You can achieve it without JavaScript. Try :checked pseudo class and adjacent sibling selector (+):

你可以在没有JavaScript的情况下实现它尝试:检查伪类和相邻的兄弟选择器(+):

label {
  width: 120px;
  height: 40px;
  background: #d00;
  display: inline-block;
  text-align: center;
  line-height: 40px;
  cursor: pointer;
  color: white;
}
#checkbox {
  display: none;
}
#checkbox:checked + label {
  background: #600;
}
<input type="checkbox" id="checkbox">
<label for="checkbox">select me </label>

I also did a little change to your fiddle

我也对你的小提琴做了一点改动

You can also make nice look single selection:

你也可以看看单一选择:

label {
  width: 120px;
  height: 40px;
  background: #d00;
  display: inline-block;
  text-align: center;
  line-height: 40px;
  cursor: pointer;
  color: white;
}
input {
  display: none;
}
input:checked + label {
  background: #600;
}
<input type="radio" name="group" id="option1">
<label for="option1">select 1</label>
<input type="radio" name="group" id="option2">
<label for="option2">select 2</label>
<input type="radio" name="group" id="option3">
<label for="option3">select 3</label>
<input type="radio" name="group" id="option4">
<label for="option4">select 4</label>

#3


3  

First, decorate your css styles that use :active attribute with an alias class like

首先,装饰你使用的css样式:带有别名类的active属性

.button:active, .button-active{
...
}

.button:active span, .button-active span{
...
}

then make a onclick event handler which manually toggles the class

然后创建一个onclick事件处理程序,手动切换类

function toggleActive(element){
   element.classList.toggle('button-active');
}

function toggleActive(element){
   element.classList.toggle('button-active');
}
body {
    background: #A7A9AC;
    background-image: -webkit-gradient(
        linear,
        left top,
        right top,
        color-stop(.34, rgba(230,237,241,.05)),
        color-stop(.67, rgba(230,237,241,0)));
    -webkit-background-size: 5px 5px;
}

#pagegradient {
  background-image:
        -webkit-gradient(
            radial, 
            50% -50, 
            300, 
            50% 0, 
            0, 
            from(rgba(230, 237, 241, 0)), 
            to(rgba(230, 237, 241, 0.8)));
    
  height:100%;
  left:0px;
  position:absolute;
  top:0;
  width: 600px;
}

.button {
    position: absolute;
    top: 100px;
    left: 200px;
    display: inline-block;
    margin: 0 auto;
    
    -webkit-border-radius: 10px;
    
    -webkit-box-shadow: 
        0px 3px rgba(128,128,128,1), /* gradient effects */
        0px 4px rgba(118,118,118,1),
        0px 5px rgba(108,108,108,1),
        0px 6px rgba(98,98,98,1),
        0px 7px rgba(88,88,88,1),
        0px 8px rgba(78,78,78,1),
        0px 14px 6px -1px rgba(128,128,128,1); /* shadow */
    
    -webkit-transition: -webkit-box-shadow .1s ease-in-out;
} 

.button span {
    background-color: #E8E8E8;
    
    background-image: 
        /* gloss gradient */
        -webkit-gradient(
            linear, 
            left bottom, 
            left top, 
            color-stop(50%,rgba(255,255,255,0)), 
            color-stop(50%,rgba(255,255,255,0.3)), 
            color-stop(100%,rgba(255,255,255,0.2))),
        
        /* dark outside gradient */
        -webkit-gradient(
            linear, 
            left top, 
            right top, 
            color-stop(0%,rgba(210,210,210,0.3)), 
            color-stop(20%,rgba(210,210,210,0)), 
            color-stop(80%,rgba(210,210,210,0)), 
            color-stop(100%,rgba(210,210,210,0.3))),
        
        /* light inner gradient */
        -webkit-gradient(
            linear, 
            left top, 
            right top, 
            color-stop(0%,rgba(255,255,255,0)), 
            color-stop(20%,rgba(255,255,255,0.5)), 
            color-stop(80%,rgba(255,255,255,0.5)), 
            color-stop(100%,rgba(255,255,255,0))),        
        
        /* diagonal line pattern */
        -webkit-gradient(
            linear, 
            0% 100%, 
            100% 0%, 
            color-stop(0%,rgba(255,255,255,0)), 
            color-stop(40%,rgba(255,255,255,0)), 
            color-stop(40%,#D2D2D1), 
            color-stop(60%,#D2D2D1), 
            color-stop(60%,rgba(255,255,255,0)), 
            color-stop(100%,rgba(255,255,255,0)));
    
        -webkit-box-shadow:
            0px -1px #fff, /* top highlight */
            0px 1px 1px #FFFFFF; /* bottom edge */
    
    -webkit-background-size: 100%, 100%, 100%, 4px 4px;
    
    -webkit-border-radius: 10px;
    -webkit-transition: -webkit-transform .1s ease-in-out;
    
    display: inline-block;
    padding: 10px 40px 10px 40px;
    
    color: #3A474D;
    text-transform: uppercase;
    font-family: 'TradeGothicLTStd-BdCn20','PT Sans Narrow';
    font-weight: 700;
    font-size: 32px;
    
    text-shadow: 0px 1px #fff, 0px -1px #262F33;
}

        .button span:hover {
            color: #AEBF3B;
            text-shadow: 0px -1px #97A63A;
            cursor: pointer;
        }

        .button:active, .button-active{
            -webkit-box-shadow: 
                0px 3px rgba(128,128,128,1),
                0px 4px rgba(118,118,118,1),
                0px 5px rgba(108,108,108,1),
                0px 6px rgba(98,98,98,1),
                0px 7px rgba(88,88,88,1),
                0px 8px rgba(78,78,78,1),
                0px 10px 2px 0px rgba(128,128,128,.6); /* shadow */
        }

        .button:active span, .button-active span{
            -webkit-transform: translate(0, 5px); /* depth of button press */
        }

    .button span:after {
        content: ">";
        display: block;
        width: 10px;
        height: 10px;
    
        position: absolute;
        right: 14px;
        top: 12px;    
        
        font-family: 'Cabin';
        font-weight: 700;
        color: #AEBF3B;
        text-shadow: 0px 1px #fff, 0px -1px #97A63A;
        font-size: 26px;
    }
<link  href="http://fonts.googleapis.com/css?family=Cabin:400,500,600,bold" rel="stylesheet" type="text/css" >

<link  href="http://fonts.googleapis.com/css?family=PT+Sans+Narrow:regular,bold" rel="stylesheet" type="text/css" >

<div id="pagegradient"></div>

    <a class="button" onclick="toggleActive(this)">
        <span>select me </span>
           </a>