Efficient HTML Div with hole in it

前端 未结 5 950
日久生厌
日久生厌 2021-01-20 20:32

Let me explain a bit more, I am trying to have a large area (full screen) with a fixed size hole in it. The hole will follow the mouse around the screen. Think of having a

相关标签:
5条回答
  • 2021-01-20 20:39

    Conclusions:

    • Having 4 divs and resizing them appears to be the fastest approach
    • Moving 4 divs is 4x slower
    • Moving 1 container div w/ 4 inner divs is just as slow (4x slower than resizing)
    • Using background-images is the only option that IE supports (as opacity filter (alpha) is too slow for whole page size divs).
    • background-image has to be solid (i.e. 1px solid colour with semi-opacity). Trying to get a solid color / semi-opaque color to achieve blur will cause screen repaint artifacts (even when not resizing/moving) divs.
    0 讨论(0)
  • 2021-01-20 20:46
    1. Have one div which is twice as wide and twice as tall as the viewport.
    2. Inside that, put your four divs to create the fixed-width hole in the middle.
    3. In your javascript, make the outer div move around with the cursor, rather than resizing the four divs.
    0 讨论(0)
  • 2021-01-20 20:47

    You may want to use the canvas tag, which can work on IE using excanvas, http://excanvas.sourceforge.net/, and you would then put the text inside the canvas tag. It can react to events as it is an html element, basically.

    0 讨论(0)
  • 2021-01-20 20:51

    I would suggest a single div with which is partially opaque. Have it have transparent background, but black borders. As the mouse moves around, adjust the thickness of the borders. Because it's partially opaque (say 50%), the part covered by the borders will seem grayed out. Remember, borders can be as thick as you want - even hundreds of pixels. This way you don't need to use images, or modify the DOM at all - just change the style properties of the single div.

    EDIT: Just noticed the requirement to let mouse events through. I believe the single div solution fails here. A four-div solution would need to be used. However, this can still be done by using black, semi-opaque divs (instead of background images). They can then be resized (via dynamically changing the style object) rather than removed and recreated (not sure if this is what OP means by "re-painting").

    0 讨论(0)
  • 2021-01-20 21:03

    Unfortunately, the DOM's design doesn't allow events to "pass through" a block to anything below it. This is why toolkits which allow drag 'n drop (mootools, jquery, etc...) are so complicated. They pre-calculate the location of all "droppable" elements and do mass comparison of coordinates of the dragged object v.s. the droppable objects, tied to the onmousemove events. That's one way around the limitation you could explore. Figure out what elements you want to react to your "window" and do the coordinate comparisons, etc...

    I've seen partial solutions that involve offsetting the dragged element from the mouse pointer, so that the pointer's "clear" and can properly trigger events, but that would most likely ruin the window effect you're going for.

    Most likey you're stuck dragging around those 4 divs. Do they have to have the background image? CSS opacity not a candidate?

    0 讨论(0)
提交回复
热议问题