最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Moving div right and left on click - Stack Overflow

programmeradmin4浏览0评论

I want to make div go left the first time i click on it, back to its original position the second time, left again the third etc..

So why isn't this working: /

    var checker = new Boolean();
checker = true;
if (checker = true){
    $("#div").click(function () {   
        $(this).animate({left: "10%"},400);   
        checker = false;
    });
}
if (checker != true){
    $("#div").click(function () {   
        $(this).animate({left: "30%"},400);   
        checker = true;
    });
}

I want to make div go left the first time i click on it, back to its original position the second time, left again the third etc..

So why isn't this working: http://jsfiddle/kS7KE/2/

    var checker = new Boolean();
checker = true;
if (checker = true){
    $("#div").click(function () {   
        $(this).animate({left: "10%"},400);   
        checker = false;
    });
}
if (checker != true){
    $("#div").click(function () {   
        $(this).animate({left: "30%"},400);   
        checker = true;
    });
}
Share Improve this question asked Apr 12, 2013 at 22:04 KoiskiKoiski 5682 gold badges8 silver badges23 bronze badges
Add a ment  | 

8 Answers 8

Reset to default 3

Why so much code ? :)

var checker = true;
$("#div").click(function () {
    targetLeft = checker ? "10%" : "30%";
    $(this).animate({left: targetLeft},400);   
    checker ? checker = false : checker = true;
});

http://jsfiddle/kS7KE/5/

A few issues:

  1. if (checker = true)

    You are using an assignment (=) rather than a check for equality (== or ===).

  2. Your check happens at bind time

    You want to check checker in your click event.

Soemthing like this will do the trick:

var checker = new Boolean();
checker = true;
$("#div").click(function () { 
    var left = checker ? "10%" : "30%";
    $(this).animate({left: left},400);   
    checker = !checker;
});

http://jsfiddle/lbstr/L4TN3/

First, don't use new Boolean(). Just initialize it like var checker = true;

Second, you're using an assignment operator in your first conditional (checker = true).

Third, you're not executing the conditional on each click.

Fourth, your code is pretty convoluted. Here's a sample:

var checker = true;
$('#div').click(function() {
    $(this).animate({
        left: checker ? "10%" : "30%"
    });
    checker = !checker;
});

You should not attach multiple event handlers to the same object which conflict with each other like you did. Here is a bit clearer and simpler version which does the same thing your code is supposed to do. As you can see it makes use of only one event handler.

var checker = true;
$('#div').click(function(){
    if(checker){
       $(this).animate({left: "10%"},400);   
       checker = false; 
    } else {
        $(this).animate({left: "30%"},400);   
        checker = true;
    }
});

Additionally you perform an assignment in your first if statement (single =) instead of using == to pare your operands.

You need to assign the click event once, and do the logic in it : http://jsfiddle/kS7KE/2/

var checker = new Boolean();
checker = true;
$("#div").click(function () { 

if (checker == true){       
        $(this).animate({left: "10%"},400);   
        checker = false;
    }
else{

        $(this).animate({left: "30%"},400);   
        checker = true;
    }
});

Use this

    var checker = new Boolean();
checker = true;

    $("#div").click(function () { 
        if (checker == true){
        $(this).animate({left: "10%"},400);   
        checker = false;
            }
        else
        {
            $(this).animate({left: "30%"},400);   
        checker = true;
        }
    });
var checker = true;

$("#div").on('click', function () {
    if (checker == true) {
        checker = false;
        $(this).animate({
            left: "10%"
        }, 400);

    } else {
        $(this).animate({
            left: "30%"
        }, 400);
        checker = true;
    }
});

Demo

Try this:

var checker = true;
$("#div").on('click', function () {
    var left = checker ? '10%' : '30%';
    $(this).animate({left: left},400);   
    checker = !checker
});
发布评论

评论列表(0)

  1. 暂无评论