2017-08-12 59 views
0

我有以下腳本,其中使用.draggable, 我需要確保當用戶單擊並拖動文本時,文本在其父級約束內水平滾動。jquery draggable如何保持x約束並使拖動有效?

當前我正在使用此代碼,但無法正常工作。 有什麼不對?如何解決它?

例子:http://jsfiddle.net/t1smb6u8/

jQuery("#child").draggable({ 
 
    cursor: "move", 
 
    axis: "x", 
 
    containment: "parent" 
 
});
.parent { 
 
    width: 300px; 
 
    border: 5px solid red; 
 
    overflow: scroll; 
 
} 
 

 
#child { 
 
    width: 1000px; 
 
    padding: 10px; 
 
}
<script src="//code.jquery.com/jquery-1.10.2.js"></script> 
 
<script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script> 
 
<div class="parent"> 
 

 
    <div id="child"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It 
 
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop 
 
    publishing software like Aldus PageMaker including versions of Lorem Ipsum </div> 
 

 

 
</div>

回答

0

只是刪除containment: "parent"

jQuery("#child").draggable({ 
 
    cursor: "move", 
 
    axis: "x", 
 
    //containment: "parent" 
 
});
.parent { 
 
    width: 300px; 
 
    border: 5px solid red; 
 
    overflow: scroll; 
 
} 
 

 
#child { 
 
    width: 1000px; 
 
    padding: 10px; 
 
}
<script src="//code.jquery.com/jquery-1.10.2.js"></script> 
 
<script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script> 
 
<div class="parent"> 
 

 
    <div id="child"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It 
 
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop 
 
    publishing software like Aldus PageMaker including versions of Lorem Ipsum </div> 
 

 

 
</div>

+0

它不工作應該只在其大小範圍內滾動對象,當用戶滾動文本時,它可以滾動很多以至於在紅色框中您只看到白色。我需要應用一個限制滾動限制。有任何想法嗎?謝謝 – Radex

+0

@Radex在dragbble的「開始」中添加約束條件,如果它向左或向右返回false –

+0

您可以添加一個現場示例,以便我可以接受您的答案。謝謝! – Radex

相關問題