I have code like this:
$(function() {
var $selection = $('.selection');
$('li').filter(function() {
var self = $(this);
return /* ????? */;
}).addClass('selected');
});
.widget {
width: 320px;
height: 200px;
border: 1px solid gray;
position: absolute;
overflow: scroll;
}
.selection {
position: absolute;
top: 90px;
left: 90px;
border: 1px dotted black;
height: 120px;
width: 120px;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
float: left;
background: blue;
width: 40px;
height: 40px;
margin: 10px;
}
li.selected {
background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="widget">
<div class="selection"></div>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
How can I select only those li elements that are intersecting with .selection rectangle?
Using standard DOM techniques, you can iterate over each LI element and obtain a bounding rectangle, which gives the coordinates of the LI's rectangle.
Do this for the selection rectangle too and then you can simply check whether the coordinates are within the range of the selection's.
Please see below, the edited code which selects the LI elements that are fully contained or partially intersecting the selection.