使用 jQuery UI 定位 div 时,我遇到了一个奇怪的问题。第一次调用该函数会将 div 放在预期的位置。随后的调用将 div 放置在窗口的右侧和底部越来越远。以下是我可以重现该问题的最少代码量。
<!DOCTYPE html>
<html>
<head>
    <title>jQuery UI Position Test </title>
    <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
    <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
    <style type="text/css">
        #header {
            height: 100px;
            background: orange;
        }
        #content {
            height: 300px;
            background: blue;
        }
        #message {
            width: 300px;
            background: yellow;
            position: absolute;
            display: none;
        }
    </style>
</head>
<body>
    <div id="header"></div>
    <div id="message">a message to the user</div>
    <div id="content">
        <input id="ShowMessageButton" type="button" value="Show Message" />
    </div>
    <script>
        $(document).ready(function () {
            $('#ShowMessageButton').bind("click", function () {
                $('#message').position({ my: "center top", at: "center bottom-12", of: "#header" });
                $('#message').fadeIn(800).delay(1000).fadeOut(1000);
            });
        });
    </script>
</body>
</html>
如果您第一次运行代码,这是我所期望的。
屏幕截图 1 http://www.michaelware.net/OutsideImages/jQueryPositionIssue/Screen1.png
第二次通话后
屏幕截图 2 http://www.michaelware.net/OutsideImages/jQueryPositionIssue/Screen2.png
第三次通话后
屏幕截图 3 http://www.michaelware.net/OutsideImages/jQueryPositionIssue/Screen3.png
请注意,您必须让淡入/淡出完成,否则问题不会发生。