Created
September 26, 2018 19:26
-
-
Save mujahidi/792cb81a3c6f40a341998a5d2e5df1d4 to your computer and use it in GitHub Desktop.
Sticky Sidebar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<script src="https://code.jquery.com/jquery-3.1.0.js"></script> | |
<title>JS Bin</title> | |
<style id="jsbin-css"> | |
.head, .foot{ | |
background: #ccc; | |
width: 100%; | |
height: 200px; | |
} | |
.contain{ position: relative; } | |
.content{ | |
height: 1000px; | |
background: red; | |
float: left; | |
width: 70%; | |
} | |
.sidebar{ | |
float: right; | |
background: green; | |
width: 25%; | |
height: 200px; | |
} | |
.sticky{ | |
position: fixed; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="head"></div> | |
<div class="contain"> | |
<div class="content"> | |
this is content area | |
</div> | |
<div class="sidebar"> | |
this is sidebar area | |
</div> | |
</div> | |
<div style="clear: both;"></div> | |
<div class="foot"></div> | |
<script id="jsbin-javascript"> | |
jQuery(document).ready(function($){ | |
var sidebar = $('.sidebar'); | |
var sidebarPos = $('.sidebar').offset(); | |
var sidebarHeight = sidebar.height(); | |
var contentPos = $('.content').offset(); | |
var footPos = $('.foot').offset(); | |
// console.log(pos); | |
var $w = $(window).scroll(function(){ | |
var pos3 = $('.sidebar').offset(); | |
if ( $w.scrollTop() > contentPos.top ) { | |
sidebar.addClass('sticky').css('top', '0px').css('left', sidebarPos.left); | |
} else { | |
sidebar.removeClass('sticky'); | |
} | |
if ( $w.scrollTop() > footPos.top - sidebarHeight ) { | |
sidebar.css('background', 'green'); | |
sidebar.removeClass('sticky'); | |
} else { | |
} | |
}); | |
}); | |
</script> | |
<script id="jsbin-source-css" type="text/css">.head, .foot{ | |
background: #ccc; | |
width: 100%; | |
height: 200px; | |
} | |
.contain{ position: relative; } | |
.content{ | |
height: 1000px; | |
background: red; | |
float: left; | |
width: 70%; | |
} | |
.sidebar{ | |
float: right; | |
background: green; | |
width: 25%; | |
height: 200px; | |
} | |
.sticky{ | |
position: fixed; | |
}</script> | |
<script id="jsbin-source-javascript" type="text/javascript">jQuery(document).ready(function($){ | |
var sidebar = $('.sidebar'); | |
var sidebarPos = $('.sidebar').offset(); | |
var sidebarHeight = sidebar.height(); | |
var contentPos = $('.content').offset(); | |
var footPos = $('.foot').offset(); | |
// console.log(pos); | |
var $w = $(window).scroll(function(){ | |
var pos3 = $('.sidebar').offset(); | |
if ( $w.scrollTop() > contentPos.top ) { | |
sidebar.addClass('sticky').css('top', '0px').css('left', sidebarPos.left); | |
} else { | |
sidebar.removeClass('sticky'); | |
} | |
if ( $w.scrollTop() > footPos.top - sidebarHeight ) { | |
sidebar.css('background', 'green'); | |
sidebar.removeClass('sticky'); | |
} else { | |
} | |
}); | |
});</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.head, .foot{ | |
background: #ccc; | |
width: 100%; | |
height: 200px; | |
} | |
.contain{ position: relative; } | |
.content{ | |
height: 1000px; | |
background: red; | |
float: left; | |
width: 70%; | |
} | |
.sidebar{ | |
float: right; | |
background: green; | |
width: 25%; | |
height: 200px; | |
} | |
.sticky{ | |
position: fixed; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function($){ | |
var sidebar = $('.sidebar'); | |
var sidebarPos = $('.sidebar').offset(); | |
var sidebarHeight = sidebar.height(); | |
var contentPos = $('.content').offset(); | |
var footPos = $('.foot').offset(); | |
// console.log(pos); | |
var $w = $(window).scroll(function(){ | |
var pos3 = $('.sidebar').offset(); | |
if ( $w.scrollTop() > contentPos.top ) { | |
sidebar.addClass('sticky').css('top', '0px').css('left', sidebarPos.left); | |
} else { | |
sidebar.removeClass('sticky'); | |
} | |
if ( $w.scrollTop() > footPos.top - sidebarHeight ) { | |
sidebar.css('background', 'green'); | |
sidebar.removeClass('sticky'); | |
} else { | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment