简体   繁体   English

在函数内部并被调用时,JS代码的行为不同

[英]JS code doesn't act the same when inside a function, and invoked

Example code: Html: 示例代码:HTML:

<div class="wrap">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>
<div class="wrap">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>
<div class="wrap">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

The css: CSS:

*{
  margin: 0;
  padding: 0;
}

.wrap{
  width: 30%;
  height: 200px;
  background: #f7f7f7;
  float: left;
}

.wrap:not(:first-child){
  margin-left: 5%;
}

.item{
  width: 100%;
  height: 47px;
  margin-bottom: 3px;
  background: #999;
}

The js code that works: 起作用的js代码:

(function($){
          $('.wrap').each(function(){
            var thisWrap = $(this),
            naturalHeight = thisWrap[0].scrollHeight,
            restrictedHeight = thisWrap[0].clientHeight;
            if (naturalHeight - 5 > restrictedHeight){
              thisWrap.css({'background': 'red'});
            }
          });



})(jQuery);

The js code that doesn't: 没有的js代码:

(function($){

    function myCustomFunction(){
          $('.wrap').each(function(){
            var thisWrap = $(this),
            naturalHeight = thisWrap[0].scrollHeight,
            restrictedHeight = thisWrap[0].clientHeight;
            if (naturalHeight - 5 > restrictedHeight){
              thisWrap.css({'background': 'red'});
            }
          });
    }

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

    myCustomFunction();

})(jQuery);

In this codepen example both work, but on my test page when the code is in a function and invoked after the definition of the function it doesn't work. Codepen示例中,这两种方法均有效,但是在我的测试页上,当代码位于函数中并在函数定义后调用时,它不起作用。 The wrap element is already on the page when the js loads, the js code detects for overflow, that css is defined in a file that gets loaded on line 39, the js gets loaded on line 1300+. js加载时, wrap元素已经在页面上,js代码检测到溢出,css在第39行加载的文件中定义,js在1300+行加载。 Putting all in a document ready function doesn't help. 全部放入文档就绪功能没有帮助。

How can this code in the same file work properly when not in a function? 当不在函数中时,同一文件中的此代码如何正常工作?

EDIT: The issue seems to be with the window resize function, it seems that something is triggering that while the page loads. 编辑:问题似乎与窗口调整大小功能有关,似乎是在页面加载时触发了某些事件。

This issue was caused by another script which was putting display: none; 此问题是由另一个放置display: none;脚本引起的display: none; styling on the wrap-s(so the problem was it fired too late). 在wrap-s上进行样式设置(因此问题是触发时间太晚了)。 I solved it by adding a setTimeout with 400ms on the tab change click, and including the function there. 我通过在选项卡更改单击上添加400ms的setTimeout并在其中包含函数来解决了该问题。

To fix your problem use this 要解决您的问题,请使用此

$(window).on('resize', function(){
  myCustomFunction();
});

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

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