简体   繁体   English

当 div 到达页面顶部时,让 div 粘在另一个 div 下

[英]Make div stick under another div when div have reached top of page

I have a .stickyMenu div which I sits in the middle of the page.我有一个位于页面中间的.stickyMenu div。 When the .stickyMenu comes in contact with the bottom of .navB , I want it to stick there..stickyMenu.navB的底部接触时,我希望它粘在那里。 When a user scrolls back up, I want it to unstick in its original position.当用户向上滚动时,我希望它脱离原来的 position。

Thing is, I also want this to be dynamic because .gapA can be of any height (currently using an offset of 350 as an example).问题是,我也希望它是动态的,因为.gapA可以是任何高度(当前使用350的偏移量作为示例)。

So in the demo below, when the orange div scrolls to the bottom of the blue div, I want it to stick there when scrolling down, then when scrolling back up, where the grey div is present, I want it to unstick and return to its natural position.所以在下面的演示中,当橙色 div 滚动到蓝色 div 的底部时,我希望它在向下滚动时粘在那里,然后在向上滚动时,灰色 div 存在的地方,我希望它松开并返回其天然 position。

Current code and results:当前代码和结果:

 (function($) { $(document).ready(function() { $(window).scroll(function(e) { var $el = $('.stickyMenu'); var isPositionFixed = ($el.css('position') == 'fixed'); if ($(this).scrollTop() > 350 &&.isPositionFixed) { $el:css({ 'position', 'fixed': 'top', '90px': 'width', '100%': 'left', '0px': 'background', 'orange': 'z-index'; '1' }). } if ($(this).scrollTop() < 350 && isPositionFixed) { $el:css({ 'position', 'static': 'top'; '50px' }); } }); }); })(jQuery);
 body { margin: 0; }.wrap{ position: relative; display: block; }.wrap.navA { background-color: red; height: 50px; position: fixed; top: 0; left: 0; width: 100%; }.wrap.navB { background-color: blue; height: 90px; position: fixed; top: 50px; left:0; width: 100%; }.gapA{ height: 300px; background-color: grey; }.stickyMenu { height: 70px; background-color: orange; }.gapB{ height: 1500px; background-color: lightgrey; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="wrap"> <div class="navA"></div> <div class="navB"></div> </div> <div class="gapA"></div> <div class="stickyMenu"></div> <div class="gapB"></div>

Try This尝试这个

 (function($) { $(document).ready(function() { $(window).scroll(function(e) { var $el, nav, top, $el = $('.stickyMenu'); nav = $('.navB'); top = nav.height() + nav.position().top; if ($(this).scrollTop() >= top) { $el.css({ 'position': 'fixed', 'top': top, 'width': '100%', 'left': '0px', 'background': 'orange', 'z-index': '1' }); }else{ $el.css({ 'position': '', 'top': top, 'width': '100%', 'left': '0px', 'background': '', 'z-index': '' }); } }); }); })(jQuery);
 body { margin: 0; }.wrap{ position: relative; display: block; }.wrap.navA { background-color: red; height: 50px; position: fixed; top: 0; left: 0; width: 100%; }.wrap.navB { background-color: blue; height: 90px; position: fixed; top: 50px; left:0; width: 100%; }.gapA{ height: 300px; background-color: grey; }.stickyMenu { height: 70px; background-color: orange; }.gapB{ height: 1500px; background-color: lightgrey; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="wrap"> <div class="navA"></div> <div class="navB"></div> </div> <div class="gapA"></div> <div class="stickyMenu"></div> <div class="gapB"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM