Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a fixed position div to scroll horizontally with the content? Using jQuery

Tags:

jquery

css

fixed

I have a div.scroll_fixed with the following CSS

.scroll_fixed {     position:absolute     top:210px  } .scroll_fixed.fixed {     position:fixed;     top:0; }  

I'm using the following jQuery code to set the .fixed class when the div reaches the top of the page.

var top = $('.scroll_fixed').offset().top - parseFloat($('.scroll_fixed').css('margin-top').replace(/auto/, 0));  $(window).scroll(function (event) {     // what the y position of the scroll is     var y = $(this).scrollTop();      // whether that's below the form     if (y >= top) {         // if so, ad the fixed class         $('.scroll_fixed').addClass('fixed');     } else {         // otherwise remove it         $('.scroll_fixed').removeClass('fixed');     } }); 

This works great for the vertical scroll fixing. But with a small browser window, horizontal scrolling causes a clash with content to the right of this fixed div.

I would like the div to scroll with the content horizontally.

Could anyone point me in the right direction. Still getting my feet wet with JS/JQuery.

I basically want it to work like the second box in this example.

like image 587
jfeust Avatar asked Jan 13 '11 02:01

jfeust


People also ask

How do I horizontally scroll a div using jQuery?

In jQuery, we can make any element tag move horizontally using the built-in function scrollLeft() function for scrolling the scrollbar horizontally according to either the position which is set as the parameter or without setting the position which would specify the position in pixel number of the scrollbar.

How can get horizontal scroll position in jQuery?

The scrollLeft() method sets or returns the horizontal scrollbar position for the selected elements. Tip: When the scrollbar is on the far left side, the position is 0. When used to return the position: This method returns the horizontal position of the scrollbar for the FIRST matched element.

How do you make a div scroll horizontally?

Set the overflow-y: hidden; and overflow-x: auto; that will automatically hide the vertical scroll bar and present only the horizontal scrollbar. The white-space: nowrap; property is used to wrap text in a single line. Here the scroll div will be horizontally scrollable.

How do I add a scroll horizontally?

To enable horizontal scrolling, we can use the CSS property overflow-x. If we assign the value scroll to the overflow-x property of the container element, the browser will hide horizontally overflowing content and make it accessible via horizontal scrolling.


1 Answers

The demo is keeping the element's position:fixed and manipulating the left property of the element:

var leftInit = $(".scroll_fixed").offset().left; var top = $('.scroll_fixed').offset().top - parseFloat($('.scroll_fixed').css('margin-top').replace(/auto/, 0));   $(window).scroll(function(event) {     var x = 0 - $(this).scrollLeft();     var y = $(this).scrollTop();      // whether that's below the form     if (y >= top) {         // if so, ad the fixed class         $('.scroll_fixed').addClass('fixed');     } else {         // otherwise remove it         $('.scroll_fixed').removeClass('fixed');     }      $(".scroll_fixed").offset({         left: x + leftInit     });  }); 

Using leftInit takes a possible left margin into account. Try it out here: http://jsfiddle.net/F7Bme/

like image 128
Andrew Whitaker Avatar answered Oct 16 '22 12:10

Andrew Whitaker