当我按下切换按钮时,jquery将'.active'
类添加到'.sidebar-contact'
,然后在CSS .sidebar-contact.active
中应该激活,但是什么也没发生。
.sidebar-contact{
position: fixed;
transform: translateY(-50%);
top: 50%;
left: -350px;
width: 350px;
height: auto;
padding: 20px;
background: #fff;
box-sizing: border-box;
transition: .5s;
}
.sidebar-contact.active
{
left:0px;
}
.toggle{
position: absolute;
top: 0;
right: -48px;
width: 48px;
height: 48px;
line-height: 48px;
cursor: pointer;
text-align: center;
background: #f00;
}
$(document).ready(function(){
$('.toggle').click(function(){
$('.sidebar-contact').toggleClass('.active')
});
});
答案 0 :(得分:3)
您需要在.
中的类定义之前删除toggleClass
:
$('.toggle').click(function(){
$('.sidebar-contact').toggleClass('active')
});
.sidebar-contact {
background: blue;
height: 200px;
width: 200px;
}
.sidebar-contact.active {
background: green;
}
button { margin-top: 20px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sidebar-contact"></div>
<button class="toggle">Toggle</button>