javascript - 向节点添加调整大小句柄

标签 javascript css

我有一个元素,我想在其上显示调整大小的句柄。我当前的代码:

const cursorMove = (e) => {
    if (!curData.doAnimate) {

        mouse.x = e.clientX;
        mouse.y = e.clientY;

        resize = {
            top: mouse.y < curPos.y + settings.resizeMargin,
            right: mouse.x > curPos.x + curPos.w - settings.resizeMargin,
            bottom: mouse.y > curPos.y + curPos.h - settings.resizeMargin,
            left: mouse.x < curPos.x + settings.resizeMargin,
        };
        const {top, right, bottom, left} = resize;

        if (top || left || right || bottom) {
            // mouse over border

            if ((top && left) || (bottom && right)) {
                wrap.style.cursor = 'nwse-resize';
            } else if ((top && right) || (bottom && left)) {
                wrap.style.cursor = 'nesw-resize';
            } else if (top || bottom) {
                wrap.style.cursor = 'ns-resize';
            } else if (left || right) {
                wrap.style.cursor = 'ew-resize';
            }
        } else {
            wrap.style.cursor = 'default';
        }
    }
};

wrap.addEventListener('mousemove', cursorMove);

如您所见,我使用 javascript 为鼠标移动添加事件监听器,每次鼠标移动时我都会检查它是否位于元素的边框上。如果我使用 curData.doAnimate 对节点执行某些操作,我可以关闭事件监听器。如果我移出元素,我可以删除事件监听器,但我认为这不会提高性能。

理想的解决方案是使用 Css 或没有事件监听器永久监听移动事件的解决方案。

提前谢谢你:)

编辑:我只对更改后的光标感兴趣 - 我已经有一个调整节点大小的函数(除非你知道使用一些魔法的 super 巧妙的方法)。

最佳答案

幸运的是,CSS 已经有一个属性来处理这个问题:resize

关于javascript - 向节点添加调整大小句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54546517/

相关文章:

twitter-bootstrap - 在网格中 Bootstrap 4 新行空间

css - Safari: :before-element in column-count on wrong position (bug?)

javascript - 使用javascript动态地将样式表添加到头部

javascript - 如何删除 ios 7 的 cordova 3.9.2 中的index.html

html - 响应式 flexbox 和重新排序子项

css - 有没有办法隐藏另一个元素下的部分?

javascript - 如何在 Backbone Marionette ui click 中获取被点击的元素

javascript - 如何与其他异步模块一起使用 async?

javascript - 如何在不重新加载页面的情况下更改框架源

javascript - 弹出图像并从现有 img 中查看大图