最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery | Get div elements in defined area - Stack Overflow

programmeradmin6浏览0评论

is there a simple way to get the div elements fitting pletely in a defined area?

Example:

<div id="redbox"> RESIZE DIV </div>

<div id="grid">
  <div id="box1"></div>
  <div id="box2"></div>
  <div id="box3"></div>
  <div id="box4"></div>
</div>

I got 4 Boxes (grey) and I am able to resize a div (red on top of all boxes). After resize I want to know which of the div elements are fitting pletely in this area.

Does anyone knows how to do this? Is there a method or function in JQUERY?

is there a simple way to get the div elements fitting pletely in a defined area?

Example:

<div id="redbox"> RESIZE DIV </div>

<div id="grid">
  <div id="box1"></div>
  <div id="box2"></div>
  <div id="box3"></div>
  <div id="box4"></div>
</div>

I got 4 Boxes (grey) and I am able to resize a div (red on top of all boxes). After resize I want to know which of the div elements are fitting pletely in this area.

Does anyone knows how to do this? Is there a method or function in JQUERY?

Share Improve this question edited Jun 27, 2012 at 13:07 frgtv10 asked Jun 27, 2012 at 13:02 frgtv10frgtv10 5,4604 gold badges32 silver badges46 bronze badges 1
  • 3 Upvoting for having the patience to attach a meaningful graphic. :) – Cranio Commented Jun 27, 2012 at 13:07
Add a ment  | 

3 Answers 3

Reset to default 10

It looks to me like the withinBox plugin might help you solve this (jquery.fn.withinBox). You could use the code like this:

var area = $('#redbox'),
    offset = area.offset(),
    selected = $('#grid div').withinBox(offset.left,
                                        offset.top,
                                        area.width(),
                                        area.height()
                                        );

What I'd do with JQuery is loop through all the elements to check, getting their .offset() value, plus width and height.

Then for each element I'll get these four valuyes:

X1 (offset().top)
Y1 (offset().left)
X2 (= X1 + width)
Y2 (= Y1 + height)

I will use these values to check the following four points (still for each element to check)

x1,y1  x1,y2  x2,y2  x2,y1  (the four corners)

Provided we have done the same tring with the "covering" DIV (retrieving it's four corners xd1, yd1, xd2, yd2), I will reason like this:

If one or more of these points falls into my "covering" DIV, then consider the DIV "covered".

Edit: I didn't know there was a plugin for that, I guess it's internal mechanics are like mine, but I bet it's a more simple solution than mine :)

I don't think there is already plete solution for this. According that this may various.

But you can use document.elementFromPoint function for that.

Knowing absolute position of re-sizable div you can create map of coordinates and then see what elements under that div.

发布评论

评论列表(0)

  1. 暂无评论