2

jquery 动画函数在多线程上工作吗?

在我的代码中......假设我点击顶部按钮并调用函数工作正常......

现在单击任何剩余的一次....第二个功能也可以使用,但也可以使用第一个调用。

那么,我如何一次调用一个函数?

它是多线程吗?这是杀死它的方法吗?

<table width="1200px" height="600px" align="center">
        <tr>
            <td>
            </td>
            <td style="text-align: center">
                <button id="btntop">
                    top</button>
            </td>
            <td>
            </td>
        </tr>
        <tr>
            <td style="text-align: right">
                <button id="btnleft">
                    left</button>
            </td>
            <td style="text-align: center">

                <img id="book" src="Image/images.jpg" alt="" width="250" height="123" style="position: relative;
                    left: 10px;" />

            </td>
            <td>
                <button id="btnright">
                    right</button>
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td style="text-align: center">
                <button id="btnbottom">
                    bottom</button>
            </td>
            <td>
            </td>
        </tr>
    </table>
    <script type="text/javascript">


        function anim(direction) {


            if (direction == "top") {

                $('#book').animate({

                    top: '-=10'

                }, 200, function () {


                        anim("top");

                });

            }

            if (direction == "left") {
                $('#book').animate({

                    left: '-=10'
                }, 200, function () {

                        anim("left");

                });
            }

            if (direction == "right") {
                $('#book').animate({

                    left: '+=10'
                }, 200, function () {

                        anim("right");

                });
            }

            if (direction == "bottom") {
                $('#book').animate({

                    top: '+=10'
                }, 200, function () {

                        anim("bottom");

                });
            }
        }


        $('#btntop').hover(function () {

            anim("top");

        });


        $('#btnleft').hover(function () {

            anim("left");

        });

        $('#btnright').hover(function () {


            anim("right");

        });

        $('#btnbottom').hover(function () {


            anim("bottom");

        });


    </script>
4

1 回答 1

1

您正在调用您的anim()函数,hover()这不是一个好主意。改为click()改为。

至于第二部分,$('#book').stop(true);在调用anim()每个 4 按钮单击处理程序之前添加:

$('#btntop').click(function () {
    $('#book').stop(true);
    anim("top");
});

$('#btnleft').click(function () {
    $('#book').stop(true);
    anim("left");
});

$('#btnright').click(function () {
    $('#book').stop(true);
    anim("right");
});

$('#btnbottom').click(function () {
    $('#book').stop(true);
    anim("bottom");
});

它将停止所有当前正在运行的动画并开始新的动画。

于 2013-02-07T12:04:04.480 回答