最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Scrolling within a div without moving page - Stack Overflow

programmeradmin4浏览0评论

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!

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!

Share Improve this question edited May 23, 2017 at 12:24 CommunityBot 11 silver badge asked Jun 14, 2011 at 17:08 amyamy 2432 gold badges4 silver badges8 bronze badges 2
  • Can you just use an iframe? Seems like it might be easier. – cwallenpoole Commented Jun 14, 2011 at 17:14
  • Here's a decent answer that uses only Javascript: stackoverflow.com/a/3730577/1374474 – UserEleventyOne Commented May 17, 2012 at 7:06
Add a comment  | 

5 Answers 5

Reset to default 12

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;    
});

Inspired by Morlem's code, but working the other way around: stop the propagation only if scrolling out. In pure JS:

container.addEventListener('mousewheel', function(e) {
    var scrollingOverTheTop = (e.wheelDelta > 0 && this.scrollTop == 0);
    var scrollingOverTheBottom = (e.wheelDelta < 0 && (this.scrollTop >= this.scrollHeight - this.offsetHeight));
    if (scrollingOverTheBottom || scrollingOverTheTop) {
        e.preventDefault();
        e.stopPropagation();
    }
}, true);

Use a fixed layout. In this jsfiddle I have a working example for it. Study the css. You don't need javascript for it.

And if you do decide to use javascript (again, like KooiInc said, if you don't need js, don't use it), you can try using event.cancelBubble = true, which would prevent the event from propagating to the parent container so the page would not see your inner-div scrolling. Additional command you can use is event.preventDefault(), which prevents browser from triggering default behavior (i.e. scrolling) to the event.

I just solved my issue with this by creating the following in CSS:

body.scroll_locked {
    height: 100%;
    width: 100%;
    overflow: hidden;
}

Then, when I show my modal/lightbox, which is JavaScript (jQuery), I add the scroll_locked class to the body to lock it in place and remove the class to go back. Seems like you could do the same for a mouseenter/mouseleave event for a div that's always on the page and you want to have the same effect.

$('body').addClass('scroll_locked');
$('body').removeClass('scroll_locked');
发布评论

评论列表(0)

  1. 暂无评论