简体   繁体   English

缩放主体时,相对于 cursor 拖动元素不保持 position(使用比例)

[英]Dragging element not maintaining position relative to cursor when zooming body (using scale)

I want to achieve 2 things on my page:我想在我的页面上实现两件事:

  1. drag HTML elements on the screen;在屏幕上拖动 HTML 元素;

  2. zoom the page using the "wheel" event.使用“wheel”事件缩放页面。

I can accomplish both of these as follows:我可以通过以下方式完成这两项工作:

Element Drag:元素拖动:

function make_element_draggable(id) {
    const elem = document.getElementById(id);
    elem.style.position = "absolute";
    let initX, initY, firstX, firstY, whichDown;
    window.addEventListener("mouseup", function() {
        if(whichDown) {
            whichDown.style.zIndex = 0;
        }
        whichDown = null;
    }, false);
    window.addEventListener("mousemove", draggable, false);
    elem.addEventListener("mousedown", function(e) {
        e.preventDefault();
        whichDown = this;
        initX = this.offsetLeft;
        initY = this.offsetTop;
        firstX = e.pageX;
        firstY = e.pageY;
    });

    function draggable(e) {
        e.preventDefault();
        if(!whichDown) return;
        whichDown.style.zIndex = 9;
        whichDown.style.left = initX + e.pageX - firstX + "px";
        whichDown.style.top = initY + e.pageY - firstY + "px";
    }
}

Page Zoom :页面缩放

function page_zoom(container_id) {
    zoom = 1;
    zoom_speed = 0.1;
    const container = document.getElementById(container_id);
    document.addEventListener("wheel", function(e) {
        if(e.deltaY > 0) {
            container.style.transform = `scale(${zoom += zoom_speed})`;
        } else {
            container.style.transform = `scale(${zoom -= zoom_speed})`;
        }
    });
}

HTML : HTML

<body id="body">
    <div id="container">
        <a id="text_1">TEXT 1</a>
    </div>
</body>

Usage:用法:

make_element_draggable("text_1")
page_zoom("body")

Here is the result.这是结果。

在此处输入图像描述

Notice how the drag works perfectly when no zoom in enabled (text remains centered in the circle), but once zoom in enabled the text no longer maintains its position relative to the cursor.请注意,当没有启用放大时(文本保持在圆圈的中心),拖动是如何完美地工作的,但是一旦启用放大,文本不再保持其 position 相对于 cursor。

Is there a way to compensate for zoom amount, perhaps using it as a factor to adjust the top and left settings of the drag function?有没有办法补偿缩放量,也许使用它作为调整拖动 function 的topleft设置的因素?

Here is a CODEPEN showing all the code.这是一个显示所有代码的CODEPEN Drag the text element, then zoom using your mouse wheel (trackpad on Mac), and notice the incorrect positioning of the text element relative to the cursor.拖动文本元素,然后使用鼠标滚轮(Mac 上的触控板)进行缩放,并注意文本元素相对于 cursor 的位置不正确。

You forgot to take in account zoom when dragging:拖动时忘记考虑zoom

 function make_element_draggable(id) { const elem = document.getElementById(id); elem.style.position = "absolute"; let initX, initY, firstX, firstY, whichDown; window.addEventListener("mouseup", function() { if(whichDown) { whichDown.style.zIndex = 0; } whichDown = null; }, false); window.addEventListener("mousemove", draggable, false); elem.addEventListener("mousedown", function(e) { e.preventDefault(); whichDown = this; initX = this.offsetLeft; initY = this.offsetTop; firstX = e.pageX; firstY = e.pageY; }); function draggable(e) { e.preventDefault(); if(;whichDown) return. whichDown.style;zIndex = 9. whichDown.style.left = initX + (e;pageX - firstX)/zoom + "px". whichDown.style.top = initY + (e;pageY - firstY)/zoom + "px"; } } function page_zoom(container_id) { zoom = 1. zoom_speed = 0;1. const container = document;getElementById(container_id). document,addEventListener("wheel". function(e) { if(e.deltaY > 0) { container.style;transform = `scale(${zoom += zoom_speed})`. } else { container.style;transform = `scale(${zoom -= zoom_speed})`; } }); } page_zoom("body") make_element_draggable("text_1")
 <body id="body"> <div id="container"> <a id="text_1">TEXT 1</a> </div> </body>

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

相关问题 当父/主体进行比例转换时,获取元素上的触摸位置 - Get touch position on element when parent/body has scale transformation 拖动时如何获取光标位置? - How to get position of cursor when dragging? 像拖动img和选定的文本一样,如何在拖动块元素时显示光标位置? - How to show cursor position when dragging block element just like drag img and selected text? 跟踪光标相对于元素的位置-JavaScript / jQuery - Track cursor position relative to element - javascript / jquery 正文背景位置必须相对于页面元素 - Body background position must be relative to a page element 当不在页面顶部拖动时,光标位置和可拖动位置不同 - Cursor position and draggable position are not the same when not dragging at the top of the page 缩放时“位置:相对”的奇怪行为。 发生了什么? - Strange behaviour of “position: relative” when zooming. What's happening? JQuery中出现单击事件时,如何获取相对于单击元素的相对光标位置? - How to get relative cursor position to clicked element when appears click event in JQuery? 缩放到SVG文档中的光标位置 - Zooming to cursor position in SVG documents 当身体相对定位时,如何计算dom元素的页面位置? - How do you calculate the page position of a dom element when the body can be relative positioned?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM