悬停复选框黑客
Hover with checkbox hack
我正在尝试制作一个框,当按下按钮时,文本仅显示 "Reserved" 截至目前它显示 "Free Space!Reserved" 因为我使用了 :after 函数。有什么方法可以创建悬停功能,以便在框为绿色时显示 "Click here to reserve" 并在框为红色时悬停 "Mine"?
代码在jsFiddle及以下:
<input type="checkbox" id="btnControl"/>
<label class="mouseover1" for="btnControl">Free Space</label>
.mouseover1 {
background-color: green;
color: white;
display: inline-block;
padding: 5% ;
text-align: center;
width: 20%;
}
#btnControl {
display: none;
}
.mouseover1:active {
margin-left: 2px;
box-shadow: 0px 0px 1px #000;
outline: 1px solid black;
}
.mouseover1:hover {
background-image: linear-gradient(to bottom, #c3e3fa, #a5defb);
color:black;
}
#btnControl:checked + label:after {
content: 'Reserved!';
}
input[type=checkbox]:checked ~ .mouseover1 {
background-color: red;
}
你可以使用字体大小:
.mouseover1 {
background-color: green;
color: white;
display: inline-block;
padding: 5% ;
text-align: center;
width: 20%;
}
#btnControl {
display: none;
}
.mouseover1:active {
margin-left: 2px;
box-shadow: 0px 0px 1px #000;
outline: 1px solid black;
}
.mouseover1:hover {
background-image: linear-gradient(to bottom, #c3e3fa, #a5defb);
color:black;
}
#btnControl:checked + label {
font-size:0;
}
#btnControl:checked + label:after {
content: 'Reserved!';
font-size:1rem;
}
input[type=checkbox]:checked ~ .mouseover1 {
background-color: red;
}
<input type="checkbox" id="btnControl"/>
<label class="mouseover1" for="btnControl">Free Space</label>
或浮动和文本缩进
.mouseover1 {
background-color: green;
color: white;
display: inline-block;
padding: 5%;
text-align: center;
width: 20%;
}
#btnControl {
display: none;
}
.mouseover1:active {
margin-left: 2px;
box-shadow: 0px 0px 1px #000;
outline: 1px solid black;
}
.mouseover1:hover {
background-image: linear-gradient(to bottom, #c3e3fa, #a5defb);
color: black;
}
#btnControl:checked + label {
text-indent: -999px;
;
}
#btnControl:checked + label:after {
content: 'Reserved!';
float: left;
width: 100%;
text-indent: 0;
}
input[type=checkbox]:checked ~ .mouseover1 {
background-color: red;
}
<input type="checkbox" id="btnControl" />
<label class="mouseover1" for="btnControl">Free Space</label>
或您认为相关的任何其他方法。最好是 javascript 更改文本。
将文本包裹在 span
中可以让您轻松隐藏它。悬停文本 ("Click here to reserve / Mine") 可以添加另一个伪元素
.mouseover1 {
background-color: green;
color: white;
display: inline-block;
padding: 5% ;
text-align: center;
width: 20%;
}
#btnControl {
display: none;
}
.mouseover1:active {
margin-left: 2px;
box-shadow: 0px 0px 1px #000;
outline: 1px solid black;
}
.mouseover1:hover {
background-image: linear-gradient(to bottom, #c3e3fa, #a5defb);
color:black;
}
label {
position: relative;
}
#btnControl:checked + label:after {
content: 'Reserved!';
}
#btnControl:checked + label span {
display: none;
}
#btnControl + label:before {
content: 'Click here to reserve';
position: absolute;
bottom: 0;
left: 0;
right: 0;
background: rgba(0,0,0,.8);
color: #fff;
opacity: 0;
transition: all .2s ease;
}
#btnControl + label:hover:before {
opacity: 1;
}
#btnControl:checked + label:before {
content: 'Mine!';
}
input[type=checkbox]:checked ~ .mouseover1 {
background-color: red;
}
<input type="checkbox" id="btnControl"/>
<label class="mouseover1" for="btnControl"><span>Free Space</span></label>
我确实用:before
来show/hide原文
body {
max-width:100%;
max-height:100vh;
font-family: arial, sans-serif;
font-weight: bold;
}
label {
position:absolute;
right:0;
left:0;
margin:auto;
}
.mouseover1 {
background-color: yellowgreen;
color: white;
display: inline-block;
padding: 5% ;
text-align: center;
width: 20%;
}
#btnControl {
display: none;
}
.mouseover1:active {
box-shadow: 0px 0px 1px #000;
outline: 2px solid darkred;
}
.mouseover1:hover {
background: yellowgreen;
color: white;
}
#btnControl:checked + label:after {
content: 'Reserved';
}
#btnControl + label:before {
content: 'Free Space';
}
#btnControl:checked + label:before {
content: '';
}
#btnControl:checked:hover + label:after {
content: 'Mine!';
}
#btnControl:hover + label:before {
content: 'Click here!';
}
#btnControl:checked:hover + label:before {
content: '';
}
input[type=checkbox]:checked + .mouseover1 {
background-color: tomato;
}
<input type="checkbox" id="btnControl"/>
<label class="mouseover1" for="btnControl"></label>
要更改标签内的文本,请先将其包装在 div
中
<input type="checkbox" id="btnControl"/>
<div class="container">
<label class="mouseover1 new-label" for="btnControl"><Span>Free Space</Span></label>
</div>
确保将 "new-label" 添加到标签 class
然后改变 CSS 如下:
.mouseover1 {
background-color: green;
color: white;
display: inline-block;
padding: 5% ;
text-align: center;
width: 20%;
}
#btnControl {
display: none;
}
.mouseover1:active {
margin-left: 2px;
box-shadow: 0px 0px 1px #000;
outline: 1px solid black;
}
.mouseover1:hover {
background-image: linear-gradient(to bottom, #c3e3fa, #a5defb);
color:black;
}
#btnControl:checked + label:after {
content: 'Reserved!';
}
input[type=checkbox]:checked ~ .mouseover1 {
background-color: red;
}
.container:hover .new-label span{
display: none;
}
.container:hover .new-label:after{
content: 'Click Here To Reserve';
}
只需为文本添加跨度并添加 class。
CSS
#btnControl:checked + .mouseover1 span {
display: none;
}
给你:jsFiddle
我正在尝试制作一个框,当按下按钮时,文本仅显示 "Reserved" 截至目前它显示 "Free Space!Reserved" 因为我使用了 :after 函数。有什么方法可以创建悬停功能,以便在框为绿色时显示 "Click here to reserve" 并在框为红色时悬停 "Mine"?
代码在jsFiddle及以下:
<input type="checkbox" id="btnControl"/>
<label class="mouseover1" for="btnControl">Free Space</label>
.mouseover1 {
background-color: green;
color: white;
display: inline-block;
padding: 5% ;
text-align: center;
width: 20%;
}
#btnControl {
display: none;
}
.mouseover1:active {
margin-left: 2px;
box-shadow: 0px 0px 1px #000;
outline: 1px solid black;
}
.mouseover1:hover {
background-image: linear-gradient(to bottom, #c3e3fa, #a5defb);
color:black;
}
#btnControl:checked + label:after {
content: 'Reserved!';
}
input[type=checkbox]:checked ~ .mouseover1 {
background-color: red;
}
你可以使用字体大小:
.mouseover1 {
background-color: green;
color: white;
display: inline-block;
padding: 5% ;
text-align: center;
width: 20%;
}
#btnControl {
display: none;
}
.mouseover1:active {
margin-left: 2px;
box-shadow: 0px 0px 1px #000;
outline: 1px solid black;
}
.mouseover1:hover {
background-image: linear-gradient(to bottom, #c3e3fa, #a5defb);
color:black;
}
#btnControl:checked + label {
font-size:0;
}
#btnControl:checked + label:after {
content: 'Reserved!';
font-size:1rem;
}
input[type=checkbox]:checked ~ .mouseover1 {
background-color: red;
}
<input type="checkbox" id="btnControl"/>
<label class="mouseover1" for="btnControl">Free Space</label>
或浮动和文本缩进
.mouseover1 {
background-color: green;
color: white;
display: inline-block;
padding: 5%;
text-align: center;
width: 20%;
}
#btnControl {
display: none;
}
.mouseover1:active {
margin-left: 2px;
box-shadow: 0px 0px 1px #000;
outline: 1px solid black;
}
.mouseover1:hover {
background-image: linear-gradient(to bottom, #c3e3fa, #a5defb);
color: black;
}
#btnControl:checked + label {
text-indent: -999px;
;
}
#btnControl:checked + label:after {
content: 'Reserved!';
float: left;
width: 100%;
text-indent: 0;
}
input[type=checkbox]:checked ~ .mouseover1 {
background-color: red;
}
<input type="checkbox" id="btnControl" />
<label class="mouseover1" for="btnControl">Free Space</label>
或您认为相关的任何其他方法。最好是 javascript 更改文本。
将文本包裹在 span
中可以让您轻松隐藏它。悬停文本 ("Click here to reserve / Mine") 可以添加另一个伪元素
.mouseover1 {
background-color: green;
color: white;
display: inline-block;
padding: 5% ;
text-align: center;
width: 20%;
}
#btnControl {
display: none;
}
.mouseover1:active {
margin-left: 2px;
box-shadow: 0px 0px 1px #000;
outline: 1px solid black;
}
.mouseover1:hover {
background-image: linear-gradient(to bottom, #c3e3fa, #a5defb);
color:black;
}
label {
position: relative;
}
#btnControl:checked + label:after {
content: 'Reserved!';
}
#btnControl:checked + label span {
display: none;
}
#btnControl + label:before {
content: 'Click here to reserve';
position: absolute;
bottom: 0;
left: 0;
right: 0;
background: rgba(0,0,0,.8);
color: #fff;
opacity: 0;
transition: all .2s ease;
}
#btnControl + label:hover:before {
opacity: 1;
}
#btnControl:checked + label:before {
content: 'Mine!';
}
input[type=checkbox]:checked ~ .mouseover1 {
background-color: red;
}
<input type="checkbox" id="btnControl"/>
<label class="mouseover1" for="btnControl"><span>Free Space</span></label>
我确实用:before
来show/hide原文
body {
max-width:100%;
max-height:100vh;
font-family: arial, sans-serif;
font-weight: bold;
}
label {
position:absolute;
right:0;
left:0;
margin:auto;
}
.mouseover1 {
background-color: yellowgreen;
color: white;
display: inline-block;
padding: 5% ;
text-align: center;
width: 20%;
}
#btnControl {
display: none;
}
.mouseover1:active {
box-shadow: 0px 0px 1px #000;
outline: 2px solid darkred;
}
.mouseover1:hover {
background: yellowgreen;
color: white;
}
#btnControl:checked + label:after {
content: 'Reserved';
}
#btnControl + label:before {
content: 'Free Space';
}
#btnControl:checked + label:before {
content: '';
}
#btnControl:checked:hover + label:after {
content: 'Mine!';
}
#btnControl:hover + label:before {
content: 'Click here!';
}
#btnControl:checked:hover + label:before {
content: '';
}
input[type=checkbox]:checked + .mouseover1 {
background-color: tomato;
}
<input type="checkbox" id="btnControl"/>
<label class="mouseover1" for="btnControl"></label>
要更改标签内的文本,请先将其包装在 div
中<input type="checkbox" id="btnControl"/>
<div class="container">
<label class="mouseover1 new-label" for="btnControl"><Span>Free Space</Span></label>
</div>
确保将 "new-label" 添加到标签 class 然后改变 CSS 如下:
.mouseover1 {
background-color: green;
color: white;
display: inline-block;
padding: 5% ;
text-align: center;
width: 20%;
}
#btnControl {
display: none;
}
.mouseover1:active {
margin-left: 2px;
box-shadow: 0px 0px 1px #000;
outline: 1px solid black;
}
.mouseover1:hover {
background-image: linear-gradient(to bottom, #c3e3fa, #a5defb);
color:black;
}
#btnControl:checked + label:after {
content: 'Reserved!';
}
input[type=checkbox]:checked ~ .mouseover1 {
background-color: red;
}
.container:hover .new-label span{
display: none;
}
.container:hover .new-label:after{
content: 'Click Here To Reserve';
}
只需为文本添加跨度并添加 class。
CSS
#btnControl:checked + .mouseover1 span {
display: none;
}
给你:jsFiddle