web前端一起努力

Client 家族

2018-04-25  本文已影响0人  追逐_chase
timg.jpg
clientWidth
可视区域的检测

//封装

<script type="text/javascript">
    function client() {
        if (window.innerWidth != null){

            return {
                width:window.innerWidth,
                height:window.innerHeight
            }
        } else  if (window.compatMode == "CSS1Compat"){

            return {
                width:document.documentElement.clientWidth,
                height:document.documentElement.clientHeight
            }
        }
        return {
            width:document.body.clientWidth,
            height:document.body.clientHeight
        }
    }
</script>
window.onresize 改变窗口事件
<script type="text/javascript">
    //函数 值调用一次
    reSize();

    window.onresize =  reSize; // 不带括号,只要屏幕出发,就调用,是一个 函数体


        function reSize() {

            var clientWidth = client().width;

            if (clientWidth > 960){

                document.body.style.backgroundColor = "red";
            } else if(clientWidth > 640){
                document.body.style.backgroundColor = "green";
            }else{
                document.body.style.backgroundColor = "pink";
            }


    }


    function client() {
        if (window.innerWidth != null){

            return {
                width:window.innerWidth,
                height:window.innerHeight
            }
        } else  if (window.compatMode == "CSS1Compat"){

            return {
                width:document.documentElement.clientWidth,
                height:document.documentElement.clientHeight
            }
        }


        return {
            width:document.body.clientWidth,
            height:document.body.clientHeight
        }
    }

</script>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>06-点击空白处隐藏</title>

    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }

        body {
            height: 2000px;
        }

        #box {
            width: 100%;
            height: 100%;
            opacity: 0.4;
            filter: alpha(opacity = 40);  /* ie 6 半透明*/
            background-color: black;
            position: fixed;
            top: 0;
            left: 0;
            display: none;

        }
        #logo {
            width: 300px;
            height: 300px;
            background-color: #ffffff;
            position: fixed;
            left: 50%;
            top: 50%;
            margin-left: -150px;
            margin-top: -150px;
            display: none;
        }


    </style>
</head>
<body>

    <a href="javascript:;" id="login">注册</a>
    <a href="javascript:;">登录</a>

    <div id="box" ></div>
    <div id="logo"></div>

</body>
</html>

<script type="text/javascript">

    function $(id) {

        return document.getElementById(id);
    }


    var  box = $("box");
    var login = $("login");
    var logo = $("logo");

    login.onclick = function (event) {
        //点击隐藏滚动条
        document.body.style.overflow = "hidden";
        box.style.display = "block";
        logo.style.display = "block";
        var event = event || window.event;

        if (event && event.stopPropagation){
            event.stopPropagation();
        } else  {
            event.cancelBubble = true;
        }

    }

    document.onclick = function (event) {
        var event = event || window.event;
    var targetId = event.target ? event.target.id : event.srcElement.id;
    if (targetId != "logo"){
        box.style.display = "none";
        logo.style.display = "none";
    }

    }



</script>
110.gif
上一篇 下一篇

猜你喜欢

热点阅读