0

I'm not sure if this is a browser issue but in Webkit browsers, I have a div with the following CSS:

.fixed_box {
    position: fixed;
    top: 100px;
    height: 200px;
    width: 200px;
    overflow-y: scroll;
}

When you scroll inside that div and you hit the top of that div, the rest of the page starts to scroll as well if there are overflowing elements, but I haven't scrolled there.

I need to set it so that only .fixed_box will scroll when my mouse is inside and scrolling.

Thanks

6
  • can u make a fiddle for this? Commented Jun 19, 2014 at 3:43
  • I'm behind a firewall and for some reason fiddle is blocked Commented Jun 19, 2014 at 3:46
  • ok fine post your html code. Commented Jun 19, 2014 at 3:51
  • i have generated one jsfiddle.net/bLXux/show , no issue at all... Commented Jun 19, 2014 at 3:51
  • 1
    That is the normal (and expected) behavior. How else would users access the content that is not in the .fixed-box div that was outside the viewport? Commented Jun 19, 2014 at 6:51

2 Answers 2

1

You can use mousewheel and DOMMouseScroll function to achieve this. Add the following jquery in your code.

$('.fixed_box').on('mousewheel DOMMouseScroll', function(e) {
var scrollTo = null;

if (e.type == 'mousewheel') {
    scrollTo = (e.originalEvent.wheelDelta * -1);
}
else if (e.type == 'DOMMouseScroll') {
    scrollTo = 40 * e.originalEvent.detail;
}

if (scrollTo) {
    e.preventDefault();
    $(this).scrollTop(scrollTo + $(this).scrollTop());
}
});

Explanation:

Here I have used two type of function because in jQuery 1.7+, the detail property is not available at the jQuery Event object. So, we need to use event.originalEvent.detail to for this property at the DOMMouseScroll event. This method is backwards-compatible with older jQuery versions.

Coming to inside part, It is actually getting the values of mousewheel height, it means when you use your mouse up or down how much height it should go up and down of particular div. I am using preventDafault function which will execute all the time so that the default action of this event will not be triggered. So that i am manually moving my scroll up and down using the bottom line calculation.

The if condition is checking whether we are getting value or not using mousewheel function. If we are not getting values, it will be a "null" value. So that bottom preventDefault and positioning won't work.

Have a Fiddle!!

Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot for the answer. Are you able to briefly explain what this code does? Thank You :)
I have added my explanation in the answer. By the way you should not ask like "are you able to briefly explain". It looks like you are thinking i am a fool.
Thanks a lot. But I was trying to be polite, I mean if I just asked you directly it might sound really demanding for some people
0

As has been said in the comments, how will users access the content without scrolling? Here is the answer though :)

overflow: hidden; on the body.

Have a fiddle

CSS

body { margin:0; padding:0; color:#fff; overflow: hidden; }

#wrapper { background:#000; }
.fixed_box {
    position: fixed;
    top: 100px;
    height: 200px;
    width: 200px;
    overflow-y: scroll;
    background:#f00;
}

4 Comments

in my experience overflow: hidden; just hides the mess rather than cleaning it! :)
In this case it's hiding the body, which is already hidden by the viewport :)
It is a weird request though!
This doesn't really solve my problem because I need both areas to be scrollable, just when one scrolls, the other one doesn't. I think the javascript answer below might be more useful for me. Thanks for the help guys :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.