js基础03特效简单响应式页面布局client()函数封装

网页可视区的宽高的获取:
client系列:
clientWidth用于响应式布局:
窗体大小发生改变的事件 onresize
封装能够获取网页可视区的宽度和高度

<script>
        //窗体大小发生改变的事件 onresize
        window.onresize=function () {
            console.log(client().width);
        }
        //封装能够获取网页可视区的宽度和高度
        function client(){
            var clientWidth=window.innerWidth || document.documentElement.clientWidth||document.body.clientWidth||0;
            var clientHeight=window.innerHeight || document.documentElement.clientHeight||document.body.clientHeight||0;
            var o={};//点出来的东西式一个对象
            o.width=clientWidth;
            o.height=clientHeight;
            return o;
        }
        //封装函数简化:
        function client(){
            return {
                width:window.innerWidth || document.documentElement.clientWidth||document.body.clientWidth||0,
                height:window.innerHeight || document.documentElement.clientHeight||document.body.clientHeight||0
            };//点出来的东西是一个对象
        }
    </script>

体会响应式布局原理(其中一种实现响应式布局的方式):
案例:当页面宽度 大于960像素时 页面为红色并显示computer;
当页面宽度 大于640 小于960 页面为绿色并显示 tablet
剩余情况为黄色并显示mobile。

<script>
        window.onresize=function () {//页面改变时执行
            responsive();
        };
        responsive();//页面一加载就开始执行

        function responsive() {
            if (client().width>960) {
                document.body.style.backgroundColor="red";
                document.body.innerHTML="computer";
            }else if (client().width>640) {
                document.body.style.backgroundColor="green";
                document.body.innerHTML="tablet";
            }else{
                document.body.style.backgroundColor="yellow";
                document.body.innerHTML="mobile";
            }
        }
        function client(){
            return {
                width:window.innerWidth || document.documentElement.clientWidth||document.body.clientWidth||0,
                height:window.innerHeight || document.documentElement.clientHeight||document.body.clientHeight||0
            };
    </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值