最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - html onresizeend event, or equivalent way to detect end of resize - Stack Overflow

programmeradmin3浏览0评论

I'm trying to detect when the onresize event ends in a browser. If I use the onresize event, in Firefox it seems to be fired only once, after the resize event ends, which is exactly what I want. But if I try in IE, the onresize event gets fired many times during the resize.

I also try the onresizeend event, advertised in MSDN. But it does not seem to get fired at all, neither in Firefox, nor in IE. I use the following code:

<html>
    <head>
        <script>
        <!--
        function doLog(message)
        {
            document.getElementById("log").innerHTML += "<br/>" + message;
        }
        
        function doResize()
        {
            doLog("plain resize");
        }

        function doResizeEnd()
        {
            doLog("resize end");
        }
        -->
        </script>
        <style>
        <!--
        #log {
            width: 400px;
            height: 600px;
            overflow: auto;
            border: 1px solid black;
        }
        -->
        </style>
    </head>
    <body onresize="doResize();" onresizeend="doResizeEnd();">
        <div id="log"/>
    </body>
</html>

Any ideas why this does not work? Maybe the onresizeend event is not supported? In this case, how can I detect when the resize event has ended?

I'm trying to detect when the onresize event ends in a browser. If I use the onresize event, in Firefox it seems to be fired only once, after the resize event ends, which is exactly what I want. But if I try in IE, the onresize event gets fired many times during the resize.

I also try the onresizeend event, advertised in MSDN. But it does not seem to get fired at all, neither in Firefox, nor in IE. I use the following code:

<html>
    <head>
        <script>
        <!--
        function doLog(message)
        {
            document.getElementById("log").innerHTML += "<br/>" + message;
        }
        
        function doResize()
        {
            doLog("plain resize");
        }

        function doResizeEnd()
        {
            doLog("resize end");
        }
        -->
        </script>
        <style>
        <!--
        #log {
            width: 400px;
            height: 600px;
            overflow: auto;
            border: 1px solid black;
        }
        -->
        </style>
    </head>
    <body onresize="doResize();" onresizeend="doResizeEnd();">
        <div id="log"/>
    </body>
</html>

Any ideas why this does not work? Maybe the onresizeend event is not supported? In this case, how can I detect when the resize event has ended?

Share Improve this question edited Jun 18, 2022 at 14:07 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 10, 2008 at 12:36 GabrielGabriel 2,3839 gold badges29 silver badges41 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

From MSDN onresizeend()

Only content editable objects can be included in a control selection. You can make objects content editable by setting the contentEditable property to true or by placing the parent document in design mode.

That explains why it's not firing for you. I suspect you don't want to enable contentEditable, so why not set a timer.

var resizeTimer = 0;
function doResize()
{
    if (resizeTimer)
        clearTimeout(resizeTimer);

    resizeTimer = setTimeout(doResizeEnd, 500);
}

It's not perfect but hopefully will be good enough.

发布评论

评论列表(0)

  1. 暂无评论