简体   繁体   English

html文档没有足够的高度时,菜单固定在滚动条顶部的问题?

[英]Problems with menu fixed top on scroll, when html document not enough height?

I do a menu fixed top when scroll, it ok with some pages have large height, but in page has not enough height, this script loop: 滚动时我在顶部固定了一个菜单,可以确定某些页面的高度很大,但是页面的高度不够,此脚本循环如下:

Example: 例:

I have menu with height 50px and i write a script: 我有高度为50px的菜单,并编写了一个脚本:

if ($(this).scrollTop() > 50){
    // add class fixed
} else { //remove class }
  • on pages with large height this run: scroll(over 50px) > add class 在运行高度较大的页面上: scroll(over 50px) > add class
  • on pages with small height this run: scroll(over 50px) > add class > remove class 在运行高度较小的页面上: scroll(over 50px) > add class > remove class

Please see this example: http://jsfiddle.net/F4BmP/2930/ 请参见以下示例: http : //jsfiddle.net/F4BmP/2930/

Use this javascript: 使用此javascript:

$(document).ready(function(){
var elementPosition = $('.menu').offset();

$(window).scroll(function(){
        if($(window).scrollTop() > elementPosition.top){
              $('.menu').css('position','fixed').css('top','0');
              $('.menu').css('width','100%');
              $('.menu').css('z-index','500');
        } else {
            $('.menu').css('position','static');
        }    
});
});

Well, this code is working on my menubar, even if screen size is different . 好的,即使屏幕大小不同,此代码也可以在我的菜单栏上运行。

Basic concept is user has to see the menu while scrolling the page. 基本概念是用户在滚动页面时必须查看菜单。

But in your functionality is correct.There is no much content and User can see the menu in current screen itself. 但是您的功能是正确的,没有太多内容,用户可以在当前屏幕本身中看到菜单。 If there is more content user can scroll and get sticky menu always on top. 如果有更多内容,用户可以滚动并始终在顶部获得即时贴菜单。

If you really want to make browser scroll you can give min-height for container. 如果您真的想使浏览器滚动,则可以指定容器的min-height

Example

.containerclassname{
  min-height: 1500px;
}

I tested your code in firefox and also in chrome , the issue seems to be in chrome . 我在Firefox和chrome中测试了您的代码,问题似乎出在chrome中。 i reworked the code both html and JS and it works fine in chrome, for for that matter in any browser . 我修改了html和JS的代码,因此在任何浏览器中都可以在chrome中正常工作。

heres what should probably work for you : 这可能应该为您工作:

 $(document).ready(function(){
             $(window).scroll(function (e) {
                    $el = $('.nav');
                    if ($(this).scrollTop() > 100) {
                        $('.nav').addClass("fixedNav");
                    }else {
                        $('.nav').removeClass("fixedNav");
                    }
                });
             });

the entire code is enclosed in a fiddle . 整个代码放在小提琴中。

Link to fiddle 链接到小提琴

Kind regards . 亲切的问候 。

Alexander . 亚历山大。

Finally, i find a solution for my problem. 最后,我为我的问题找到了解决方案。

Reason make problem is HTML document lost height when menu change from static to fixed.Examble: Browser has 500px and has a scrollbar, when user scroll my menu change to fixed and browser lost 50px of menu, so browser not enough height to has scrollbar, it will return to top page and do code lines in ELSE statement. 原因是菜单从静态更改为固定时HTML文档丢失了高度。解说:浏览器具有500px并具有滚动条,当用户将我的菜单更改为固定并浏览器丢失了50px的菜单时,浏览器的高度不足以具有滚动条,它将返回首页并在ELSE语句中执行代码行。

So i add a div wrap my menu and set height the same height with my menu, this will make the height of document always the same on before and after scroll: 因此,我添加了一个div包装菜单,并将高度设置为与菜单相同的高度,这将使滚动前后文档的高度始终相同:

<div id="wrap" style="height:50px;width:100%">
      <div id="mymenu"></div>
</div>

This solution solve my problem. 此解决方案解决了我的问题。

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

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