简体   繁体   English

当css作为绝对位置并且其父div将css作为相对位置时,如何使图像可拖动?

[英]How to make a image draggable when it has css as position absolute and it's parent div has css as position relative?

This is my part of code: 这是我的代码部分:

<div id="canvasImg" class="img-content position-relative">
    <loading></loading>
    <canvas id="Canvas" class="position-absolute"></canvas>
</div>

CSS used: 使用的CSS:

.position-relative{
    position: relative;
}
.position-absolute{
    position: absolute;
}

After generating the canvas image, I'm creating the some image dynamically. 生成画布图像后,我将动态创建一些图像。

var createCanvasImgDiv = function (markerDetails) {
            var result = "<img src='../../../../../sharedAssets/images/device_marker.png'"
                    + "class='position-absolute marker-Img'"
                    + "id='" + markerDetails.deviceId + "'"
                    + "name='" + markerDetails.externalName + "'"
                    + "radius='" + markerDetails.radius + "'"
                    + "emailTags='" + markerDetails.emails.join(",") + "'"
                    + "/>";
            return result;
        };
_.map(deviceMarkers, function (tempMarker) {
                var imagDiv = createCanvasImgDiv(tempMarker);
                $("#canvasImg").append(angular.element(imagDiv));
                $compile(angular.element(imagDiv))($scope);
            });

Now I want the <img> object to move anywhere inside the parent div(id=canvasImg) on drag of mouse. 现在,我希望<img>对象在鼠标拖动时移动到父div(id = canvasImg)内的任何位置。 Also I want the mouse position when it drops. 另外,当鼠标下降时,我还希望其位置。

Please help me on this. 请帮我。 Thanks in advance. 提前致谢。

I've used jquery-ui draggable widget to achieve this. 我使用了jquery-ui可拖动小部件来实现此目的。

 $("#image").draggable({ containment: "parent" }); $("#image").on("dragstop", function(event, ui) { alert("mouse position: X:" + event.pageX + ", Y:" + event.pageY); }); 
 .position-relative { position: relative; } .position-absolute { position: absolute; } 
 <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script> <div id="canvasImg" class="img-content position-relative" style="width:300px;height:200px;border:1px solid #000000;"> <loading></loading> <canvas id="Canvas" class="position-absolute" style="width:200px;height:100px;border:1px solid #000000;"></canvas> <img id="image" class="position-relative" src="http://wallpaper.sc/en/ipad/wp-content/uploads/2014/10/ipad-2048x2048-thumbnail_00721-256x256.jpg" style="width:25px;height:25px;" /> </div> 

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

相关问题 CSS:position absolute 的屏幕宽度与 position 在出现垂直溢出时固定在 chrome 中存在冲突 - CSS: position absolute has conflicting screen width with position fixed in chrome when there's a vertical overflow CSS - 图像推子的绝对和相对位置 - CSS - Absolute and Relative Position with image fader 如何使用父div id更改绝对位置div的css属性 - how to change the css property of absolute position div with parent div id 如何定位 <div> 绝对对父母和相对邻居 - How to position <div> absolute to parent and relative to neighbors 您可以将带有 position:relative 的图像与具有 position:absolute 的图像对齐吗? - can you align an image with position:relative with an image that has position:absolute? 当父div具有position:relative时,pageY计算错误 - wrong pageY calculation when parent div has position:relative CSS:相对位置在相对位置div中的绝对位置 - CSS: Relative position in absolute position in a relatively positioned div CSS position 当父元素隐藏溢出时出现问题 - CSS position issue when parent element has overflow hidden 获取父DIV具有绝对位置的DIV的高度(JQuery) - Get the height of a DIV where the parent DIV has position absolute (JQuery) CSS位置问题:相对位置:绝对孩子 - Problem with CSS postion: relative with position: absolute children
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM