简体   繁体   English

JS仅在刷新页面几次时有效

[英]JS only works when refreshing the page a few times

I have 3 columns that extend to equal height 我有3列延伸到相等高度

and this is the JS that makes those 3 columns work: SEE DEMO 这是使这3列起作用的JS: SEE DEMO

<script>
  $(document).foundation();
</script> 
<script>
   if(!(/iPhone|iPad|iPod|Android|webOS|BlackBerry|Opera Mini|IEMobile/i.test(navigator.userAgent) )) {
jQuery(document).ready(function($){
    var inHeight = $("#wrapper").innerHeight();
    $("#wrapper .col").each(function(){
        $(this).height(inHeight+"px");
        $(this).find('.content').height((inHeight-60)+"px");
    });
}); 
}
</script>

The problem is that this only works when I refresh the page. 问题是,这仅在刷新页面时有效。 Sometimes I even need to refresh the page a couple of times to make it work. 有时,我什至需要刷新页面几次才能使其正常工作。 Is there any way to fix this issue? 有什么办法可以解决此问题? Thanks! 谢谢!

This is my JS structure: 这是我的JS结构:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script src="js/modernizr.js"></script>
<script src="js/foundation.min.js"></script> 
<script>
  $(document).foundation();
</script> 
<script>
   if(!(/iPhone|iPad|iPod|Android|webOS|BlackBerry|Opera Mini|IEMobile/i.test(navigator.userAgent) )) {
jQuery(document).ready(function($){
    var inHeight = $("#wrapper").innerHeight();
    $("#wrapper .col").each(function(){
        $(this).height(inHeight+"px");
        $(this).find('.content').height((inHeight-60)+"px");
    });
}); 
}
</script> 

If as per your comment, you need IE8+ support, lets try solving your problem pure HTML way, no JS required !! 如果根据您的评论,您需要IE8 +支持,请尝试以纯HTML方式解决您的问题,而无需JS :) :)

demo here 在这里演示

Remove all the JS and float from your code, then, see the comments in markup below : 从代码中删除所有JSfloat ,然后,查看下面标记中的注释:

#wrapper {
    height: 100%;
    width:  100%;
    margin: 20px auto;
    display:table; /* added, so your div will behave like css-table */
    overflow: hidden;
}
#wrapper > .col {
    display: table-cell; /* abra-ka-dabra, equal height for all divs!!*/
    width: 30.3%;
    border:1px solid red;
    margin: 0 15px 3px;
    background-color: #fff;
    text-align: center;
    position: relative; /* required, because we need to place button @ he bottom of div*/
    height: 100%;
    -moz-box-shadow: 0px 1px 2px rgba(48, 48, 48, 0.3);
    -webkit-box-shadow: 0px 1px 2px rgba(48, 48, 48, 0.3);
    box-shadow: 0px 1px 2px rgba(48, 48, 48, 0.3);
}

    div.btn-align-bottom {
        position:absolute; /* place button at the bottom of div */
        bottom:50px; /* placing at proper position */
        left:0; /* needed for centering */
        right:0;/* needed for centering */
        border:1px solid yellow /* just to show you */
    }

EDIT 编辑

If as per comment you have image in you markup,make sure to mention : 如果根据评论,您在标记中有图片,请务必提及:

img{
    vertical-align:top; /* change default of baseline to top*/
}

working demo 工作演示

window.onload = function() {
    if(!window.location.hash) {
        window.location = window.location + '#';
        window.location.reload();
    }
}

This snippet reloads your pace once 此摘要可让您重新调整节奏

This happens because you are triggering your script before the images are loaded, so you cannot know the real innerHeight of #wrapper . 发生这种情况是因为您在加载图像之前触发了脚本,因此您无法知道#wrapper的真实innerHeight

You options are specifying the image height on the DOM 您可以选择在DOM上指定图像高度

OR 要么

Triggering this function on window onLoad. 在窗口onLoad上触发此功能。

Also, you don't need $.each: 另外,您不需要$ .each:

var inHeight = $("#wrapper").innerHeight();
$("#wrapper .col").css("height", inHeight+"px");
$("#wrapper .col .content").css("height", (inHeight-60)+"px");

An optimized way (caching wrapper): 一种优化的方式(缓存包装器):

var $wrapper = $("#wrapper");
var inHeight = $wrapper.innerHeight();
$wrapper.find(".col").css("height", inHeight+"px");
$wrapper.find(".col .content").css("height", (inHeight-60)+"px");

The problem occurs because your elements are being floated and therefore the DOM doesn't see them as solid objects. 发生问题是因为您的元素是浮动的,因此DOM不会将它们视为实体对象。

You'll need to make a couple of edits to the CSS to fix the problem. 您需要对CSS进行几次修改才能解决此问题。

#wrapper {
    height: 100%;
    width: 100%;
    margin: 20px auto;
    overflow: auto;
}

#wrapper > .col {
    display: inline-block;
    float: left;
    width: 30.3%;
    margin: 0 15px 3px;
    background-color: #fff;
    text-align: center;
    position: relative;
    height: 100%;
    -moz-box-shadow: 0px 1px 2px rgba(48, 48, 48, 0.3);
    -webkit-box-shadow: 0px 1px 2px rgba(48, 48, 48, 0.3);
    box-shadow: 0px 1px 2px rgba(48, 48, 48, 0.3);
    overflow: auto;
}
#wrapper > .col > .content {
    padding: 0 0 35px;
    overflow: auto;
}

And for the jQuery side of it, you'll need to change it to this: 对于jQuery ,您需要将其更改为:

var inHeight = $("#wrapper").innerHeight();
$("#wrapper .col").each(function(){
    $(this).height(inHeight+"px").css('overflow', 'hidden');
    $(this).find('.content').height((inHeight-60)+"px").css('overflow', 'hidden');
});

If you want to give that a try. 如果您想尝试一下。

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

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