简体   繁体   English

Javascript / jQuery元素在重新加载页面之前不起作用

[英]Javascript/jQuery elements not working until page reload

I've got a strange problem where the javascript/jQuery on my homepage is only working after I reload the page. 我遇到了一个奇怪的问题,即我的网页上的javascript / jQuery仅在重新加载页面后才能工作。 Both the floating menu and smooth scroll fail to work as expected. 浮动菜单和平滑滚动均无法按预期工作。 The problem can be replicated by forcing the browser to get a new version of the page (ie, Ctrl+F5). 可以通过强制浏览器获取页面的新版本(即Ctrl + F5)来复制该问题。

I include the code below. 我包括下面的代码。 The first bit is the floating menu, and the second is the smooth scroll. 第一位是浮动菜单,第二位是平滑滚动。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script>
function UpdateTableHeaders() {
   $(".persist-area").each(function() {

       var el             = $(this),
           offset         = el.offset(),
           scrollTop      = $(window).scrollTop(),
           floatingHeader = $(".floatingHeader", this)

       if ((scrollTop > offset.top) && (scrollTop < offset.top + el.height())) {
           floatingHeader.css({
            "visibility": "visible"
           });
       } else {
           floatingHeader.css({
            "visibility": "hidden"
           });      
       };
   });
}

// DOM Ready      
$(function() {

   var clonedHeaderRow;

   $(".persist-area").each(function() {
       clonedHeaderRow = $(".persist-header", this);
       clonedHeaderRow
         .before(clonedHeaderRow.clone())
         .css("width", '100%')
         .addClass("floatingHeader");

   });

   $(window)
    .scroll(UpdateTableHeaders)
    .trigger("scroll");

});
</script>

<script>
    $(function() {
      $('a[href*=#]:not([href=#])').click(function() {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {

          var target = $(this.hash);
          target = target.length ? target : $('[id=' + this.hash.slice(1) +']');
          if (target.length) {
            $('html,body').animate({
              scrollTop: target.offset().top - 40
            }, 1000);
            return false;
          }
        }
      });
    });
</script>

I have a sneaking suspicion that the problem might be a change I made to the floating menu where I set the width to 100% rather than finding the parent width (which then gave a width in pixels) because the element was full width anyway, and when the page is viewed on a mobile the floating menu wasn't updating to the new width when the device orientation changed. 我偷偷地怀疑问题可能是我对浮动菜单所做的更改,因为我将宽度设置为100%而不是找到父宽度(然后以像素为单位给出宽度),因为元素无论如何都是全宽度,并且当在移动设备上查看页面时,当设备方向更改时,浮动菜单不会更新为新的宽度。

If that is the problem, is there a better way to update the width of the menu on orientation change instead? 如果这是问题所在,是否有更好的方法来更改方向更改菜单的宽度?

I know that's quite a lot to go on there! 我知道那有很多事情要做!

OK, I think I might of solved it. 好的,我想我可以解决它。

Where I commented // DOM ready I didn't actually wrap it in a $( document ).ready(function() function. I have now, and it seems to have solved the problem. 在我// DOM ready评论// DOM ready地方,我实际上并没有将它包装在$( document ).ready(function()函数中。现在,我已经解决了这个问题。

Schoolboy error 101! 小学生错误101!

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

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