自适应屏幕大小的背景图片

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        body{
            background-image: url(123.jpg);
            background-repeat: no-repeat;
        }
    </style>
</head>
<body>
    <script>
        function init(){
            document.body.style.backgroundSize="100% "+document.documentElement.clientHeight+"px";
        }
        init();
        window.onresize = function(){
            init()
        }    
    </script>
</body>
</html>
document.documentElement.clientHeight指的是浏览器的可见高度
onresize是浏览器高度和宽度发生改变

posted on 2017-08-01 11:34  -韩帅  阅读(183)  评论(0编辑  收藏  举报

导航