Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow one column to scroll till end of content, and then remain fixed

Tags:

html

css

I have two columns in my html content. The first column is a sidebar and is expected to be limited in content, while the second column is the actual content which can be several pages length.

What I'm trying to achieve is this: As I scroll the page, I want the first column to scroll till the point that the end of the first column content is visible, at which point the it should not scroll further as I scroll down. The second column should scroll all the way through, as the first column fixes at the point above. Note: When they do scroll, I need both columns to scroll together and not independently.

I tried using position:fixed with overflow-y: auto, but that doesn't seem to work. Please refer the jsFiddle here: http://jsfiddle.net/a1qn17gw/1/

.fixed-content {
  position: fixed;
  overflow-y: auto;
}

To illustrate, I'm trying to achieve the below as I scroll down the page:

enter image description here

like image 865
Gaurav Jain Avatar asked Oct 18 '22 07:10

Gaurav Jain


1 Answers

I read your requirements and came up with the below code, I changed the positioning and added some jquery for watching the window scroll event.

$(window).bind('scroll', function () {
		var windowScrollHeight = $(window).scrollTop()
		var scrollPlusWindowHeight = $(window).scrollTop() + $(window).height();
    var fixedContentHeight = $(".fixed-content").height();
    if(scrollPlusWindowHeight > fixedContentHeight){
     	$(".fixed-content").addClass("fixed");
     }else{
     	$(".fixed-content").removeClass("fixed");
     }
});
.fixed-content {
  position: absolute;
  width: 250px;
  color: red;
}
.fixed {
    position:fixed;
    bottom:0;
}
body{
  margin:0px;
  height:100%;
  width:100%;
}

.scrollable-content {
  position: absolute;
  margin-left: 300px;
  color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="clearfix">
<div class="fixed-content">
Afghanistan<br>
Albania<br>
Algeria<br>
Andorra<br>
Angola<br>
Antigua & Deps<br>
Argentina<br>
Armenia<br>
Australia<br>
Austria<br>
Azerbaijan<br>
Bahamas<br>
Bahrain<br>
Bangladesh<br>
Afghanistan<br>
Albania<br>
Algeria<br>br>
Armenia<br>
Australia<br>
Austria<br>
Azerbaijan<br>
Bahamas<br>
Bahrain<br>
Bangladesh<br>
Afghanistan<br>
Albania<br>
Algeria<br>
Andorra<br>
Angola<br>
Antigua & Deps<br>
Argentina<br>
Armenia<br>
Australia<br>
Austria<br>
Azerbaijan<br>
Bahamas<br>
Bahrain<br>
Bangladesh<br>
Barbados<br>
Belarus<br>
Belgium<br>
Belize<br>
Benin<br>
Bhutan<br>
Bolivia<br>
Bosnia Herzegovina<br>
Botswana<br>
Brazil<br>
Brunei<br>

</div>
<div class="scrollable-content">
Afghanistan<br>
Albania<br>
Algeria<br>
Andorra<br>
Angola<br>
Antigua & Deps<br>
Argentina<br>
Armenia<br>
Australia<br>
Austria<br>
Azerbaijan<br>
Bahamas<br>
Bahrain<br>
Bangladesh<br>
Barbados<br>
Belarus<br>
Belgium<br>
Belize<br>
Benin<br>
Bhutan<br>
Bolivia<br>
Bosnia Herzegovina<br>
Botswana<br>
Brazil<br>
Brunei<br>
Bulgaria<br>
Burkina<br>
Burundi<br>
Cambodia<br>
Cameroon<br>
Canada<br>
Cape Verde<br>
Central African Rep<br>
Chad<br>
Chile<br>
China<br>
Colombia<br>
Comoros<br>
Congo<br>
Congo {Democratic Rep}<br>
Costa Rica<br>
Croatia<br>
Cuba<br>
Cyprus<br>
Czech Republic<br>
Denmark<br>
Djibouti<br>
Dominica<br>
Dominican Republic<br>
East Timor<br>
Ecuador<br>
Egypt<br>
El Salvador<br>
Equatorial Guinea<br>
Eritrea<br>
Estonia<br>
Ethiopia<br>
Fiji<br>
Finland<br>
France<br>
Gabon<br>
Gambia<br>
Georgia<br>
Germany<br>
Ghana<br>
Greece<br>
Grenada<br>
Guatemala<br>
Guinea<br>
Guinea-Bissau<br>
Guyana<br>
Haiti<br>
Honduras<br>
Hungary<br>
Iceland<br>
India<br>
Indonesia<br>
Iran<br>
Iraq<br>
Ireland {Republic}<br>
Israel<br>
Italy<br>
Ivory Coast<br>
Jamaica<br>
Japan<br>
Jordan<br>
Kazakhstan<br>
Kenya<br>
Kiribati<br>
Korea North<br>
Korea South<br>
Kosovo<br>
Kuwait<br>
Kyrgyzstan<br>
Laos<br>
Latvia<br>
Lebanon<br>
Lesotho<br>
Liberia<br>
Libya<br>
Liechtenstein<br>
Lithuania<br>
Luxembourg<br>
Macedonia<br>
Madagascar<br>
Malawi<br>
Malaysia<br>
Maldives<br>
Mali<br>
Malta<br>
Marshall Islands<br>
Mauritania<br>
Mauritius<br>
Mexico<br>
Micronesia<br>
Moldova<br>
Monaco<br>
Mongolia<br>
Montenegro<br>
Morocco<br>
Mozambique<br>
Myanmar, {Burma}<br>
Namibia<br>
Nauru<br>
Nepal<br>
Netherlands<br>
New Zealand<br>
Nicaragua<br>
Niger<br>
Nigeria<br>
Norway<br>
Oman<br>
Pakistan<br>
Palau<br>
Panama<br>
Papua New Guinea<br>
Paraguay<br>
Peru<br>
Philippines<br>
Poland<br>
Portugal<br>
Qatar<br>
Romania<br>
Russian Federation<br>
Rwanda<br>
St Kitts & Nevis<br>
St Lucia<br>
Saint Vincent & the Grenadines<br>
Samoa<br>
San Marino<br>
Sao Tome & Principe<br>
Saudi Arabia<br>
Senegal<br>
Serbia<br>
Seychelles<br>
Sierra Leone<br>
Singapore<br>
Slovakia<br>
Slovenia<br>
Solomon Islands<br>
Somalia<br>
South Africa<br>
South Sudan<br>
Spain<br>
Sri Lanka<br>
Sudan<br>
Suriname<br>
Swaziland<br>
Sweden<br>
Switzerland<br>
Syria<br>
Taiwan<br>
Tajikistan<br>
Tanzania<br>
Thailand<br>
Togo<br>
Tonga<br>
Trinidad & Tobago<br>
Tunisia<br>
Turkey<br>
Turkmenistan<br>
Tuvalu<br>
Uganda<br>
Ukraine<br>
United Arab Emirates<br>
United Kingdom<br>
United States<br>
Uruguay<br>
Uzbekistan<br>
Vanuatu<br>
Vatican City<br>
Venezuela<br>
Vietnam<br>
Yemen<br>
Zambia<br>
Zimbabwe<br>
</div>
</div>
like image 78
Naren Murali Avatar answered Nov 11 '22 09:11

Naren Murali