简体   繁体   English

背景图片-如何仅使用CSS正确缩放

[英]Background Image - how to scale properly with just css

I have this site in development, http://melanie.3drexstaging.com/ The client wants the background to scale up or down (to a point) depending on the browser size. 我的网站正在开发中, 网址为http://melanie.3drexstaging.com/ 。客户希望根据浏览器的大小来扩大或缩小背景(达到一定程度)。 I have gotten it to work halfway decent with some jquery hacks to adjust sizing of elements as the browser resizes, but I would much prefer a css only option. 我已经使它可以与一些jquery hack一起进行一半的工作,以随着浏览器调整大小来调整元素的大小,但是我更喜欢仅使用css的选项。 We have 3 images, one is the fixed aspect ratio center image that should always show in entirety, and to the left and right we have the images that continue the pattern. 我们有3张图像,其中一张是固定的长宽比中心图像,应始终完整显示,而在左右两侧则有延续图案的图像。

Thanks in advance for any tips! 在此先感谢您提供任何提示!

My SAD javascript hacks: 我的SAD JavaScript hacks:

<script type="text/javascript">
    $(document).ready(function () {
        fixBg();
    });
    $(window).load(function () {
        fixBg();
    });
    $(window).resize(function () {
        fixBg();
    });
    function fixBg()
    {
        var mh = Math.max($(window).height(), 768);
        if ($("#ControlBar").length > 0) {
            mh -= 54;
        }
        var mw = Math.round((mh / 768) * 1264);
        var winW = Math.max(1264, $(window).width());
        $("#bgCenter").height(mh).width(mw);
        $("#shade").height(mh).width(mw).css("left", ((winW - mw) / 2) + 10);
        var extra = ((winW - mw) / 2) + 10;
        $(".bgFillers").width(extra).height(mh);
        var bgw = (mh / 768) * 360;
        $(".bgFillers").css("background-size", bgw + "px " + mh + "px");
        //$("#siteContent").css("min-height", mh - $("#header").height() - $("#footer").height() - 20);
    }
</script>

And the basic markup: 和基本标记:

<div id="master">
    <div id="bg">
        <div id="bgLeft" class="bgs bgFillers"></div>
        <div id="bgCenter" class="bgs">
        </div>
        <div id="bgRight" class="bgs bgFillers"></div>
    </div>
    <div id="shade"></div>
    <div id="centeredSite">
    </div>
</div>

have you tried making the size relative? 您是否尝试过将尺寸设为相对尺寸?

you just use % instead of px (or any other option) 您只使用%而不是px(或任何其他选项)

100% is full element, 50% is half your element (if you put it in a div you set to 50% it'll take half of the div that's taking up half of your page, so 1/4th of your page) 100%是完整元素,50%是元素的一半(如果将其放在div中,则设置为50%,它将占div的一半,即占页面的一半,因此是页面的1/4)

otherwise i'll need some more information 否则我需要更多信息

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

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