jquery-UI: Draggable div 只能垂直拖动,不能水平拖动
jquery-UI: Draggable div only drags vertically, not horizontally
我试图将我的可拖动区域限制在背景 (.bg) 上,但是一旦我这样做了,window 就只能垂直拖动,尽管背景的宽度为 100%。
我尝试了同样的方法,对“body”和“parent”都进行了约束,结果都出现了同样的问题。
我怎样才能让我的 window 可以向各个方向拖动?
$( function() {
$( ".draggable" ).draggable({
handle: ".window-titlebar",
scroll: false,
containment: ".bg"
});
} );
/* - - - GENERAL - - - */
body,html {
font-family: Roboto;
font-size: 18px;
height: 100%;
width: 100%;
margin: 0;
}
.bg {
background-image: url("http://abload.de/img/background4mqdx.jpg");
height: 100%;
width: 100%;
background-size: cover;
background-repeat: no-repeat;
}
/* - - - WINDOWS - - - */
.window {
display: flex;
flex-direction: column;
min-width: 30%;
width: fit-content;
padding: 4px 5px 5px 4px;
height: 70%;
box-shadow: -1px -1px 0px 0px #000 inset, 1px 1px 0px 0px #FFF inset, -2px -2px 0px 0px #868A8E inset;
background-color: #C2C5CA;
margin: 0 auto;
}
.window-titlebar {
display: block;
background: linear-gradient(to right, #000080, #0992ec);
position: relative;
width: 100%;
height: 25px;
color: #FFF;
}
.window-titlebar > .tab-icon {
margin-right: 5px;
}
.window-option-bar {
display: block;
position: relative;
width: 100%;
height: 18px;
padding: 5px 0px 8px 0px;
}
.window-option {
-webkit-user-select: none;
padding: 2px 5px 2px 5px;
cursor: pointer;
}
.window-option:hover {
box-shadow: 2px 2px 0px 0px #000 inset, -1px -1px 0px 0px #FFF inset;
}
.window-content {
margin-left: auto;
margin-right: auto;
display: block;
width: calc(98.5% - 7px);
height: 100%;
border: 2px solid #C2C5CA;
box-shadow: -1px -1px 0px 0px #000, 1px 1px 0px 0px #FFF;
background-color: #FFF;
overflow: scroll;
padding: 5px;
}
<!-- JavaScript -->
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<body>
<div class="bg">
<!-- Windows -->
<div class="window menu-context draggable" id="credits-window" >
<div class="window-titlebar">
<div class="tab-icon"></div> credits - Notepad
</div>
<div class="window-option-bar">
</div>
<div class="window-content">
hewwo
</div>
</div>
</div>
</body>
我想这个问题已经解决了,但以防万一。
只需将值为 absoulte
的 css 属性 position
添加到 .window
class,它应该可以工作。
$( function() {
$( ".draggable" ).draggable({
handle: ".window-titlebar",
scroll: false,
containment: ".bg"
});
} );
/* - - - GENERAL - - - */
body,html {
font-family: Roboto;
font-size: 18px;
height: 100%;
width: 100%;
margin: 0;
}
.bg {
background-image: url("http://abload.de/img/background4mqdx.jpg");
height: 100%;
width: 100%;
background-size: cover;
background-repeat: no-repeat;
}
/* - - - WINDOWS - - - */
.window {
position:absolute;
display: flex;
flex-direction: column;
min-width: 30%;
width: fit-content;
padding: 4px 5px 5px 4px;
height: 70%;
box-shadow: -1px -1px 0px 0px #000 inset, 1px 1px 0px 0px #FFF inset, -2px -2px 0px 0px #868A8E inset;
background-color: #C2C5CA;
margin: 0 auto;
}
.window-titlebar {
display: block;
background: linear-gradient(to right, #000080, #0992ec);
position: relative;
width: 100%;
height: 25px;
color: #FFF;
}
.window-titlebar > .tab-icon {
margin-right: 5px;
}
.window-option-bar {
display: block;
position: relative;
width: 100%;
height: 18px;
padding: 5px 0px 8px 0px;
}
.window-option {
-webkit-user-select: none;
padding: 2px 5px 2px 5px;
cursor: pointer;
}
.window-option:hover {
box-shadow: 2px 2px 0px 0px #000 inset, -1px -1px 0px 0px #FFF inset;
}
.window-content {
margin-left: auto;
margin-right: auto;
display: block;
width: calc(98.5% - 7px);
height: 100%;
border: 2px solid #C2C5CA;
box-shadow: -1px -1px 0px 0px #000, 1px 1px 0px 0px #FFF;
background-color: #FFF;
overflow: scroll;
padding: 5px;
}
<!-- JavaScript -->
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<body>
<div class="bg">
<!-- Windows -->
<div class="window menu-context draggable" id="credits-window" >
<div class="window-titlebar">
<div class="tab-icon"></div> credits - Notepad
</div>
<div class="window-option-bar">
</div>
<div class="window-content">
hewwo
</div>
</div>
</div>
</body>
我试图将我的可拖动区域限制在背景 (.bg) 上,但是一旦我这样做了,window 就只能垂直拖动,尽管背景的宽度为 100%。
我尝试了同样的方法,对“body”和“parent”都进行了约束,结果都出现了同样的问题。
我怎样才能让我的 window 可以向各个方向拖动?
$( function() {
$( ".draggable" ).draggable({
handle: ".window-titlebar",
scroll: false,
containment: ".bg"
});
} );
/* - - - GENERAL - - - */
body,html {
font-family: Roboto;
font-size: 18px;
height: 100%;
width: 100%;
margin: 0;
}
.bg {
background-image: url("http://abload.de/img/background4mqdx.jpg");
height: 100%;
width: 100%;
background-size: cover;
background-repeat: no-repeat;
}
/* - - - WINDOWS - - - */
.window {
display: flex;
flex-direction: column;
min-width: 30%;
width: fit-content;
padding: 4px 5px 5px 4px;
height: 70%;
box-shadow: -1px -1px 0px 0px #000 inset, 1px 1px 0px 0px #FFF inset, -2px -2px 0px 0px #868A8E inset;
background-color: #C2C5CA;
margin: 0 auto;
}
.window-titlebar {
display: block;
background: linear-gradient(to right, #000080, #0992ec);
position: relative;
width: 100%;
height: 25px;
color: #FFF;
}
.window-titlebar > .tab-icon {
margin-right: 5px;
}
.window-option-bar {
display: block;
position: relative;
width: 100%;
height: 18px;
padding: 5px 0px 8px 0px;
}
.window-option {
-webkit-user-select: none;
padding: 2px 5px 2px 5px;
cursor: pointer;
}
.window-option:hover {
box-shadow: 2px 2px 0px 0px #000 inset, -1px -1px 0px 0px #FFF inset;
}
.window-content {
margin-left: auto;
margin-right: auto;
display: block;
width: calc(98.5% - 7px);
height: 100%;
border: 2px solid #C2C5CA;
box-shadow: -1px -1px 0px 0px #000, 1px 1px 0px 0px #FFF;
background-color: #FFF;
overflow: scroll;
padding: 5px;
}
<!-- JavaScript -->
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<body>
<div class="bg">
<!-- Windows -->
<div class="window menu-context draggable" id="credits-window" >
<div class="window-titlebar">
<div class="tab-icon"></div> credits - Notepad
</div>
<div class="window-option-bar">
</div>
<div class="window-content">
hewwo
</div>
</div>
</div>
</body>
我想这个问题已经解决了,但以防万一。
只需将值为 absoulte
的 css 属性 position
添加到 .window
class,它应该可以工作。
$( function() {
$( ".draggable" ).draggable({
handle: ".window-titlebar",
scroll: false,
containment: ".bg"
});
} );
/* - - - GENERAL - - - */
body,html {
font-family: Roboto;
font-size: 18px;
height: 100%;
width: 100%;
margin: 0;
}
.bg {
background-image: url("http://abload.de/img/background4mqdx.jpg");
height: 100%;
width: 100%;
background-size: cover;
background-repeat: no-repeat;
}
/* - - - WINDOWS - - - */
.window {
position:absolute;
display: flex;
flex-direction: column;
min-width: 30%;
width: fit-content;
padding: 4px 5px 5px 4px;
height: 70%;
box-shadow: -1px -1px 0px 0px #000 inset, 1px 1px 0px 0px #FFF inset, -2px -2px 0px 0px #868A8E inset;
background-color: #C2C5CA;
margin: 0 auto;
}
.window-titlebar {
display: block;
background: linear-gradient(to right, #000080, #0992ec);
position: relative;
width: 100%;
height: 25px;
color: #FFF;
}
.window-titlebar > .tab-icon {
margin-right: 5px;
}
.window-option-bar {
display: block;
position: relative;
width: 100%;
height: 18px;
padding: 5px 0px 8px 0px;
}
.window-option {
-webkit-user-select: none;
padding: 2px 5px 2px 5px;
cursor: pointer;
}
.window-option:hover {
box-shadow: 2px 2px 0px 0px #000 inset, -1px -1px 0px 0px #FFF inset;
}
.window-content {
margin-left: auto;
margin-right: auto;
display: block;
width: calc(98.5% - 7px);
height: 100%;
border: 2px solid #C2C5CA;
box-shadow: -1px -1px 0px 0px #000, 1px 1px 0px 0px #FFF;
background-color: #FFF;
overflow: scroll;
padding: 5px;
}
<!-- JavaScript -->
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<body>
<div class="bg">
<!-- Windows -->
<div class="window menu-context draggable" id="credits-window" >
<div class="window-titlebar">
<div class="tab-icon"></div> credits - Notepad
</div>
<div class="window-option-bar">
</div>
<div class="window-content">
hewwo
</div>
</div>
</div>
</body>