In a web application that I am writing, I have a series of divs which create a grid. The grid is X divs by Y divs, based on user input. Using jQuery hover function, I would like to change the background color of all surrounding divs within a certain distance. Basically, if I hover over a div, all divs within 4 rows and 4 columns away should also change their background color. I can get this functioning fine, but when the grid becomes 32 by 128 divs there is a real performance issue and the hover effect noticeably lags behind the mouse. I am almost certain that it is because of the large amount of similar divs within the DOM because the issue is not there when the grid is something like 30 by 30.
This is the basic structure of my html:
<div class="table_area">
<div class="table_row" id="row-0">
<div class="cap" data-row="0" data-column="0"></div>
<div class="cap" data-row="0" data-column="1"></div>
...
</div>
<div class="table_row" id="row-1">
<div class="cap" data-row="1" data-column="0"></div>
<div class="cap" data-row="1" data-column="1"></div>
...
</div>
...
</div>
To try to speed up the search of the DOM, I have added each row to an array. Thus, $('div.table_row[data-row="0"]')
would be in arr[0]
. So when a div
in row 8 is hovered, I only check arr[4]
through arr[12]
for the necessary divs.
I would think that this would speed up the process quite a bit, since I am eliminating a substantial amount of the searching, but there is still a very noticeable lag in the hover.
Is there anything blatantly wrong with how I set this up? I am using the latest version of Chrome, if that matters.