overflow与underflow

是新近的firefox浏览器中支持overflow, underflow这两个事件,当某一元素的大小超出父元素的显示范围就会触发overflow事件,如果从超出显示再变回不超出的状态则触发underflow事件.


<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="UTF-8">
        <title>测试用例 by 司徒正美</title>
    </head>
    <body >
        <div id="wrapper">
            <div id="child"></div>
        </div>
        <br/>
        <label><input type="checkbox" id="toggle" checked/> Overflow</label>

        <style>
            #wrapper {
                width: 300px;
                height: 300px;
                background: blue;
                overflow: hidden;
            }

            #child {
                width: 200px;
                height: 200px;
            
                background: yellow;
            }
        </style>

        <script>
            var wrapper = document.getElementById("wrapper"),
                    child = document.getElementById("child"),
                    toggle = document.getElementById("toggle");

            wrapper.addEventListener("overflow", function(event) {
                console.log(event);
            }, false);

            wrapper.addEventListener("underflow", function(event) {
                console.log(event);
            }, false);



            toggle.addEventListener("change", function(event) {
                if (event.target.checked) {
                    child.style.width = "400px";
                    child.style.height = "400px";
                } else {
                    child.style.width = "200px";
                    child.style.height = "200px";
                }

            }, false);
        </script>
    </body>
</html>
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>测试用例 by 司徒正美</title> </head> <body > <div id="wrapper"> <div id="child"></div> </div> <br/> <label><input type="checkbox" id="toggle" checked/> Overflow</label> <style> #wrapper { width: 300px; height: 300px; background: blue; overflow: hidden; } #child { width: 200px; height: 200px; background: yellow; } </style> <script> var wrapper = document.getElementById("wrapper"), child = document.getElementById("child"), toggle = document.getElementById("toggle"); wrapper.addEventListener("overflow", function(event) { console.log(event); }, false); wrapper.addEventListener("underflow", function(event) { console.log(event); }, false); toggle.addEventListener("change", function(event) { if (event.target.checked) { child.style.width = "400px"; child.style.height = "400px"; } else { child.style.width = "200px"; child.style.height = "200px"; } }, false); </script> </body> </html>

运行代码

如果是webkit系统的浏览器,则用overflowchanged这个事件代替

<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>测试用例 by 司徒正美</title> </head> <body > <div id="wrapper"> <div id="child"></div> </div> <br/> <label><input type="checkbox" id="toggle" checked/> Overflow</label> <style> #wrapper { width: 300px; height: 300px; background: blue; overflow: hidden; } #child { width: 200px; height: 200px; background: yellow; } </style> <script> var wrapper = document.getElementById("wrapper"), child = document.getElementById("child"), toggle = document.getElementById("toggle"); wrapper.addEventListener("overflowchanged", function(event) { var overflow = event.verticalOverflow || event.horizontalOverflow var type = overflow ? "overflow" : "underflow" delete event.type event.type = type console.log(event) }, false); toggle.addEventListener("change", function(event) { if (event.target.checked) { child.style.width = "400px"; child.style.height = "400px"; } else { child.style.width = "200px"; child.style.height = "200px"; } }, false); </script> </body> </html>

运行代码

对于不支持的浏览器,那只能轮询判定是否存在滚动条了,可以看这里

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值