Script running only once

Iulius

I'm trying to make a side menu that opens and closes from the left when you press the menu button. Got the css, html code running properly but I can't figure out what I'm doing wrong with the script. It works perfectly but only once: When I press the menu button it comes out, press it again and it goes back just as intended. The problem is if I press it again it shows and it goes back by itself. Can anyone help me? Here is my script:

$(document).ready(function(){

$('.menu-icon').click(function(){
    $('#navigator').animate({left:'0px'},200);
    $(this).animate({left:'250px'},200);
    $('.menu-icon').click(function(){
        >$('#navigator').animate({left:'-250px'},200);
        >$(this).animate({left:'0px'},200);
}); 
});
>});
Gone Coding

You have placed a click handler inside a click handler so it will run multiple times. Twice first time, then three times, then four times etc.

You need to have a single handler and decide how to animate based on the current state of the element. e.g. something like the following (not tested):

$(document).ready(function () {

    $('.menu-icon').click(function () {
        if ($('#navigator').css("left") != "0px") {
            $('#navigator').animate({
                left: '0px'
            }, 200);
            $(this).animate({
                left: '250px'
            }, 200);
        } else {
            $('#navigator').animate({
                left: '-250px'
            }, 200); > $(this).animate({
                left: '0px'
            }, 200);
        }
    });
});

I would suggest testing the "current state" using a class you toggle on the element as testing css values is notoriously unreliable during animation.

e.g. something like:

    if ($('#navigator').toggleClass("open").hasClass("open")) {

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

onclick event listener only running once

分類Dev

How to prevent a Perl script from running more than once in parallel

分類Dev

Do I Only Need to Run This Script File Once?

分類Dev

How to fix keypress event for enter key that is running a function twice while only being called once

分類Dev

Powershell loop is only running once file per filename, even if the filename exists with multiple extensions

分類Dev

How to make a script redirect only once every time an appropriate page loads?

分類Dev

Run a script on a gameobject once

分類Dev

drawable rotating only once

分類Dev

the button works only once

分類Dev

replaceWith only working once

分類Dev

Running test suite from both terminal using pytest and unittest runner but show only unittest runner results and execute once

分類Dev

Running all targets at once in a Makefile

分類Dev

Quick-and-dirty way to ensure only one instance of a shell script is running at a time

分類Dev

Foreach Loop only loops once

分類Dev

Retrofit observable works only once

分類Dev

Userform QueryClose Will only Work Once

分類Dev

JS Function only works once

分類Dev

Trigger function only once on scroll

分類Dev

Collapsible only works once (NOT BOOTSTRAP)

分類Dev

Why is the source only called once?

分類Dev

While loop executes only once

分類Dev

Bootstrap Modal shown only once

分類Dev

__doPostBack on event works only once

分類Dev

Bootstrap Alert showing only once

分類Dev

Show Bootstrap popover only once

分類Dev

for loop runs only once (python)

分類Dev

Keeping only rows which are duplicated once only

分類Dev

Script not running at startup

分類Dev

Helping with this jquery script that is not running

Related 関連記事

ホットタグ

アーカイブ