刷新页面后,jquery脚本无效。我使用了$(document).ready();和$(window).load();功能。此代码用于拟合图像。
这是我的HTML代码:
<div id="coverPicContainer">
<img class="cover" src="<?php echo base_url()?>core/uploads/company/<?php echo $cover_image?>" alt="cover"/>
</div>`
CSS代码
#coverPicContainer {
width: 100vw;
margin-left:-15px;
height: 410px;
position: relative;
overflow: hidden;
background-color:#ffffff;
border: 1px solid #d3d3d3;
}
#coverPicContainer img.wide {
display: block;
width: 100%;
max-height: 100%;
height: auto;
margin: 0 auto;
vertical-align: middle;
}
#coverPicContainer img.tall {
display: block;
max-height: 100%;
width: 100%;
width: auto;
margin: 0 auto;
vertical-align: middle;
}
和jQuery代码
$(window).load(function() {
$('#coverPicContainer').find('img').each(function() {
var imgClass = (this.width / this.height > 1) ? 'wide' : 'tall';
$(this).addClass(imgClass);
});
});
当我刷新页面时,它的工作时间有一段时间不起作用。 谢谢你的帮助。
答案 0 :(得分:0)
始终在这里工作(运行,刷新,更新): http://jsfiddle.net/L4m9dfby/1/
注意:脚本在Windows加载时执行。
针对您的特定问题的可能解决方案是将window.load function
放在document ready
之外。