我正在抽屉中工作,在单击按钮时必须将div从左向右移动。我正在尝试使用CSS过渡和jQuery来做到这一点,但是它不起作用。这是我到目前为止所尝试的:
$(document).ready(function() {
$(".openUserEditBox").on("click", function() {
$(".user-drawer").show();
});
});
.user-drawer {
position: relative;
width: 600px;
top: 50px;
background-color: #f2f2f2;
border: 1px solid #bbbbbb;
min-height: 450px;
float: right;
display: none;
left: 100px;
transition: left 1s ease-in-out;
}
.user-menu {
padding: 10px 20px;
position: absolute;
}
.btn-default {
background-color: #fff;
padding: 10px;
border: 1px solid #bbbbbb;
width: 150px;
}
.btn-default:hover {
cursor: pointer;
box-shadow: inset 0 1px 3px 0 #bbbbbb;
}
.btngroup {
float: right
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="btngroup">
<button class="btn-default openUserEditBox">Show Drawer</button>
<button class="btn-default closeUserEditBox">Hide Drawer</button>
</div>
<div class="userForm">
<div class="user-drawer">
<div class="user-menu">
<h1>Edit User</h1>
</div>
</div>
</div>
答案 0 :(得分:1)
如果要转换left
属性,则不能使用show()
和hide()
。只需在JavaScript中设置left
属性即可。
$(document).ready(function() {
$(".openUserEditBox").on("click", function() {
$(".user-drawer")[0].style.left = '0';
});
$(".closeUserEditBox").on("click", function() {
$(".user-drawer")[0].style.left = '602px';
});
});
.userForm {
overflow-x: hidden;
}
.user-drawer {
position: relative;
width: 600px;
background-color: #f2f2f2;
border: 1px solid #bbbbbb;
min-height: 450px;
margin-left:auto;
left: 602px;
transition:left 1s;
}
.btn-default {
background-color: #fff;
padding: 10px;
border: 1px solid #bbbbbb;
width: 150px;
}
.btngroup {
text-align:right;
margin:8px 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="btngroup">
<button class="btn-default openUserEditBox">Show Drawer</button>
<button class="btn-default closeUserEditBox">Hide Drawer</button>
</div>
<div class="userForm">
<div class="user-drawer">
<div class="user-menu">
<h1>Edit User</h1>
</div>
</div>
</div>
请注意,我取消了浮点数,因为它们会干扰用户表单中的overflow
。希望它仍能以当前形式使用。
答案 1 :(得分:1)
我只是使用绝对定位来显示隐藏部分。并且还删除了该部分的float属性以修复对齐方式。
$('.openUserEditBox').click(function() {
$(".userForm").removeClass("hide");
$(".userForm").addClass("show");
});
$('.closeUserEditBox').click(function() {
$(".userForm").removeClass("show");
$(".userForm").addClass("hide");
});
* {
box-sizing: border-box;
position: relative;
}
body {
overflow: hidden;
}
.user-drawer {
position: relative;
width: 600px;
top: 50px;
background-color: #f2f2f2;
border: 1px solid #bbbbbb;
min-height: 450px;
}
.user-drawer .active {
left: 20px;
transform: translateX(0%);
}
.userForm::after {
content: "";
clear: both;
display: table;
}
.clearfix::after {
content: "";
clear: both;
display: table;
}
.user-menu {
padding: 10px 20px;
position: absolute;
}
.btn-default {
background-color: #fff;
padding: 10px;
border: 1px solid #bbbbbb;
width: 150px;
}
.btn-default:hover {
cursor: pointer;
box-shadow: inset 0 1px 3px 0 #bbbbbb;
}
.btngroup {
text-align: right
}
.userForm {
position: absolute;
right: -100%;
visibility: visible;
transition: 0.6s;
}
.userForm.show {
visibility: visible;
right: 0;
}
.userForm.hide {
visibility: hidden;
right: -100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="btngroup">
<button class="btn-default openUserEditBox">Show Drawer</button>
<button class="btn-default closeUserEditBox">Hide Drawer</button>
</div>
<div class="userForm">
<div class="user-drawer">
<div class="user-menu">
<h1>Edit User</h1>
</div>
</div>
</div>
答案 2 :(得分:0)
您可以通过添加类并将其删除并在其上设置动画来做到这一点
$(document).ready(function() {
$(".openUserEditBox").on("click", function() {
$(".user-drawer").removeClass("slide-out");
$(".user-drawer").addClass("slide-in");
});
$(".closeUserEditBox").on("click", function() {
$(".user-drawer").removeClass("slide-in");
$(".user-drawer").addClass("slide-out");
});
});
.userForm {
overflow-x: hidden;
}
.user-drawer {
position: relative;
width: 600px;
background-color: #f2f2f2;
border: 1px solid #bbbbbb;
min-height: 450px;
margin-left:auto;
left: 100%;
}
.btn-default {
background-color: #fff;
padding: 10px;
border: 1px solid #bbbbbb;
width: 150px;
}
.btngroup {
text-align:right;
margin:8px 0;
}
.slide-in {
animation: slide-in 0.5s forwards;
-webkit-animation: slide-in 0.5s forwards;
}
.slide-out {
animation: slide-out 0.5s forwards;
-webkit-animation: slide-out 0.5s forwards;
}
@-webkit-keyframes slide-in {
100% {
left: 0;
}
}
@keyframes slide-in {
100% {
left: 0;
}
}
@-webkit-keyframes slide-out {
0% {
left: 0;
}
100% {
left: 100%;
}
}
@keyframes slide-out {
0% {
left: 0;
}
100% {
left:100%;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="btngroup">
<button class="btn-default openUserEditBox">Show Drawer</button>
<button class="btn-default closeUserEditBox">Hide Drawer</button>
</div>
<div class="userForm">
<div class="user-drawer">
<div class="user-menu">
<h1>Edit User</h1>
</div>
</div>
</div>