jquery-ui - 水平可拖动图像库上的“容器”元素问题
问题描述
我正在尝试制作一个通过水平拖动进行导航的图片库。我目前面临的问题是元素应该停止拖动时左右没有界限。我尝试过使用“容器”元素,但是当我这样做时,它会完全停止拖动。
我尝试过使用“父级”或实际的 div 作为容器,但都没有正常工作。我在另一个留言板上看到,在这种情况下使用 flexbox 会使事情变得更加复杂,所以我转而使用 display: inline-block on images。
这是我目前的草稿:https ://jsfiddle.net/samseurynck/ka1e9soj/21/
HTML
<div class="item_block_left">
<div class="item_block_left_gallery_container">
<div class="item_block_left_gallery">
<img class="item_block_left_gallery_item" src="https://placeimg.com/640/480/animals">
<img class="item_block_left_gallery_item" src="https://placeimg.com/200/200/animals">
<img class="item_block_left_gallery_item" src="https://placeimg.com/640/400/animals">
</div>
</div>
</div>
SCSS
.item_block_left{
height:200px;
width: 50%;
border: 1px solid pink;
overflow: hidden;
.item_block_left_gallery_container{
position: relative;
height:100%;
width: auto;
.item_block_left_gallery{
height:100%;
display: flex;
cursor: grab;
.item_block_left_gallery_item{
position: relative;
height:100%;
width:auto;
display: inline-block;
}
}
}
}
查询
$(".item_block_left_gallery").draggable({
scroll: false,
axis: "x",
});
预期的结果是只能水平滚动/拖动图像,左侧或右侧没有空白。
解决方案
工作示例:https ://jsfiddle.net/Twisty/4ak6q0zu/44/
JavaScript
$(function() {
var bounds = {
left: $(".item_block_left_gallery").position().left
};
bounds.right = bounds.left - $(".item_block_left_gallery").width() - $(".item_block_left").width() + 10;
$(".item_block_left_gallery").draggable({
scroll: false,
axis: "x",
drag: function(e, ui) {
var l = ui.position.left;
if (l > bounds.left) {
console.log("Hit Left Boundry");
ui.position.left = bounds.left;
}
if (l <= bounds.right) {
console.log("Hit Right Boundry");
ui.position.left = bounds.right;
}
}
});
});
使用drag
回调,您可以检查和设置position
可拖动项目的属性。基于拖动项目的左边缘,我们可以根据一些特定的边界检查和限制移动。右边似乎有一个 10px 的填充或边距,可能只是空白,所以我只是对此进行了调整。
查看更多:http ://api.jqueryui.com/draggable/#event-drag
希望有帮助。
推荐阅读
- nhibernate - 为什么 NHibernate 在执行查询之前会级联?
- solr - 在 Solr 中,我们可以使用 max score 而不是 sum for or 子句吗?
- html - 单击特定单选按钮时需要输入
- jupyter-notebook - Jupyterhub K8s - 将用户从 Jovyan 更改为 NB_USER 的问题
- python-3.x - 如何检查远程windows机器上是否安装了telnet客户端
- node.js - mongoose 查询数组,带 id 数组
- python - 运行远程 kubernetes 命令会产生错误,即使机器上的相同命令没有
- wordpress - 如何在我的自定义模板页面中获取类别明智的帖子
- javascript - 成功登录第一个网址后如何导航到第二个网址(angular7)
- sql - 从用户表单 VBA Excel 输入 SQL Server 的用户名和密码