jquery - 图像未反射(reflect)其属性

标签 jquery html css background

http://grab.by/1rI8

我写了一些 jQuery 脚本,这样背景图片就会一直缩放,永远不会显示空白点。

    function runIt() {
currentHeight = $("#bg").height() //useless for you
currentWidth = $("#bg").width() //useless for you
ccurrentHeight = $("#cover").height() //useless for you
ccurrentWidth = $("#cover").width() //useless for you
wcurrentHeight = $(window).height() //useless for you
wcurrentWidth = $(window).width() //useless for you
$("#auto").empty() //useless for you
$("#auto").append('BG Current Height:"'+currentHeight+'"Width"'+currentWidth+'"<br \>') //useless for you
$("#auto").append('Cover Current Height:"'+ccurrentHeight+'"Width"'+ccurrentWidth+'"<br \>') //useless for you
$("#auto").append('Window Current Height:"'+wcurrentHeight+'"Width"'+wcurrentWidth+'"') //useless for you
if ($("#bg").height() < $(window).height()) {
//  $("#bg").empty() //useless for you
//  $("#bg").append('<img src="'+imgPath+'" style="height:100%" />') //useless for you
        $("#bg").css({
        width: $("#cover").width(),
        height: "auto"
        })
    }
else {
//  $("#bg").empty() //useless for you
//  $("#bg").append('<img src="'+imgPath+'" style="width:100%" />') //useless for you
        $("#bg").css({
        height: $("#cover").height(),
        width: "auto"
        })
    }
}
setInterval(runIt, 50);

有人知道为什么这不能正常工作吗?

谢谢, 斯威茨

最佳答案

    function runIt() {
 winprop = $(window).height() / $(window).width();
 var bgprop = $("#bgimg").height() / $("#bgimg").width();
 if(winprop < bgprop)
 {
         $("#bgimg").css({
         width: $(window).width(),
         height: "auto"
         })
 } else {
         $("#bgimg").css({
         width: "auto",
         height: $(window).height()
         })
    }
}
function doSomething() {
    alert("I'm done resizing for the moment");
};
 runIt();
var resizeTimer = null;
$(window).bind('resize', function() {
    if (resizeTimer) clearTimeout(resizeTimer);
    resizeTimer = setTimeout(runIt, 20);
});

把Bg图片给自己的id。更改图像的大小,而不是 div。

关于jquery - 图像未反射(reflect)其属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1977914/

相关文章:

javascript - jQuery 表到 CSV 导出

javascript - 如何在照片之间制作滑动的东西?

html - 固定页面布局 HTML

javascript - 如何使用html2canvas捕获多个Canvas

html - rails 5 中具有线性渐变的背景图像

javascript - jQuery 并实时检测滚动位置

asp.net-mvc - Web API 无法处理我不知道自己正在发送的参数?

html - 选择具有数据属性的第一个元素

javascript - toggleClass/addClass 不适用于输入

html - 左侧导航栏与 IE 中的内容一起移动(必须独立滚动)