Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrolling within a div without moving page

I have a <div id="innerContent"> with overflow-y:scroll;. Links to anchors within innerContent are located on parent page, not in the div.

So far, I have tried anchors and scrollto's to attempt to scroll within the content. They both complete the scroll, but innerContent's height is larger than the browser window, so the entire parent page also scrolls to the anchor when the links are clicked.

Is there a way to do this with javascript, without moving the parent page? I do not have control over the height of the div - this is someone else's design.

This came close... but there isn't an answer here. How to automatic scroll inline div without scrolling the whole page?

Thank you!

like image 637
amy Avatar asked Jun 14 '11 17:06

amy


1 Answers

This jsfiddle works in Chrome for me. Not tested in other browsers.

Catches the mousewheel event, uses the event data to scroll manually, then cancels the original event. Seems potentially messy for production.

$('#scroll').bind('mousewheel', function(e){

    $(this).scrollTop($(this).scrollTop()-e.originalEvent.wheelDeltaY);

    //prevent page fom scrolling
    return false;    
});
like image 57
Morlem Avatar answered Oct 12 '22 23:10

Morlem