簡體   English   中英

Javascript拖動選擇拖動區域周圍的文本和元素

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

我正在使用以下代碼來允許用戶垂直調整DIV元素的大小,但是當單擊並拖動手柄時,就選擇了頁面上的文本和其他元素,就好像用戶只是單擊並突出顯示頁面上的所有內容一樣。 有一種方法可以防止這種情況的發生,因為這看起來很糟糕? 這正在與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');
    });
}

在前面的問題和答案之后,添加一個小手柄元素,並將其作為唯一可以選擇和拖動的元素。 另外我猜你把div的位置設置為relative位置,把手柄的位置設置為absolute位置。 對??

在DIV中添加以下內容可解決此問題:

onselectstart="return false;"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM