我正在创建四个框,然后单击框打开,滑动休息三个框。问题是当我向父级添加一个类来滑动框时。它添加但在点击相同的框后没有删除。这是代码。
$(document).ready(function() {
$('.box-inner').click(function() {
$(this).siblings().removeClass('active');
if (!$(this).hasClass('active')) {
$(this).toggleClass('active');
}
//$(this).parent().removeClass('active');
//$(this).parent().addClass('active');
});
});

.main {
width: 100%;
float: left;
}
.box-main {
display: flex;
}
.box-inner {
height: 450px;
width: 25%;
display: flex;
position: relative;
transition: width 0.3s ease-in-out;
z-index: 1;
}
.box1 {
width: 100%;
background: #ccc;
border-right: 1px solid #333;
z-index: 0;
position: relative;
}
.box-2 {
width: 65%;
position: absolute;
left: 0;
height: 100%;
z-index: -1;
background: red
}
.box-main.active .box-inner.active {
width: 70%;
}
.box-inner.active .box1 {
width: 35%;
}
.box-inner.active .box-2 {
position: static;
width: 65%;
}
.box-main.active .box-inner:not(.active) {
width: 10%;
-webkit-transition: width .5s ease-in-out;
transition: width .5s ease-in-out;
}
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="main">
<div class="box-main">
<div class="box-inner">
<div class="box1"> hihjksdfsdfjsd </div>
<div class="box-2"> </div>
</div>
<div class="box-inner">
<div class="box1"> </div>
<div class="box-2"> </div>
</div>
<div class="box-inner">
<div class="box1"> </div>
<div class="box-2"> </div>
</div>
<div class="box-inner">
<div class="box1"> </div>
<div class="box-2"> </div>
</div>
</div>
</div>
&#13;
答案 0 :(得分:0)
The issue with your logic is due to the if
statement preventing you calling toggleClass()
on the element if it already has the class. Simply remove that if
condition:
$(document).ready(function() {
$('.box-inner').click(function() {
$(this).toggleClass('active').siblings().removeClass('active');
});
});
.main {
width: 100%;
float: left;
}
.box-main {
display: flex;
}
.box-inner {
height: 450px;
width: 25%;
display: flex;
position: relative;
transition: width 0.3s ease-in-out;
z-index: 1;
}
.box1 {
width: 100%;
background: #ccc;
border-right: 1px solid #333;
z-index: 0;
position: relative;
}
.box-2 {
width: 65%;
position: absolute;
left: 0;
height: 100%;
z-index: -1;
background: red
}
.box-main.active .box-inner.active {
width: 70%;
}
.box-inner.active .box1 {
width: 35%;
}
.box-inner.active .box-2 {
position: static;
width: 65%;
}
.box-main.active .box-inner:not(.active) {
width: 10%;
-webkit-transition: width .5s ease-in-out;
transition: width .5s ease-in-out;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="main">
<div class="box-main">
<div class="box-inner">
<div class="box1"> hihjksdfsdfjsd </div>
<div class="box-2"> </div>
</div>
<div class="box-inner">
<div class="box1"> </div>
<div class="box-2"> </div>
</div>
<div class="box-inner">
<div class="box1"> </div>
<div class="box-2"> </div>
</div>
<div class="box-inner">
<div class="box1"> </div>
<div class="box-2"> </div>
</div>
</div>
</div>