我制作了一个2D正方形,可以在“类”之间切换以更改颜色。单击后,它变成蓝色。再次单击时,它会变回灰色。代码如下!
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<style>
.square {
display: inline-block;
border-radius: 4px;
height: 100px;
width: 100px;
text-align: center;
background: #D2D7D3;
}
.blue {
background: #446CB3;
}
</style>
</head>
<body>
<div class="square" id="b1"></div>
<script type="text/javascript">
<!-- changes square color -->
$("#b1").click(function() {
$('#b1').toggleClass('blue');
});
</script>
</body>
</html>
现在,我尝试使用3D正方形复制此切换功能,但未成功。因此,在单击它时,它应该变成蓝色并保持蓝色,直到再次单击它为止。然后,再次单击时,它应该变回灰色。 (就像在2D版本中一样。)以下是我当前无法使用的代码!
提前感谢您的任何建议!
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<style>
.square {
display: inline-block;
border-radius: 4px;
height: 100px;
width: 100px;
text-align: center;
}
.square a {
color: #B6BBB7;
font-family: Helvetica, sans-serif;
font-weight: bold;
font-size: 36px;
text-align: center;
text-decoration: none;
background-color: #B6BBB7;
display: block;
position: relative;
padding: 20px 40px;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
filter: dropshadow(color=#000, offx=0px, offy=1px);
-webkit-box-shadow: inset 0 1px 0 #D2D7D3, 0 10px 0 #505551;
-moz-box-shadow: inset 0 1px 0 #D2D7D3, 0 10px 0 #505551;
box-shadow: inset 0 1px 0 #D2D7D3, 0 10px 0 #505551;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
.square a:active {
top: 10px;
color: #446CB3;
background-color: #446CB3;
-webkit-box-shadow: inset 0 1px 0 #B6D5FF, inset 0 -3px 0 #003D7E;
-moz-box-shadow: inset 0 1px 0 #B6D5FF, inset 0 -3pxpx 0 #003D7E;
box-shadow: inset 0 1px 0 #B6D5FF, inset 0 -3px 0 #003D7E;
}
</style>
</head>
<body onload="pageLoad();">
<div id="experiment">
<div id="shape_container">
<div ontouchstart="" class="square" id="b1"><a href="#">b1</a></div>
</div>
<script type="text/javascript">
$("#b1").click(function() {
// haven't figured out how to write correct toggle code here!
});
</script>
</body>
</html>
答案 0 :(得分:2)
将其添加为类,而不是定位:active
状态:
$("#b1").click(function() {
$(this).children('a').toggleClass('active');
});
.square {
display: inline-block;
border-radius: 4px;
height: 100px;
width: 100px;
text-align: center;
}
.square a {
color: #B6BBB7;
font-family: Helvetica, sans-serif;
font-weight: bold;
font-size: 36px;
text-align: center;
text-decoration: none;
background-color: #B6BBB7;
display: block;
position: relative;
padding: 20px 40px;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
filter: dropshadow(color=#000, offx=0px, offy=1px);
-webkit-box-shadow: inset 0 1px 0 #D2D7D3, 0 10px 0 #505551;
-moz-box-shadow: inset 0 1px 0 #D2D7D3, 0 10px 0 #505551;
box-shadow: inset 0 1px 0 #D2D7D3, 0 10px 0 #505551;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
.square a.active {
top: 10px;
color: #446CB3;
background-color: #446CB3;
-webkit-box-shadow: inset 0 1px 0 #B6D5FF, inset 0 -3px 0 #003D7E;
-moz-box-shadow: inset 0 1px 0 #B6D5FF, inset 0 -3pxpx 0 #003D7E;
box-shadow: inset 0 1px 0 #B6D5FF, inset 0 -3px 0 #003D7E;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="experiment">
<div id="shape_container">
<div ontouchstart="" class="square" id="b1"><a href="#">b1</a></div>
</div>
答案 1 :(得分:0)
更改
.square a:active{...}
到
.square a.active{...}
如下修改点击功能
$("#b1").click(function() {
// Toggle active class
$("#b1 > a").toggleClass('active');
});
请参见Codepen
中的工作示例