【问题标题】:jquery-UI: Draggable div only drags vertically, not horizontallyjquery-UI:可拖动的div仅垂直拖动,而不是水平拖动
【发布时间】:2021-10-05 11:05:32
【问题描述】:

我试图将我的可拖动区域限制为背景 (.bg),但是一旦我这样做了,窗口就只能垂直拖动,尽管背景的宽度为 100%。

我尝试将约束设置为“body”以及“parent”,但都导致了相同的问题。

我怎样才能让我的窗口可以向各个方向拖动?

$( 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>

【问题讨论】:

    标签: jquery jquery-ui jquery-ui-draggable


    【解决方案1】:

    我认为这个问题已经解决了,但以防万一。
    只需将值为absoulte 的css 属性position 添加到.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 {
        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>

    【讨论】:

      猜你喜欢
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 2011-08-16
      • 2013-03-25
      • 2017-04-18
      相关资源
      最近更新 更多