設置背景圖片隨着瀏覽器縮放保持不變

圖片是作爲div背景存在的,div的width是100%,height是根據屏幕高度計算的。用這種方法只實現了瀏覽器縮放背景寬度上保持不變,但是高度變了

background-image: url(../images/bg_1.jpg);
background-size: 100%;
background-repeat: no-repeat;

以下是設置div高度的代碼

var changeBg = (function(){
        var count = 0,
            height = (document.body.scrollHeight > document.documentElement.scrollHeight ? document.body.scrollHeight : document.documentElement.scrollHeight) - $(".header").outerHeight() - $(".footer").outerHeight() + 'px',
            $bg = $(".home-bg");

        $bg.css({
            height: height
        });

        if(_searchConfig["catagory"]["indexBackgroundImage"] && _searchConfig["catagory"]["indexBackgroundImage"].length > 0){
            setInterval(function(){
                $bg.css({
                    background: '#425061 url("' + _searchConfig["catagory"]["indexBackgroundImage"][count].properties.imageUrl + '") repeat center',
                    height: height
                });

                count++;

                if(count >= _searchConfig["catagory"]["indexBackgroundImage"].length){
                    count = 0;
                }
            }, 5000);
        }
    })();

先寫這麼多,有空再來改

發佈了52 篇原創文章 · 獲贊 14 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章