admin管理员组文章数量:1334935
I need to fix 'middle_block2' to either the bottom of the fixed position 'top_block' or 85px (the height of 'top_block') from the top of the window when 'middle_block2' reaches the bottom of 'top_block' when scrolling down. Here is the code and a link to the jsfiddle.
I have it working when it reaches that top of the window but that is as far as I have been able to get.
jsfiddle: /
HTML
<div class='top_block'>
<p>Top Block</p>
</div>
<div class='middle_block1'>
<p>Middle Block 1</p>
</div>
<div class ='ghost_div'>
<div class='middle_block2'>
<p>Middle Block 2</p>
</div>
</div>
<div class='bottom_block'>
<p>Bottom Block</p>
<p>Bottom Block</p>
<p>Bottom Block</p>
<p>Bottom Block</p>
<p>Bottom Block</p>
<p>Bottom Block</p>
<p>Bottom Block</p>
<p>Bottom Block</p>
<p>Bottom Block</p>
<p>Bottom Block</p>
<p>Bottom Block</p>
</div>
CSS
* {
margin:0;
padding:0;
}
.top_block {
width:100%;
background-color: black;
height: 85px;
color:white;
padding: 0px;
margin: 0px;
position:fixed;
}
.middle_block1 {
width:100%;
background-color: yellow;
height: 450px;
color:black;
padding-top: 85px;
z-index:2;
}
.ghost_div {
height: 85px;
background-color: red;
}
.middle_block2 {
width:100%;
background-color: blue;
height: 85px;
color:white;
}
.bottom_block {
width:100%;
background-color: red;
height: 950px;
color:white;
}
JQUERY
$(function(){
// Check the initial Position of the fixed_nav_container
var stickyHeaderTop = $('.middle_block2').offset().top;
$(window).scroll(function(){
if( $(window).scrollTop() > stickyHeaderTop ) {
$('.middle_block2').css({position: 'fixed', top: '85px'});
} else {
$('.middle_block2').css({position: 'relative', top: '0px'});
}
});
});
I need to fix 'middle_block2' to either the bottom of the fixed position 'top_block' or 85px (the height of 'top_block') from the top of the window when 'middle_block2' reaches the bottom of 'top_block' when scrolling down. Here is the code and a link to the jsfiddle.
I have it working when it reaches that top of the window but that is as far as I have been able to get.
jsfiddle: https://jsfiddle/jpolsonb/u9adhkc7/1/
HTML
<div class='top_block'>
<p>Top Block</p>
</div>
<div class='middle_block1'>
<p>Middle Block 1</p>
</div>
<div class ='ghost_div'>
<div class='middle_block2'>
<p>Middle Block 2</p>
</div>
</div>
<div class='bottom_block'>
<p>Bottom Block</p>
<p>Bottom Block</p>
<p>Bottom Block</p>
<p>Bottom Block</p>
<p>Bottom Block</p>
<p>Bottom Block</p>
<p>Bottom Block</p>
<p>Bottom Block</p>
<p>Bottom Block</p>
<p>Bottom Block</p>
<p>Bottom Block</p>
</div>
CSS
* {
margin:0;
padding:0;
}
.top_block {
width:100%;
background-color: black;
height: 85px;
color:white;
padding: 0px;
margin: 0px;
position:fixed;
}
.middle_block1 {
width:100%;
background-color: yellow;
height: 450px;
color:black;
padding-top: 85px;
z-index:2;
}
.ghost_div {
height: 85px;
background-color: red;
}
.middle_block2 {
width:100%;
background-color: blue;
height: 85px;
color:white;
}
.bottom_block {
width:100%;
background-color: red;
height: 950px;
color:white;
}
JQUERY
$(function(){
// Check the initial Position of the fixed_nav_container
var stickyHeaderTop = $('.middle_block2').offset().top;
$(window).scroll(function(){
if( $(window).scrollTop() > stickyHeaderTop ) {
$('.middle_block2').css({position: 'fixed', top: '85px'});
} else {
$('.middle_block2').css({position: 'relative', top: '0px'});
}
});
});
Share
Improve this question
edited Nov 14, 2022 at 22:35
Dharman♦
33.4k27 gold badges101 silver badges147 bronze badges
asked Mar 29, 2016 at 5:41
JPBJPB
6002 gold badges6 silver badges31 bronze badges
4
- you can put top as 85px at else condition how ever it is not entering to if condition >> $('.middle_block2').css({position: 'relative', top: '85px'}); – Iqbal Pasha Commented Mar 29, 2016 at 6:03
- Thanks but I already tried that, it just moves middle_block2 85px down into bottom_block? – JPB Commented Mar 29, 2016 at 6:08
- yes and it will change position to relative – Iqbal Pasha Commented Mar 29, 2016 at 6:10
- That's not the problem though, it needs stay where it is until the top of middle block reaches the bottom of top block, it then bees fixed. It can't overlap the bottom block. – JPB Commented Mar 29, 2016 at 6:12
3 Answers
Reset to default 5Please modify the code like this and check
$(function(){
// Check the initial Position of the fixed_nav_container
var stickyHeaderTop = $('.middle_block2').offset().top;
$(window).scroll(function(){
if( $(document).scrollTop() > stickyHeaderTop-85 ) {
$('.middle_block2').css({position: 'fixed', top: '85px'});
} else {
$('.middle_block2').css({position: 'relative', top: '0px'});
}
});
});
Updated Fiddle : https://jsfiddle/u9adhkc7/4/
Try below mentioned code:
$(function(){
var topBlockheight=$('.top_block').height();
// Check the initial Position of the fixed_nav_container
var stickyHeaderTop = $('.middle_block2').offset().top;
$(window).scroll(function(){
if( $(window).scrollTop() > stickyHeaderTop-topBlockheight ) {
$('.middle_block2').css({position: 'fixed', top: '85px'});
} else {
$('.middle_block2').css({position: 'relative', top: '0px'});
}
});
});
Updated Fiddle : https://jsfiddle/u9adhkc7/6/
Try
.middle_block2 {
width:100%;
background-color: blue;
height: 85px;
color:white;
postion: fixed;
}
本文标签: javascriptFix div when position reaches 85px from top of window on scrollStack Overflow
版权声明:本文标题:javascript - Fix div when position reaches 85px from top of window on scroll - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742364299a2460972.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论