Problem with Jquery UI Slide Left

Asked

Viewed 257 times

4

I’m developing a project and I needed to know how to slide a div to the left. I am currently using:

$('#idDiv').toggle('slide', { direction: 'left', distance: 450 });

I wanted mine to div of 500px width slide and let 50px on display. The problem is that when she has just given the slide of 450px she disappears. I know the toggle has to do with the hide and show, but is there another way (without using the animate to give resize) to give slide to the left without this disappears?

Here’s a example than I said.

3 answers

3

The method .toggle() makes the div appear/disappear, it is better to use the method .animate() and encourage the margin-left for example. The width (width) could also be an alternative.

So in order to close and open again you can use one flag. A flag is a useful variable for knowing the state of the element.

In this case I use it to store information about whether it is open or closed. This flag in the first example is a global variable, in the second example is stored inside a field data of the element itself.

So every click he will check with a ternary operator if the flag open has the value true or false and as it returns "-250px" (case open for true) or "0px" (case open for false):

var open = true;
$(document).click(function () {
  $("#toggle").animate({
    'margin-left': open ? "-250px" : "0px"
  }, 500);
  open = !open;  // mudar a flag para o oposto do que estava usando o operador de negação "!"
});

Example


Another Alternative is to have a date field in the element that stores this flag about whether it is open or not:

HTML

<div id="toggle" data-open="true"></div>

JS

$(document).click(function () {
  var elemento = $("#toggle");
  elemento.animate({
    'margin-left': elemento.data('open') ? "-250px" : "0px"
  }, 500);

  // mudar a flag para o oposto do que estava usando o operador de negação "!"
  elemento.data('open', !elemento.data('open')); 
});

Example


The third example (and maybe better) is using a CSS class and transitions:

CSS

    #toggle {
        width: 300px;
        height:300px;
        background: #ccc;
        -webkit-transition: margin-left 500ms ease;
        -moz-transition: margin-left 500ms ease;
        -ms-transition: margin-left 500ms ease;
        -o-transition: margin-left 500ms ease;
        transition: margin-left 500ms ease;
    }
    .toggle {
        margin-left: -250px
    }

JS

    $(document).click(function() {
        var $toggle = $("#toggle");
        if ($toggle.hasClass('toggle')) $toggle.removeClass('toggle');
        else $toggle.addClass('toggle');
    });

Example

  • Thanks, I think I’ll use this tip and I’ll use an if to give back the initial margin

  • @blackblather, joined example to open again too

  • The only problem is that I don’t read too much into the way you used to solve the problem and since I have to defend my project, I might have the misfortune to be asked about it and then I don’t know how to explain it properly, Otherwise, I’m more comfortable if you ask me something. But anyway thank you :)

  • @blackblather, joined explanation and one more example / variant

  • Thanks for the explanation, it’s always good to learn new things :)

  • 2

    @blackblather, I added another variant :P

  • @Zuul thank you :)

Show 2 more comments

1

I’m not really into jQuery and I need to explain the code when doing my project presentation (PAP) and the way I managed to solve the problem was:

$(" #sideBarE ").click(function(e)
        {
            var divE = $('#empresas');
            var marginE = divE.css('margin-left');
            //alert(marginE);
            if (marginE == "0px")
                $('#empresas').animate({'margin-left':"-400px"}, 1000);
            else
                $('#empresas').animate({'margin-left':"0px"}, 1000);
            e.stopPropagation();
        });
  • When using the method .stopPropagation() are preventing the spread and consequent warning of what just happened in the element with the identifier sideBarE for its parent elements. I say this because I don’t know if you really need this method there. (Since you refer that you will have to explain the code, evaluate the necessity of using this method) ;)

  • I know, that was the point since I have 2 more Ivs, each with different effects, if I take that method, this gets a splash ahahah

0

You know you can do this with the translate in transitions and animations of css ??

  • I know but I need to use "technologies" that I didn’t learn in school, so I opted for jquery and bootstrap

Browser other questions tagged

You are not signed in. Login or sign up in order to post.