1

当我像这样聚焦 DOM 时,如何防止scrollTopIE 发生变化?

<html>
    <head>
        <title>demo</title>
        <script type="text/javascript">
            function test() {
                document.getElementById('div1').focus();
                //document.body.scrollLeft = 0;
                //document.body.scrollTop = 0;
            }
        </script>
    </head>
    <body onload="test()" scroll='no' style='overflow:hidden'>

        <div id="div0" style="width:1400px;height:700px;background:#333333;position:absolute;"> </div>

        <div id="div1" style="top:1500px;width:1500px;height:300px;background:#DDDDDD;position:absolute; "> </div>

    </body>
</html>

我不想重置scrollTop=0,因为它会使身体颤抖。

4

1 回答 1

0

这是你想要的效果吗?

<!DOCTYPE html>
<html>
<head>
    <title>demo</title>
    <style>
        body {
            margin: 0;
            padding: 0;
        }

        #div0, #div1 {
            position: absolute;
            height: 100px;
            width: 100%;
        }

        #div0 {
            background: #333;
        }

        #div1 {
            background: #ddd;
        }
    </style>
</head>
<body>
    <div id="div0" style="height:700px;"> </div>

    <div id="div1" style="top:1500px;height:300px;"> </div>

    <script type="text/javascript">
        (function() {
            document.getElementById('div1').focus();
        })();
    </script>
</body>
</html>
于 2013-07-11T18:31:39.203 回答