【发布时间】:2016-07-14 14:34:33
【问题描述】:
如何设置带有阴影的按钮样式,使其看起来像被按下?
我尝试使用box-shadow: ... ;。但这没有任何影响。
【问题讨论】:
-
你试过
box-shadow: inset吗? -
@Jmh2013 这正是我所需要的。不知道我怎么没找到这个。。谢谢
如何设置带有阴影的按钮样式,使其看起来像被按下?
我尝试使用box-shadow: ... ;。但这没有任何影响。
【问题讨论】:
box-shadow: inset吗?
通过使用box-shadow: inset ...; 创造性地设置:active 或:focus pseudo classes 的样式
使用:active 伪类:
button {
background: #ededed;
border: 1px solid #ccc;
padding: 10px 30px;
border-radius: 3px;
cursor: pointer;
}
button:active {
background: #e5e5e5;
-webkit-box-shadow: inset 0px 0px 5px #c1c1c1;
-moz-box-shadow: inset 0px 0px 5px #c1c1c1;
box-shadow: inset 0px 0px 5px #c1c1c1;
outline: none;
}
<button>
Click me
</button>
使用:focus 伪类:
button {
background: #ededed;
border: 1px solid #ccc;
padding: 10px 30px;
border-radius: 3px;
cursor: pointer;
}
button:focus {
background: #e5e5e5;
outline: none;
-webkit-box-shadow: inset 0px 0px 5px #c1c1c1;
-moz-box-shadow: inset 0px 0px 5px #c1c1c1;
box-shadow: inset 0px 0px 5px #c1c1c1;
}
<button>
Click me
</button>
【讨论】:
:activepseudo 类时,它可以工作,但不能与:focus 一起使用。然后单击按钮时没有任何反应。背景颜色没有变化。
:focus 改变背景颜色和阴影
作为按钮的替代方案,还可以简单地使用带有伪类:checked 的复选框来切换状态。
label.label-checkbox {
cursor: pointer;
}
label.label-checkbox input {
position: absolute;
top: 0;
left: 0;
visibility: hidden;
pointer-events: none;
}
label.label-checkbox span {
padding: 11px 21px;
border: 1px solid #ccc;
display: inline-block;
color: #202020;
border-radius: 6px;
margin: 7px;
background: #f5f5f5;
user-select: none;
}
label.label-checkbox input:checked + span {
box-shadow: inset 1px 2px 5px #777;
transform: translateY(1px);
background: #e5e5e5;
}
<h1>Pressed buttons with Checkbox</h1>
<label class="label-checkbox">
<input type="checkbox">
<span>Checkbox</span>
</label>
<label class="label-checkbox">
<input type="checkbox" checked>
<span>Styled</span>
</label>
<label class="label-checkbox">
<input type="checkbox">
<span>As</span>
</label>
<label class="label-checkbox">
<input type="checkbox" checked>
<span>Pressed</span>
</label>
<label class="label-checkbox">
<input type="checkbox">
<span>Buttons</span>
</label>
【讨论】:
最好的方法是将按钮轻推到页面下方。使用transformY 将是最直接的。但是,这可能会弄乱页面中其他内容的布局。所以我觉得还是用margin来暂时降低按钮比较好,比如,
button {
background-color: white;
padding: 10px;
vertical-align: top;
box-shadow: 2px 1px 2px gray;
margin: 4px 10px 4px 10px;
}
button:active {
box-shadow: 0 0 0 white;
margin: 6px 10px 2px 10px;
}
<button>click me</button>
<button>click me</button>
<br>
<button>click me</button>
<button>click me</button>
如示例所示,您可以从下边距中移除 2px,并在上边距中添加 2px,因此您可以保留按钮的总大小。
如果有多个按钮,则需要垂直对齐。
【讨论】:
transformY。更改边距可能会弄乱页面中其他内容的布局,但您在示例中考虑了这一点,因为您正在更改顶部 和 底部边距。 transform: translateY(2px) 不会影响其他元素的布局。如果你想要一个没有边距的按钮,它就不会起作用。请注意this example 中,当您单击它时更改其边距的按钮会强制其他元素移动,但使用变换的按钮不会。
我认为让按钮看起来像是被按下的最好方法是让它变暗一点。
button{
background-color: #03A9F4;
border: none;
padding: 15px 25px;
text-transform: uppercase;
color: white;
font-weight: 700;
border-radius: 3px;
}
button:hover, button:focus{
background-color: #0074a9;
outline: none;
}
<button>Button</button>
【讨论】:
如果您从视觉上考虑按下按钮(如在老式立体声系统上)时会发生什么,按钮会向后移动。从视觉上看,按钮的表面更暗。按钮上的文本是嵌入的。按钮的边框是深色的。
这里的其他答案都给出了部分答案。
这在视觉上完成了上述所有操作:
.btnPushed {
color: #efefef; //orig text color was #FFF
text-shadow: -1px -1px 0px #777, -1px -1px 0px #777;
box-shadow: inset 1px 1px 4px #222;
transform: translateY(1px); /* Add per Vince's helpful comment */
}
您可能会注意到,我们通过添加一个类来应用样式。
$('button').click(function(){
$('button').removeClass('depressed');
$(this).addClass('depressed');
});
button {
border: 1px solid black;
border-radius: 3px;
color: #f5f5f5;
background-color: #b8860b;
background-image: linear-gradient(-180deg,#6699FF,#3473F5 90%);
cursor: pointer;
font-size: 14px;
line-height: 20px;
outline: none; /* Removes Chrome's blue outline */
margin: 2px;
}
button:active{
}
.depressed{
color: #efefef;
text-shadow: -1px -1px 0px #777, -1px -1px 0px #777;
box-shadow: inset 1px 1px 3px #222;
margin: 3px -1px -1px 3px; /* T R B L */
transform: translateY(1px);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<button>Button1</button>
<button>Button2</button>
<button class="depressed">Button3</button>
<button>Button4</button>
为避免其他按钮因边距变化而调整(移动),只需将每个按钮放入一个固定大小的 div 中即可。这样按钮在它们的 div 中移动,而不会影响它们自己的 div 中的其他按钮。
$('button').click(function(){
$('button').removeClass('depressed');
$(this).addClass('depressed');
});
div {
display: inline-block;
width: 65px;
height: 25px;
}
button {
border: 1px solid black;
border-radius: 3px;
color: #f5f5f5;
background-image: linear-gradient(-180deg,#6699FF,#3473F5 90%);
cursor: pointer;
font-size: 14px;
line-height: 20px;
outline: none; /* Removes Chrome's blue outline */
margin: 2px;
}
button:active{
}
.depressed{
color: #efefef;
text-shadow: -1px -1px 0px #777, -1px -1px 0px #777;
box-shadow: inset 1px 1px 3px #222;
margin: 3px -1px -1px 3px; /* T R B L */
transform: translateY(1px);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div><button>Button1</button></div>
<div><button>Button2</button></div>
<div><button class="depressed">Button3</button></div>
<div><button>Button4</button></div>
根据下面文斯的有用评论添加了transform: translateY(1px)。
【讨论】:
:active 伪类呢?我不喜欢文字阴影,但这只是一种意见。 My example 受到您的启发,但不需要整个 JavaScript 库即可获得所需的效果。
transform: translateY(1px);,我很喜欢。 (事实上,我本来打算在我的回答中包含这一点,但忘记了。) 但是,我不同意使用 :active, :focus 伪类,因为只有在按钮保持重点。在您的示例中,一旦您单击屏幕的另一部分,您就会失去按下按钮的效果。在 1970 年代的汽车音响上,如果在调节音量时预设电台按钮弹回,它不会做...?至于 jQuery 的使用,绝对不需要仅仅应用一个类——这里只是一个偏好。
.button{
color: white;
background-color: blue;
padding: 8px 25px;
border-radius : 7px;
}
.button:active {
box-shadow: 0 0 0 black;
margin: 3px 0 0 0 ;
}
<input type="button" class="button" value="Enter">
【讨论】:
button{
background-color:grey;
padding:10px;
border:none;
color:white;
}
button:hover{
background-color:black;
color:white;
}
<button class"b1">button</button>
【讨论】: