I recently tried to create a situation where a user can move or shift an image and label controls to any position he or she wants inside a particular div <tag> on a web form. I was able to do that. However,
I discovered that when I put the cursor on the control that I want to move, the control moves away from the cursor. When the movement goes on, it does not make the web page to scroll if the parent image control is long in height.
Here is what I tried, if checked you will see what I mean. Please I need help in making it smooth movement. A situation where the control can be moved alongside the cursor’s movement
The mouse coordinates are relative to the window. The label and image are relative to their stacking container. If you want them to stay in view, you need to scroll the body or at least their stacking container.
You need to record the position of the control when mousedown, and also record the current position at all times for calculations when moving. Namely
pos3(clientX) and
pos4 (clientY).
Hope this can help you.
Best regards,
Xudong Peng
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
I later discovered that the script code only moves one control (which is the label control), but the image control could not be moved just like the label control.
I used the same code to try to move the image control but changed the label control name to that of image control and it did not work. And on the parent control, I have other label controls and image control which I will like to move to any position I want,
Not just a single label control. Can you please help me out?
Member
66 Points
175 Posts
Moving DIV, Image or label control with cursor in JavaScript
Oct 02, 2020 09:55 PM|georgeakpan233|LINK
Hello,
I recently tried to create a situation where a user can move or shift an image and label controls to any position he or she wants inside a particular div <tag> on a web form. I was able to do that. However,
I discovered that when I put the cursor on the control that I want to move, the control moves away from the cursor. When the movement goes on, it does not make the web page to scroll if the parent image control is long in height.
Here is what I tried, if checked you will see what I mean. Please I need help in making it smooth movement. A situation where the control can be moved alongside the cursor’s movement
HTML and JavaScript
All-Star
58194 Points
15655 Posts
Re: Moving DIV, Image or label control with cursor in JavaScript
Oct 04, 2020 05:35 PM|bruce (sqlwork.com)|LINK
The mouse coordinates are relative to the window. The label and image are relative to their stacking container. If you want them to stay in view, you need to scroll the body or at least their stacking container.
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context
Member
66 Points
175 Posts
Re: Moving DIV, Image or label control with cursor in JavaScript
Oct 05, 2020 11:26 AM|georgeakpan233|LINK
Hi bruce,
Please how do I scroll the body so that they stay in view?
Should I make these changes as shown below?
Changing from this
To this
Please I need guidance
Contributor
2090 Points
670 Posts
Re: Moving DIV, Image or label control with cursor in JavaScript
Oct 07, 2020 08:49 AM|XuDong Peng|LINK
Hi georgeakpan233,
According to your description, I created an example using your code, and found a simple tutorial and modified part of the code to fix it.
Result:
You need to record the position of the control when mousedown, and also record the current position at all times for calculations when moving. Namely pos3(clientX) and pos4 (clientY).
Hope this can help you.
Best regards,
Xudong Peng
Member
66 Points
175 Posts
Re: Moving DIV, Image or label control with cursor in JavaScript
Oct 07, 2020 10:31 AM|georgeakpan233|LINK
Hi XuDongPeng,
Thank you for the knowledge. I really appreciate your help.
-Georgeakpan233
Member
66 Points
175 Posts
Re: Moving DIV, Image or label control with cursor in JavaScript
Oct 24, 2020 10:04 PM|georgeakpan233|LINK
Hi XuDong Peng,
I later discovered that the script code only moves one control (which is the label control), but the image control could not be moved just like the label control.
I used the same code to try to move the image control but changed the label control name to that of image control and it did not work. And on the parent control, I have other label controls and image control which I will like to move to any position I want, Not just a single label control. Can you please help me out?
Here is what I did with the earlier code;