javascript - jQuery .show/.hide 被多次触发

标签 javascript jquery html css

我有一个包含 4 个图 block 的页面,当鼠标悬停在它上面时会显示 2 个选项:New FormForm List

但是,我希望隐藏这两个选项,除非用户当前将鼠标悬停在图 block 上。这工作正常,但它在第一次悬停事件后执行 2 或 3 次。基本上,在页面加载时,隐藏 2 个选项并显示磁贴。一旦我将鼠标悬停在第一个图 block 上,这两个选项就会使用 .show() 方法显示出来。如果我的鼠标离开图 block ,将调用 .hide() 方法隐藏这两个选项。

但是,如果我将鼠标悬停在同一个图 block 上,它会同时执行 .hide().show() 2 次,有时甚至更多.我假设我在做一些愚蠢的事情,但在网上找不到任何帮助。 JSFiddle 可以在这里找到:http://jsfiddle.net/n97M8/

enter image description here

代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <style type="text/css">
            .container
            {
                height: 150px;
                text-align:center;
            }
            .FormTiles
            {
                position: relative;
                text-align: center;
                vertical-align: middle;
                float:left;
                width: 150px;
                height: 0px;
                margin-left: auto;
                margin-right: auto;
                background-color: green;
                color: white;
                display: inline; 
                font-family: Verdana;
                font-size:small;
                margin-right: 5px;
                padding-bottom: 150px;
            }

            #optionsContainer, #optionsContainer1, #optionsContainer2, #optionsContainer3
            {
                position: absolute;
                width: 100%;
                text-align: center;
                bottom: 0;
                background-color: gray;
                padding-top: 3px;
                padding-bottom: 3px;
                display: none;
            }
        </style>
    </head>

    <body>
        <div class="container">
            <div class="FormTiles" id="workForInspector">Work For Inspector Form
                <div id="optionsContainer">
                    <div class="FormTilesNewForm">New Form</div>
                    <div class="FormTilesToList">Form List</div>
                </div>
            </div>
            <div class="FormTiles" id="Form2">Form 2
                <div id="optionsContainer1">
                    <div class="FormTilesNewForm">New Form</div>
                    <div class="FormTilesToList">Form List</div>
                </div>
            </div>
            <div class="FormTiles" id="Form3">Form 3
                <div id="optionsContainer2">
                    <div class="FormTilesNewForm">New Form</div>
                    <div class="FormTilesToList">Form List</div>
                </div>
            </div>

            <div class="FormTiles" id="Form4">Form 4
                <div id="optionsContainer3">
                    <div class="FormTilesNewForm">New Form</div>
                    <div class="FormTilesToList">Form List</div>
                </div>
            </div>

        </div>
        <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {

                $("#workForInspector").hover(function () {
                    $("#optionsContainer").show("fast");
                });
                $("#workForInspector").mouseout(function () {
                    $("#optionsContainer").hide("fast");
                });

            });

        </script>
    </body>
</html>

最佳答案

使用jQuery的.stop()函数来停止鼠标移动的排队:

$("#workForInspector").hover(function () {
    $("#optionsContainer").stop().show("fast");
}, function () {
    $("#optionsContainer").stop().hide("fast");
});

jsFiddle example

关于javascript - jQuery .show/.hide 被多次触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25049663/

相关文章:

javascript - 如何连接或合并两个数组

javascript - 将 JQuery UI 效果应用于文本时出现滚动条问题

html - 定位 webkit-scrollbar absolute 和 vertically center

javascript - 使用 jQueryeach() 动态添加图像到 div ..不知何故,它们都添加到最后一个 div

javascript - 模态背景模糊

php - PHP 中数组输入的键不正确

javascript - Tinymce 如何 float 图像?

javascript - 为什么带有 ng-repeat 的指令是从表中编译出来的?

javascript - 如何判断 XUL menupopup 打开还是打开?

Javascript 空字段验证 - 无法读取未定义的属性 'value'