jquery - 在调整屏幕大小之前,滚动条不会加载或调整大小

标签 jquery css html scrollbar

我正在为客户开发网站,需要一些帮助。出于某种原因,每个“页面”上的滚动条在调整窗口大小之前不会加载或更改高度。我有一个 jQuery 滚动条替换了普通滚动条,但事实并非如此,因为当我禁用它们时,普通滚动条会做同样的事情。内容存储在滚动 div 的子 div 中,但内容 div 的高度不会随内容更改而改变。将 $(window).resize(如下所示)中的代码转换为它自己的函数也不起作用。

http://brianandsacha.tumblr.com/

这不是有史以来最糟糕的事情,但非常烦人。

编辑:我添加了整个 JS 代码,以及 Max Dunn 建议的编辑。它仍然做同样的事情。

        //--details
        $(document).ready(function() {
            $("#info").show();
        });
        $(document).ready(function() {
            $('#elephant').click(function() {
                if ($('#info').css('display') == "none")
                    $('#info').fadeIn('slow'),
                    $('#stay').hide();
                    $('#todo').hide();
                    $('#nav').hide();
                    $('#gifts').hide();
                    $('#contact').hide();
                    $('#photos').hide();
                    $('#welcome').hide();
                    $("#bubblecontent").show();
                    resizeBubble();
            });
        });
        //--stay
        $(document).ready(function() {
            $("#stay").hide();
        });
        $(document).ready(function() {
            $('#gator').click(function() {
                if ($('#stay').css('display') == "none")
                    $('#stay').fadeIn('slow'),
                    $('#info').hide();
                    $('#todo').hide();
                    $('#nav').hide();
                    $('#gifts').hide();
                    $('#contact').hide();
                    $('#photos').hide();
                    $('#welcome').hide();
                    $("#bubblecontent").show();
                    resizeBubble();

            });
        });
        //--todo
        $(document).ready(function() {
            $("#todo").hide();
        });
        $(document).ready(function() {
            $('#man').click(function() {
                if ($('#todo').css('display') == "none")
                    $('#todo').fadeIn('slow'),
                    $('#info').hide();
                    $('#stay').hide();
                    $('#nav').hide();
                    $('#gifts').hide();
                    $('#contact').hide();
                    $('#photos').hide();
                    $('#welcome').hide();
                    $("#bubblecontent").show();
                    resizeBubble();
            });
        });
        //--nav
        $(document).ready(function() {
            $("#nav").hide();
        });
        $(document).ready(function() {
            $('#woman').click(function() {
                if ($('#nav').css('display') == "none")
                    $('#nav').fadeIn('slow'),
                    $('#info').hide();
                    $('#stay').hide();
                    $('#todo').hide();
                    $('#gifts').hide();
                    $('#contact').hide();
                    $('#photos').hide();
                    $('#welcome').hide();
                    $("#bubblecontent").show();
                    resizeBubble();
            });
        });
        //--gifts
        $(document).ready(function() {
            $("#gifts").hide();
        });
        $(document).ready(function() {
            $('#bird').click(function() {
                if ($('#gifts').css('display') == "none")
                    $('#gifts').fadeIn('slow'),
                    $('#info').hide();
                    $('#stay').hide();
                    $('#todo').hide();
                    $('#nav').hide();
                    $('#contact').hide();
                    $('#photos').hide();
                    $('#welcome').hide();
                    $("#bubblecontent").show();
                    resizeBubble();
            });
        });
        //--contact
        $(document).ready(function() {
            $("#contact").hide();
        });
        $(document).ready(function() {
            $('#dog').click(function() {
                if ($('#contact').css('display') == "none")
                    $('#contact').fadeIn('slow'),
                    $('#info').hide();
                    $('#stay').hide();
                    $('#todo').hide();
                    $('#nav').hide();
                    $('#gifts').hide();
                    $('#photos').hide();
                    $('#welcome').hide();
                    $("#bubblecontent").show();
                    resizeBubble();
            });
        });

        //--photos
        $(document).ready(function() {
            $("#photos").hide();
        });
        $(document).ready(function() {
            $('#cat').click(function() {
                if ($('#photos').css('display') == "none")
                    $('#photos').fadeIn('slow'),
                    $('#info').hide();
                    $('#stay').hide();
                    $('#todo').hide();
                    $('#nav').hide();
                    $('#gifts').hide();
                    $('#contact').hide();
                    $('#welcome').hide();
                    $("#bubblecontent").show();
                    resizeBubble();
            });
        });
        //welcome
        $(document).ready(function() {
            $("#welcome").show();
            $("#bubblecontent").hide();
            $('#header').click(function() {
                if ($('#welcome').css('display') == "none")
                    $('#welcome').fadeIn('slow'),
                    $('#stay').hide();
                    $('#todo').hide();
                    $('#nav').hide();
                    $('#gifts').hide();
                    $('#contact').hide();
                    $('#photos').hide();
                    $("#bubblecontent").hide();
                    resizeBubble();
            });
        });
        //bubbleheight
        function resizeBubble() {
            var divHeight = $(document).height() - 300;
            $('#bubble').css({height: divHeight});
        };
        $(window).resize(
        function() {
            resizeBubble();
        });

    body {
        overflow-y: hidden;
        overflow-x: hidden;
        font-family: "museo-slab", georgia, serif;
        font-weight: 100;
        height: 100%;
    }
    #container {
        margin-left: auto;
        margin-right: auto;
    }
    #bubble {
        background-color: #f5fec3;
        height: 100%;
        width: 900px;
        margin-left: auto;
        margin-right: auto;
        border-radius: 15px;
        -moz-border-radius: 15px;
        overflow-y: auto;
    }
    #bubblecontent {
        width: 860px;
        margin-left: auto;
        margin-right: auto;
        overflow-y: auto;
    }

