简体   繁体   English

顶部菜单的 position 已修复但在下一个 div 的滚动部分消失

[英]top menu's position fixed but on scroll part from next div disappears

The div class="first" is a top menu that I wanna try to make it fix on scroll so I used this code, everything is working fine but the problem is that when I start scrolling half of the next image mid-top.jpeg in the next div class="second" disappear. div class="first" 是一个顶部菜单,我想尝试将其固定在滚动条上,所以我使用了这段代码,一切正常,但问题是当我开始滚动下一张图片的一半时 mid-top.jpeg在下一个 div class="second" 中消失。

it is like image mid-top.jpeg comes under the image top.jpeg就像图像 mid-top.jpeg 位于图像 top.jpeg 之下

How can I fix this?我怎样才能解决这个问题? thanks!谢谢!

 var fixmeTop = $('.first').offset().top; $(window).scroll(function() { var currentScroll = $(window).scrollTop(); if (currentScroll >= fixmeTop) { $('.first').css({ position: 'fixed', top: '0', }); } else { $('.first').css({ position: 'static' }); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="first"> <img src="images/top.jpeg" alt=""> <map name=""> <area target="" alt="" title="home" href="index.html" coords="1,60,95,115" shape="rect"> </map> </div> <div class="second"> <img src="images/mid-top.jpeg" alt=""> <!--problem here--> <map name=""> <area shape="" coords="" href="" alt=""> </map> </div>

You can solve this with CSS only.您只能使用 CSS 解决此问题。

Set .first to position: fixed将 .first 设置为.first position: fixed

Vertically offset .second by the height of .first using margin-top使用margin-top.second垂直偏移.first的高度

 .first { height: 150px; position: fixed; top: 0; }.second { margin-top: 150px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="first"> <img src="https://via.placeholder.com/350x150"> <map name=""> <area target="" alt="" title="home" href="index.html" coords="1,60,95,115" shape="rect"> </map> </div> <div class="second"> <img src="https://via.placeholder.com/350x150"> <map name=""> <area shape="" coords="" href="" alt=""> </map> </div>

Is img top.jpeg suppose to be part of the fixed menu? img top.jpeg 应该是固定菜单的一部分吗? If not, it should be moved out of the.first div.如果不是,则应将其移出 the.first div。 That could explain why your second image is moving under the first.这可以解释为什么您的第二张图片在第一张图片下方移动。

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

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