如果语句在调整大小时有效,则未准备好
If statement works on resize, not ready?
我想完成两件事:
-
主内容div将动态调整大小以适合窗口的精确高度。我用这个函数实现了这个目标:
$(document).on( 'ready', function() { panelsResize(); $(window).on( 'resize', panelsResize ); }); function panelsResize() { var screenHeight = $(window).height(); var screenWidth = $(window).width(); if (screenWidth > 768) { $( ".panels" ).css( "height", screenHeight ); } };
类.panels
应用于主内容区。
这个很好用。
- 我正试图用
.large
中的图像填充.panels
。这些图像需要保持长宽比,同时可伸缩。我的代码基于这个答案。
这个可以工作,但不是on ready。我必须调整屏幕大小,在将.large
的显示切换为none的媒体查询下面。当我调整大小到更高的媒体查询,切换显示块,功能工作完美。
任何想法?
下面是函数(和标记): $(document).on( 'ready', function() {
bgResize();
$(window).on( 'resize', bgResize );
});
function bgResize(){
$( '.large' ).each(function() {
var th = $(window).height(),//box height
tw = $(window).width(),//box width
im = $(this).children('img'),//image
ih = im.height(),//inital image height
iw = im.width();//initial image width
if ( iw < tw ) {
im.css( { "width" : "100%", "height" : "auto" } );
}
else if ( ih < th ) {
im.css( { "height" : "100%", "width" : "auto" } );
}
var nh = im.height(),//new image height
nw = im.width(),//new image width
hd = (nh-th)/2,//half dif img/box height
wd = (nw-tw)/2;//half dif img/box width
if (nh<nw) {
im.css({marginLeft: '-'+wd+'px', marginTop: 0});//offset left
} else {
im.css({marginTop: '-'+hd+'px', marginLeft: 0});//offset top
}
});
};
这是HTML:
<ul id="homePanels">
<li id="cat1" class="panels">
<div class="large">
<img src="#" alt="" />
</div>
<div class="small">
<img src="#" alt="" />
</div>
</li>
</ul>
CSS: #homePanels {
list-style: none;
margin:0;
padding:0;
overflow: hidden;
}
.large {
width:100%;
height:100%;
}
@media (min-width: 768px) {
.large{display: block;}
.small{display:none}
}
@media (max-width: 768px) {
.small {display:block;}
.large {display:none;}
}
我想如果你使用jQuery ready()函数,它会起作用的。
$(document).ready(function() {
bgResize();
$(window).on('resize', bgResize );
});
你只需要改变
$(document).on( 'ready', function()
$(document).ready( function()
谢谢!我对ready事件进行了更改。
我意识到使用图像的宽度和高度导致了这个问题,因为我基本上是抓取了两个0。因为我知道图像的长宽比,我用它来缩放屏幕调整大小的图像。
var screenHeight = $(window).height(),
screenWidth = $(window).width(),
aspectRatio = 0.57066014669927,
screenAspectRatio = screenHeight / screenWidth;
if ( screenAspectRatio < aspectRatio ){
$("img").css({"width" : "100%" , "height" : "auto"});
} else if ( screenAspectRatio > aspectRatio ){
$("img").css({"width" : "auto" , "height" : "100%"});
}
但现在问题解决了。谢谢你的帮助!
相关文章:
- 在jquery文档准备好之前加载Modernizr
- 请求准备一个文件并返回路径,允许用户在准备好时下载
- Electron:Express.js服务器在Mongo数据库'It’准备好了
- 从Document调用onchange函数.已准备好从更新中预先选择的项目
- jquery触发器可以从点击开始工作,但没有准备好
- Javascript/Css导航未显示在jquery文档上,仅在ie中准备就绪
- 我怎么知道Jqgrid已经准备好了
- Javascript+HTML中的日期选择器准备好了吗?(需要:DD、MM、YY的单独字段加上日历选择器)
- 创建元素更好还是只在html上准备好它们
- 电话间隙设备准备就绪,5秒钟后未启动
- 如何移除预先准备好的容器
- 加载直到图像准备好,而图像隐藏直到它准备好
- $().bind 在未准备好文档的情况下无法正常工作
- 对象不为 null,也不未定义.更好的是 - 双重反转'!!' 或 JavaScript 中的严格等价性 '!=='
- travis-ci 在 grunt-contrib-imagemin 任务上构建失败(无法准备好未定义的属性“内容”
- localStorage未存储预先准备好的注释
- 检查“”中是否存在属性;窗口“;如果“;窗口"未准备好
- 如果语句在调整大小时有效,则未准备好
- 流星启动JQuery DOM元素未准备好(JQueryUI Draggable)
- React-native-meteor包订阅句柄未准备好