ng-image-viewer could not move the image inside a parent element which is drag-scroll
Here is my HTML code:
<drag-scroll class="drag-box" style="width: 100%;" #nav>
Inside this drag scroll i have this ng-image-viewer element
<ng-image-viewer class="responsive" [src]="[images]"></ng-image-viewer>
Here are my css code:
.responsive {
width: 100%;
}
@media screen and (max-width: 1366px) {
.drag-box {
width: 1080px;
}
}
Normally i could drag it by double clicking the image and move around, But now that dragging event is taken for its parent element which is drag-scroll.
I want that image to move around when double click on it.
2
Answers
What we need to do to make that draggable.
Add a (dblclick) event listener to the ng-image-viewer element.
In the event listener, prevent the default behavior of the event.
Get the offsetLeft and offsetTop properties of the ng-image-viewer element.
Set the left and top CSS properties of the ng-image-viewer element to the offsetLeft and offsetTop properties, respectively.
Add a mousemove event listener to the document.
In the mousemove event listener, update the left and top CSS properties of the ng-image-viewer element based on the current mouse position.
Remove the mousemove event listener when the mouse button is released.
Update TypeScript:
Update HTML:
Updated CSS:
This will make the image in the ng-image-viewer element draggable when double-clicked, even if it is inside a drag-scroll element.
ng-image-viewer allows users to zoom in/out and drag the image for a better view. However, if the image is placed inside a parent element that has a dragging function, like drag-scroll, the drag event will be applied to the parent element instead, causing the image to be immovable.
To solve this issue, we can disable the dragging function of the parent element when the user double clicks on the image. This can be achieved by adding a double click event listener to the ng-image-viewer element and calling the
stopPropagation()
method to prevent the event from bubbling up to the parent element.HTML:
Component.ts file:
Now, when the user double clicks on the image, the drag event will not be applied to the parent element, allowing the image to be dragged and moved around.