Reputation: 3410
I have a table
<table>
<tr> <td> ---- asp.net gridview </td>
<td> <div class=persondetails> </div> </td></tr>
</table>
My javascript got from another forum article
<script language="javascript" type="text/javascript">
var personDetail = document.querySelector('.persondetails');
alert (personDetail);
var origOffsetY = personDetail.offsetTop;
debugger;
function onScroll(e) {
debugger;
window.scrollY >= origOffsetY ? personDetail.classList.add('sticky') :
personDetail.classList.remove('sticky');
}
document.addEventListener('scroll', onScroll);
</script>
My asp.net gridview grows vertically depending on the no. of persons. When a person on the grid is selected, the div on the second td is populated with some fields. It works fine.
When the list grows too large, and I have to select a person at the bottom, the person details on the right side is shown at the top. This makes users to scroll up to see the details of the person selected in the grid at the bottom of the page.
Is there a way to show the div on the right side td to scroll down when users scroll down the page? I checked other links on this forum but didn't work out. I'm not sure if this due to my table and td layout.
I get null at the alert specified.
Upvotes: 0
Views: 7050
Reputation: 16714
You should use a position fixed
like this:
var $details = $(".details"),
detailsPos = $details.position().top;
$(window).on("scroll", function() {
if ($(window).scrollTop() > detailsPos)
$details.css("position", "fixed").css("top",0);
else
$details.css("position", "static");
});
Note that the code should be wrapped in document ready
like this:
$(function() {
// ... code ...
});
Also check position fixed
compatibility here
Upvotes: 3
Reputation: 2742
The better approach would be to use CSS styled divs.
Upvotes: 0