I didn’t find many ready examples of how to do this, so I decided to do it myself. See if this is what you want:
I believed a div
with the class="sideBar"
which may have several articles, the "related", or anything else. I represented this by several div.artigos
.
Stayed like this:
<div class="sideBar" >
<div class="artigos"></div>
<div class="artigos"></div>
<div class="artigos"></div>
<div class="artigos"></div>
<div class="artigos"></div>
</div>
<div class="conteudo" >
</div>
Their respective styles:
.sideBar{
width: 180px;
height: auto;
float: right;
}
.conteudo{
width: auto;
height: 2000px;
background: #333;
margin-right: 200px;
}
.sideBar .artigos{
width: 100%;
height: 200px;
background: #f30;
margin-bottom: 5px;
}
I already made the code in Jquery:
$(document).scroll(function(){
var left = $('.sideBar').offset().left;
if($(this).scrollTop() >= $('.sideBar').height() - $('.artigos:last').height()){
$('.sideBar').css({
position: 'fixed',
top: -($('.sideBar').height() - $('.artigos:last').height()),
left: left
})
}else{
$('.sideBar').css({
position: 'relative',
top: 0,
left: '0'
})
}
})
I used the event .scroll()
jquery. When scrolling the page the first thing that will be done will be the capture of the left
of .sideBar
. After that he will
verify whether the .scrollTop
document is larger than the size of the .sideBar
subtracted with the size of the last .artigos
, and if it is the div.sideBar
will receive a position: fixed
, how this behaves taking into account the whole page, will have a left
corresponding to the previous catch. top
will be equal to the size of the .sideBar
subtracted with the size of the last .artigos
, but negative (same calculation as the verification of the scroll
).
But should the scroll
shall be less than the result of the calculation, div.sideBar
this will receive a position: relative
, and a reset on left
and top
.
Complete Code
Note that when the div.conteudo
ends the div.sideBar
stays fixed, but if it goes back up, it goes back to running along with the rest.
$(document).scroll(function(){
var left = $('.sideBar').offset().left;
if($(this).scrollTop() >= $('.sideBar').height() - $('.artigos:last').height()){
$('.sideBar').css({
position: 'fixed',
top: -($('.sideBar').height() - $('.artigos:last').height()),
left: left
})
}else{
$('.sideBar').css({
position: 'relative',
top: 0,
left: '0'
})
}
})
.sideBar{
width: 180px;
height: auto;
float: right;
}
.conteudo{
height: 2000px;
background: #333;
margin-right: 200px;
}
.sideBar .artigos{
width: 100%;
height: 200px;
background: #f30;
margin:0 0 5px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sideBar" >
<div class="artigos"></div>
<div class="artigos"></div>
<div class="artigos"></div>
<div class="artigos"></div>
<div class="artigos"></div>
</div>
<div class="conteudo" >
</div>
If you have difficulties to implement in your code, let me know.
I don’t know if I got it right, but in the css class you want to leave fixed you put the property { position: Fixed; } and adjust the element’s positioning properties.
– Marcelo Bonus