<div id="container">
  <div id="bubble">
    <div id="bubblecontent">
      <div id="contentdiv1">
        blah
      </div>
      <div id="contentdiv2">
        blah
      </div>
    </div>
  </div>
</div>

最佳答案

我留下我的另一个答案,因为它可能会帮助其他遇到类似问题的人。然而,在查看了这个特定的 javascript 代码之后,有很多地方需要改进,所以我重写了代码。重要的变化是在内容更改时使用 mCustomScrollbar("update") 函数:

<script type="text/javascript">
  //loading
  $(window).load(function () {
    $('#loading').fadeOut('fast');
  });

  $(document).ready(function () {
    //elephant
    loadImage1 = new Image();
    loadImage1.src = "http://static.tumblr.com/spps9en/JN3m8jui6/elephanthover.png";
    staticImage1 = new Image();
    staticImage1.src = "http://static.tumblr.com/spps9en/j7Fm8jufs/elephant.png";
    //gator
    loadImage2 = new Image();
    loadImage2.src = "http://static.tumblr.com/spps9en/DoNm8jujc/alligatorhover.png";
    staticImage2 = new Image();
    staticImage2.src = "http://static.tumblr.com/spps9en/q9Em8jujy/alligator.png";
    //man
    loadImage3 = new Image();
    loadImage3.src = "http://static.tumblr.com/spps9en/AKZm8jvs5/manhover.png";
    staticImage3 = new Image();
    staticImage3.src = "http://static.tumblr.com/spps9en/xZam8jvbc/man.png";
    //woman
    loadImage4 = new Image();
    loadImage4.src = "http://static.tumblr.com/spps9en/AH6m8jvvq/womanhover.png";
    staticImage4 = new Image();
    staticImage4.src = "http://static.tumblr.com/spps9en/seym8jvup/woman.png";
    //bird
    loadImage5 = new Image();
    loadImage5.src = "http://static.tumblr.com/spps9en/HY8m8phuj/birdhover.png";
    staticImage5 = new Image();
    staticImage5.src = "http://static.tumblr.com/spps9en/0jkm8jvz1/bird.png";
    //dog
    loadImage6 = new Image();
    loadImage6.src = "http://static.tumblr.com/spps9en/sxcm8gfjf/doghover.png";
    staticImage6 = new Image();
    staticImage6.src = "http://static.tumblr.com/spps9en/2qKm8jw17/dog.png";
    //cat
    loadImage7 = new Image();
    loadImage7.src = "http://static.tumblr.com/spps9en/ex8mc60c7/cathover.png";
    staticImage7 = new Image();
    staticImage7.src = "http://static.tumblr.com/spps9en/MKomc6065/cat.png";

    function resizeBubble() {
      var divHeight = $(document).height() - 300;
      $('#bubble').css({height: divHeight});
    };

    $(window).resize(function () {
      resizeBubble();
    });

    function updateScroll() {
      $("#bubble").mCustomScrollbar("update");
    }

    function hideAll() {
      $("#welcome").hide();
      $("#info").hide();
      $("#stay").hide();
      $("#todo").hide();
      $("#nav").hide();
      $("#gifts").hide();
      $("#contact").hide();
      $("#photos").hide();
    }

    function switchSection(sectionId) {
      if ($(sectionId).css('display') == "none") {
        hideAll();
        $(sectionId).fadeIn('slow');
        updateScroll();
      }
    }

    $('#elephant').click(function () {
      switchSection('#info')
    });
    $('#gator').click(function () {
      switchSection('#stay')
    });
    $('#man').click(function () {
      switchSection('#todo')
    });
    $('#woman').click(function () {
      switchSection('#nav')
    });
    $('#bird').click(function () {
      switchSection('#gifts')
    });
    $('#dog').click(function () {
      switchSection('#contact')
    });
    $('#cat').click(function () {
      switchSection('#photos')
    });
    $('#header').click(function () {
      switchSection('#welcome')
    });

    // Initial state
    hideAll();
    resizeBubble();
    $("#bubble").mCustomScrollbar({set_width:false});
    switchSection('#welcome');
  });
</script>

关于jquery - 在调整屏幕大小之前,滚动条不会加载或调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12984709/

相关文章:

javascript - 更改 jQuery 中链接数组的文本

jquery - 如果长度超过 10 个字符,则收缩文本

html - 如何在移动和 PC 浏览器上为 iframe 视频提供不同的宽度和高度?

javascript - 使用 jquery 搜索,无需提交按钮

javascript - 通过更改边距构建 jquery 幻灯片

javascript - 如何排除某些链接上的 JQuery/Javascript 淡入淡出功能?

jquery - 在 Selenium 中执行 jQuery

php - Bootstrap 多级下拉不起作用

css - jQuery Mobile 保持下一页的样式

java - 如何阅读下载文本中的中断?