Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

jquery - Drag div by handle that is not nested

In the JQueryUI draggable demo, I can see you can attach a handle to a DIV but if the handle is not nested within the parent DIV that is draggable, it doesn't work e.g.

<script src="jquery-1.5.2.js"></script>
  <script src="js/jquery-ui-1.8.11.custom.min.js"></script>

  <style>    
    #hBlack
    {
        width:55px;
        height:55px;
        background-color: black;
        top:0px;
    }

    #hGreen
    {
        width:25px;
        height:25px;
        background-color: green;


    }
  </style>
  <script language="javascript" type="text/javascript">
    $(function() {
      $("#hBlack").draggable({handle:"#hGreen"});
      });
  </script>
</head>

<body>
  <div id="hBlack">
</div>
<div id="hGreen"></div>

The above doesn't make #hGreen the handle - but the following does:

<div id="hBlack">
  <div id="hGreen"></div>
</div>

Essentially, I am trying to make one DIV move when another moves - I guess you can do it with the new Position utility but for a newbie like me I find it poorly documented

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One possible solution could be to have the handle inside, but use css to position it outside.

Javascript:

$("#draggable").draggable({handle:"#handle"});

HTML:

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

CSS:

#draggable{
display: block; 
height: 300px; 
width: 600px; 
background-color: gray;
}
#handle{
display: block; 
height: 50px; 
width: 600px; 
background-color: green;
position: relative;
top: -30px;
}

Otherwise, you might have to do something similar to a multiselect draggable. How do I drag multiple elements at once with JavaScript or jQuery?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...