2

jqueryアニメーション関数はマルチスレッドで動作していますか?

ここに私のコードで...私がトップボタンをクリックして関数を呼び出すと仮定します...

次に、残りの1回をクリックします。2番目の関数も機能しますが、1回目の呼び出しでも機能します。

では、一度に1つの関数を呼び出すにはどうすればよいですか?

それはマルチスレッドですか?それを殺す方法ですか?

<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()代わりにに変更してください。

2番目の部分については、4つのボタンクリックハンドラーのそれぞれを$('#book').stop(true);呼び出す前に追加します。anim()

$('#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 に答える