简体   繁体   English

使DIV关闭图标与DIV一起拖动

[英]Making the DIV close icon drag along with the DIV

Here is the Fiddle 这是小提琴

Can't figure out how to make the DIV close icon to follow the rest of the DIV when the DIV is dragged. 拖动DIV时,无法弄清楚如何使DIV关闭图标跟随DIV的其余部分。

I seem to think there is something in this part of the code that I am not doing right. 我似乎认为代码的这一部分中有些东西我做得不对。

$self = $(this);
$(opts.parent).append($self);
$('span.' + opts.classPrefix + '_oClose').remove();
$('body').append($overlayClose);
$('div.' + opts.classPrefix + '_overlay').remove();
$('body').append($overlay);
$self.draggable();

Appears to be something to do with how elements are appended to HTML body. 似乎与元素如何附加到HTML正文有关。 I tried appending a blank div to body and then adding overlay and overlayClose to that blank div. 我尝试将一个空白div添加到body,然后将overlay和overlayClose添加到该空白div。 But that did not even render the dialog. 但这甚至没有呈现对话。 Any ideas? 有任何想法吗?

As per Harrys demo http://jsfiddle.net/hari_shanx/8njzxhvx/3/ 根据Harrys演示http://jsfiddle.net/hari_shanx/8njzxhvx/3/

Line 24 has the magic :) Appending the close to the overlay then enabling the drag 第24行具有魔力:)将附近贴近叠加然后启用拖动

  $self.append($overlayClose);
  $self.draggable();

I don't know about your code but I think you need a parent div that would be relatively positioned and a child that is absolutely positioned. 我不知道你的代码,但我认为你需要一个相对定位的父div和一个绝对定位的子。
HTML HTML

<div id="test">Hello! Trying to figure how to make the close div icon drag along with the div. 
<img class="close" src="https://cdn3.iconfinder.com/data/icons/status/100/close_4-512.png" alt="">
</div>  

CSS CSS

#test {
    background: lightgrey;
    padding: 5px;
    position: relative;
    top: 100px;
}

img.close {
    position: absolute;
    right: 0px;
    top: 0px;
    height: 15px;
    width: 15px;
    cursor: pointer;
}

For dragging I've used jQuery UI : 对于拖动,我使用了jQuery UI

$( "#test" ).draggable();

$("img.close").click( function(e){
    $("#test").fadeOut();
}); 

I've made a fiddle that explains this: 我做了一个解释这个问题的小提琴:
FIDDLE 小提琴

Try something like this: 尝试这样的事情:

<div id="test">
  <a href="javascript:void(0)" class="close">Close</a>
Hello! Trying to figure how to make the close div icon drag along with the div.</div>

CSS: CSS:

.close{
 float: right;
 top: -31px;
 position: relative;
 left: 23px;
 background:url()// your close icon

} }

This will make your anchor tag draggable along with div and you can fire event on anchor tag for closing purpose 这将使您的锚标记与div一起可拖动,您可以在锚标记上触发事件以用于关闭目的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM