views:

353

answers:

2

On my web page, I a table inside a DIV that is scrollable.

When I scroll down, I want to highlight the center most viewable row in my table.

How would I do that?

I found the following script that is close to what I desire --> www.rgagnon.com/jsdetails/js-0093.html

But this works only on the MouseOver event. I want this to work on not just the MouseOver event but also when I simply scroll up/down.

+2  A: 

Use the scroll event.

For example: (EDIT: Finally tested)

var scrollElem = $('div#panel-hlisting-all');
scrollElem.scroll(function() {
    var scrollElemPos = scrollElem.offset();

    var newCenter = $(document.elementFromPoint(
        scrollElemPos.left + scrollElem.width()  / 2,
        scrollElemPos.top  + scrollElem.height() / 2)
    ).closest('.hlisting');

    if(newCenter.is(".CenterRow")) return;
    $('.CenterRow').removeClass("CenterRow");
    newCenter.addClass('CenterRow');
});

EDIT: I changed the code to work with a specific element's scrollbar.
3rd EDIT: I updated the code to prevent flicker.

SLaks
But I only want to highlight one (1) row, that being the center most row in the scroll window. Make Sense?
TeddyH
Meaning, let's imagine that I have 10 row in my table but my viewable scroll window can only display at most 3 rows at a time. So initially, row 1-2-3 will be displayed. In this case, row 2 would be highlighted. Now let's say the person scrolls down a bit and rows 2-3-4 are now displayed. Now, row 3 would be the only row highlighted. Scroll much futher, let's now say rows 7-8-9 a viewable. As such, now row 8 is highlighted.
TeddyH
My code should do exactly that. Note that I haven't actually tried it.
SLaks
Nope :( I just tried the code above and my page now won't load. Does the JQuery scroll event need any libraries other than the core/base JQuery?
TeddyH
@SLake, any suggestions? You're code looks promising but it's freezing my page from loading
TeddyH
Try debugging the code in FIrebug or IE8's new debugger.
SLaks
You mind sharing how I would debug using Firebug. I just installed the Firefox plugin but don't know how to use it
TeddyH
I see the DOM tab of Firebug. I assume I want to look at that tab but don't know what I'm looking at :( thanks in advance
TeddyH
It gives an error saying your code above is "missing ) after argument list .closest('tr').addClass('CenterRow');"
TeddyH
Also, does you script above work on the DIV scroll window or the actual page scrollbar? I want this to only fire on the DIV scrollbar and not the overall page scrollbar
TeddyH
I changed the code to be ".closest('tr').addClass('CenterRow'));" (note the additional ")" at the end. However, now I receive an Firebug error stating the "elementFromPoint is not a function"
TeddyH
`elementFromPoint` should be a function: https://developer.mozilla.org/En/DOM/document.elementFromPoint. Check the spelling.
SLaks
Almost working. The updated code above now doesn't error and is fired on the DIV scrollbar ... BUT, it doesn't appear to be adding the CSS CenterRow. I added to my CSS .CenterRow{background:red} but the row never seems to turn read. I added a console.log('did the person scroll') and that prints out correctly. Any idea why my .CenterRow CSS might be added?
TeddyH
Last thing to note, instead of a table - I am using my own custom DIV. The structure is <div class="listing">...</div><div class="listing">...</div>. So I replaced ".closest('tr').addClass('CenterRow');" with ".closest('.listing').addClass('CenterRow');" though it still doesn't seem to be adding the CenterRow CSS element :( Any ideas
TeddyH
Check what `elementFromPoint` and `closest` are returning using `console.log`.
SLaks
They return []. When I put "console.log($(document.elementFromPoint(scrollElem.scrollLeft() + scrollElem.width() / 2, scrollElem.scrollTop() + scrollElem.height() / 2)) .closest('.hlisting').addClass('CenterRow'));" in my code, it prints out to Firebug just "[]"
TeddyH
@SLake, I have updated my original post to include a link to my live web site so you can view it
TeddyH
The problem was that my code was getting the location relative to `scrollElem` and looking for the element relative to `document`. I fixed it; try again.
SLaks
You're AWESOME. Thanks
TeddyH
A: 

Try utilize the jQuery Scroll event.

The MYYN