亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

javascript - Implement the mouse to draw a div frame and obtain the dom element covered by the div
僅有的幸福
僅有的幸福 2017-05-19 10:09:21
0
4
686

Similar to this effect, move the mouse and pull a p box to select the dom element covered by p.
Do you have any good ideas that can be implemented


The method I came up with is to judge the offsetLeft, offsetTop of p and the offsetLeft, offsetTop of each element that needs to be selected. If the dom element is smaller than the offsetLeft Top of p, it is selected by the frame.
But the bad thing is that you need to traverse the offset attribute value of each element every time. Because there are many elements, there are hundreds. 1. Obtaining the attribute value will cause a lot of backflow, 2. It is too much to judge each time.


So I want to brainstorm and ask for help if anyone has a better way


I looked for the effect of a network disk before, and wanted to express it vividly, but that one was relatively simple, and the scene was really a little more complicated

僅有的幸福
僅有的幸福

reply all(4)
Ty80

It depends on how you think about it. I provide a simple method.
When mousemove, get e.target and save it, and then filter out what you want through the function function. It’s that simple

伊謝爾倫

Take Baidu Netdisk as an example. My idea is: because the height of each list item is fixed, you only need to compare the intersection of the rectangle formed when the mouse is dragged with the rectangle of the entire list element, and the amount of calculation will be much smaller. .

The specific method is:

    When
  1. mousedown, calculate the starting list item index
    2.(offsetTop of 起始的列表項(xiàng) + offsetTop of mouseup) / height of the list item, and then ceil the result to get the selected number.

  2. We have the index of the starting list item and the number of selected items, so it is easy to calculate which elements are selected.

給我你的懷抱

The height of the parent container, the scrollTop/list item height of the parent container is fixed, the relative top of the rectangle, and the height of the rectangle
These variables should be enough

大家講道理
按下標(biāo)記關(guān)閉;
按下點(diǎn)坐標(biāo) = {x: null; y: null};
抬起點(diǎn)坐標(biāo) = {x: null; y: null};
已選取元素?cái)?shù)組 = [];
document.onmousedown {
    按下標(biāo)記開啟
    按下點(diǎn)坐標(biāo) = {x: 鼠標(biāo)當(dāng)前橫向偏移; y: 鼠標(biāo)當(dāng)前縱向偏移};
}

待選元素.onmouseover {
    如果按下標(biāo)記開啟,加入已選取元素?cái)?shù)組
}

document.onmouseup {
    按下標(biāo)記關(guān)閉
    清空已選取元素?cái)?shù)組
    抬起點(diǎn)坐標(biāo) = {x: 鼠標(biāo)當(dāng)前橫向偏移; y: 鼠標(biāo)當(dāng)前縱向偏移};
}

利用已選取元素?cái)?shù)組操作已選中的元素
利用按下點(diǎn)坐標(biāo)、抬起點(diǎn)坐標(biāo)繪制框選層

The above is the complete logic, please try to write the code yourself!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template