jquery 背景填充 100% 屏幕,但切断了图像的一部分

标签 jquery css background-image fill

我使用下面的代码来填充页面的整个屏幕。我真正需要的是让它完全填满高度,宽度没有完全填满也没关系。我只需要让背景图片在页面中间居中 100%页面的高度。两侧可以有空格,我不介意。当然我需要保持比例。我试图修改代码,但我不确定我能不能做到。我在这件事上浪费了一些时间,我相信对于比我更有经验的人来说这会很容易。感谢您提前提出任何想法。 代码在这里:

    <img src="http://test.tucado.com/4play/_images/cdj2000-start.jpg" id="bg" alt="">
    <a href="home.html"><div id="wheel-enter"><img src="http://test.tucado.com/4play/_images/empty.png" width="100" height="100" /></div></a>

CSS:

    body{
    height:100%;
    width:100%;
    margin:0;padding:0;
    overflow:hidden;
}


#bg { position: fixed; top: 0; left: 0; }
.bgwidth { width: 100%; }
.bgheight { height: 100%; }

#cdj2000 {
    position:fixed;
    top: 50%;
    left: 50%;
    width:700px;
    height:500px;
    margin-left: -350px; 
    margin-top: -250px; 
}

#wheel-enter {
    position:fixed;
    top: 50%;
    left: 50%;
    width:100px;
    height:100px;
    margin-top: -50px; 
    margin-left: -50px; 
}


img{
    border:none;
}

和jquery:

$(window).load(function() {    

    var theWindow        = $(window),
        $bg              = $("#bg"),
        aspectRatio      = $bg.width() / $bg.height();

    function resizeBg() {

        if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
            $bg
                .removeClass()
                .addClass('bgheight');
        } else {
            $bg
                .removeClass()
                .addClass('bgwidth');
        }

    }

    theWindow.resize(function() {
        resizeBg();
    }).trigger("resize");

});

顺便说一句。我不得不将空图像作为按钮,因为 div 上的某些原因不起作用。奇怪的事情。当我向这个 div 添加边框时,它开始工作,但操作仅在边框上......这很奇怪。当然我不想要边框,因为它看起来像是要单击的背景的一部分。

jsFiddle 在这里:>> jsFiddle <<

感谢您的任何想法。

最佳答案

出于某种原因,JSfiddle 不工作,但这是在测试页面上工作的结果代码:

<style>
   body{
    height:100%;
    width:100%;
    margin:0;padding:0;
    overflow:hidden;
}


#bg { position: fixed; top: 0; left: 50%;}
.bgwidth { width: 100%; }
.bgheight { height: 100%;}

#cdj2000 {
    position:fixed;
    top: 50%;
    left: 50%;
    width:700px;
    height:500px;
    margin-left: -350px; 
    margin-top: -250px; 
}

#wheel-enter {
    position:fixed;
    top: 50%;
    left: 50%;
    width:100px;
    height:100px;
    margin-top: -50px; 
    margin-left: -50px; 
}


img{
    border:none;
}
</style>
<img src="http://test.tucado.com/4play/_images/cdj2000-start.jpg" id="bg" alt="">
    <a href="home.html"><div id="wheel-enter"><img src="http://test.tucado.com/4play/_images/empty.png" width="100" height="100" /></div></a>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
    <script>
    $(window).load(function() {    

    var theWindow        = $(window),
        $bg              = $("#bg"),
        aspectRatio      = $bg.width() / $bg.height();

    function resizeBg() {
            $bg
                .removeClass()
                .addClass('bgheight').css('margin-left',$bg.width()/2*-1);
    }

    theWindow.resize(function() {
        resizeBg();
    });
    resizeBg();

});
</script>

您无缘无故地在窗口的 aspectRatio 上进行测试。因为无论发生什么情况你都想要 100% 的窗口高度,所以我删除了这部分。为了使图片居中,我使用了负边距技巧,将其定位在页面的 50%,也就是中间。然后我应用一个等于调整后图片宽度一半的负边距。

关于jquery 背景填充 100% 屏幕,但切断了图像的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13527018/

相关文章:

javascript - 组合多个变量的 jquery(元素设置为 vars)

javascript - 在 jQuery 选项卡中单击时单选按钮不会更改

Javascript/CSS 显示/隐藏有时不起作用

带有背景图像的 Java 卡布局

css - 我可以使用子类来更改背景图像吗?

javascript - 获取表单中第一个输入的值

php - 将 var 从 javascript/jquery 移动到同一页面上的 php 脚本

asp.net - CSS - 为图像标签设置标题样式

javascript - 单击事件滚动到特定的 div

css - 如何制作覆盖背景图像的透明边框(使用 CSS)?