문제

I'm trying to create a toggle when the user clicks/taps an icon to slide the #wrapper left 200px and then and then back once its clicked/tapped again. This is the code I am trying to work with:

$('a.navToggle').click(function () {
    $('#wrapper').toggle(function () {
        $('this').animate({
            right: '200px'
        }, 900);
    },
    function () {
        $('this').animate({
            left: '200px'
        }, 900);
    });
});

The problem I'm getting is the #wrapper slides of screen completely and not just 200px

도움이 되었습니까?

해결책

Is that what you want?

$('a.navToggle').click(function () {
    if($("#wrapper").css("left") == "-200px")
    {  
        $("#wrapper").animate({
            left: '0'
        }, 900);
    }
    else{
        $("#wrapper").animate({
            left: '-200px'
        }, 900);
    }
});

다른 팁

.toggle(function(){},function(){}) is Deprecated and Removed in version 1.9

You must using greater version than that.

and using .toggle() means to hide and show alternatively.

Display or hide the matched elements.

That's why your code is hiding element with id wrapper and one click again showing it.


It is this not 'this' without quotes.


Fiddle Demo

var i = 0,
    wrapper = $('#wrapper');
$('a.navToggle').click(function () {
    if (i++ % 2 == 0) {
        wrapper.removeAttr('style').animate({
            right: '200px'
        }, 900);
    } else {
        wrapper.removeAttr('style').animate({
            left: '200px'
        }, 900);
    }
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top