1

我有一个 div 我想拖动限制在一个特定的容器中。我尝试使用包含:父选项,但这会导致 div 在不拖动的情况下捕捉到上限或下限。我有溢出:隐藏设置,我希望这不是问题。(我在某处读过)

请帮帮我。

编码:

imgCanvas.appendChild(img); 
overlay.appendChild(imgContainer);

$(document).ready(function() { 
  $("#draggable").draggable({
    containment: '#imgContainer', 
    scroll: false
  });                                                       
}); 

#draggable 是一个包含img 的div,而#draggable 是附加到imgContainer 的。我希望这会有所帮助。

4

2 回答 2

2

以下内容对我来说非常有效(尽管您必须修改包含图以匹配您的可拖动元素尺寸):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
  <head>
    <title>jQuery</title>
      <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>

        <script type='text/javascript'>

          $(document).ready(function(){
            $("#draggable").draggable({
              containment: [-150,-150,0,0],
              scroll: 'false'
            });
          });

        </script>
        <style type="text/css">
          #container {
            width:200px;
            height:200px;
            border:1px solid #cccccc;
            overflow:hidden;
          }
          #draggable {
            width:359px;
            height:359px;
            background:#cc0000 url("http://www.sudoku.4thewww.com/Grids/grid.jpg");
          }
        </style>
      </head>
    <body>

      <div id="container">
        <div id="draggable"></div>
      </div>

    </body>
</html>
于 2009-06-24T01:17:31.467 回答
0

我最近遇到了同样的问题。我将我的代码提取到一个插件中,我认为它完全符合您的要求......

详情在这里:

http://webpangea.blogspot.com/2009/10/draggable-scrolling-list-with-jquery.html

于 2009-10-13T10:01:16.770 回答