我有以下使用.draggable
的脚本,
我需要确保当用户单击并拖动文本时,文本在其父约束内水平滚动。
目前,我正在使用此代码,但无法正常工作。
怎么了?如何解决?
示例: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>
最佳答案
只需删除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>
关于javascript - jQuery draggable如何保持x约束并使拖动工作?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45647459/