简体   繁体   English

Javascript拖动选择拖动区域周围的文本和元素

[英]Javascript dragging selects text and elements around the drag area

I'm using the following code to allow users to resize a DIV element vertically, but when the click and drag the handle, text and other elements on the page get selected as if the user was just clicking and highlighting everything on the page. 我正在使用以下代码来允许用户垂直调整DIV元素的大小,但是当单击并拖动手柄时,就选择了页面上的文本和其他元素,就好像用户只是单击并突出显示页面上的所有内容一样。 Is there a way to prevent this from happening as this looks very bad? 有一种方法可以防止这种情况的发生,因为这看起来很糟糕? This is being used with Prototype.js. 这正在与Prototype.js一起使用。

function DragCorner(container, handle) {
    var container = $(container);
    var handle = $(handle);

    // Add property to container to store position variables
    container.moveposition = {y:0};

    function moveListener(event) {
        // Calculate how far the mouse moved
        var moved = { y:(container.moveposition.y - event.pointerY()) };
        // Reset container's x/y utility property 
        container.moveposition = {y:event.pointerY()};
        // Update container's size
        var size = container.getDimensions();
        container.setStyle({height: size.height + moved.y + 'px'});
    }

    // Listen for 'mouse down' on handle to start the move listener
    handle.observe('mousedown', function(event) {
        // Set starting x/y 
        container.moveposition = {y:event.pointerY()};
        // Start listening for mouse move on body 
        Event.observe(document.body,'mousemove',moveListener);
    });

    // Listen for 'mouse up' to cancel 'move' listener 
    Event.observe(document.body,'mouseup', function(event) {
        Event.stopObserving(document.body,'mousemove',moveListener);
        console.log('stop listening');
    });
}

Following up your earlier question and answer, add a small handle element and make it as the only element that can be selected and draggable. 在前面的问题和答案之后,添加一个小手柄元素,并将其作为唯一可以选择和拖动的元素。 Also I guess you made that div's position as relative and handle position as absolute . 另外我猜你把div的位置设置为relative位置,把手柄的位置设置为absolute位置。 Right?? 对??

在DIV中添加以下内容可解决此问题:

onselectstart="return false;"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM