[英]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.