我有以下html结构:
HTML
<div id="outer">
<div id="inner">
<div id="bounds">
<div id="myimage">
</div>
</div>
</div>
</div>
CSS
#outer
{
border: solid 1px red;
padding: 50px 30px 50px 30px;
float: left;
}
#inner
{
width: 300px;
height: 300px;
border: solid 1px black;
overflow:hidden;
position:relative;
}
#myimage
{
background-image:url('http://upload.wikimedia.org/wikipedia/commons/9/9a/Sample_Floorplan.jpg');
background-position : 0,0;
background-repeat:no-repeat;
width: 648px; /*TODO : size will be dynamic*/
height: 438px; /*TODO : size will be dynamic*/
position:relative;
}
JS
$("#myimage").draggable(); //{ containment: "#bounds" }
//$("#bounds").width(); //? value
//$("#bounds").height(); //? value
//$("#bounds").css("left",""); //? value
//$("#bounds").height("top","");? value
一个“内部”div,它有一个带有background-image
的可拖动子div
内部div有overflow:hidden
。我需要的是限制图像的拖动
整个图像可以滚动,但图像永远不会被完全拖出“内部”div(图像的某些部分必须始终保持可见)。
这就是我添加bounds
div的原因。我如何定义尺寸和位置
bounds
div使我能够限制图像的移动。
这样我就可以写了
$("#myimage").draggable({ containment: "#bounds" });
答案 0 :(得分:1)
您需要使用
创建一个div保留图像的宽度
顶部 - 图像的高度
宽度为宽度* 2 +视口宽度(300)
高度为高度* 2 +视口高度(300)
HTML
<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<div id="outer">
<div id="inner">
<div id="bounds">
</div>
<div id="myimage">
<div>
</div>
</div>
</body>
</html>
css
#outer
{
border: solid 1px red;
padding: 50px 30px 50px 30px;
float: left;
}
#inner
{
width: 300px;
height: 300px;
border: solid 1px black;
overflow:hidden;
position:relative;
}
#myimage
{
background-image:url('http://upload.wikimedia.org/wikipedia/commons/9/9a/Sample_Floorplan.jpg');
background-position : 0,0;
background-repeat:no-repeat;
width: 648px;
height: 438px;
position:relative;
}
的javascript
var imgwidth = "648";
var imgheight = "438";
$("#bounds").css({
position:"absolute",
left: -imgwidth + 10,
top:-imgheight + 10,
width: imgwidth * 2 + 300 - 20,
height:imgheight * 2 + 300 - 20,
}
);
$("#myimage").draggable({ containment: "#bounds" });
查看此垃圾箱