0

I am trying a left bar hide/show with transition. Here's my code:

JQuery:

$("[data-toggle='offcanvas']").click(function(e) {
    e.preventDefault();
    $('.left-side').toggleClass("collapse-left");
});

CSS:

.left-side.collapse-left {
    left: -220px;
    -webkit-transition: all 1s ease-in-out;
    -moz-transition: all 1s ease-in-out;
    -ms-transition: all 1s ease-in-out;
    -o-transition: all 1s ease-in-out;
    transition: all 1s ease-in-out;
}

HTML:

<aside class="left-side sidebar-offcanvas">
    <!-- my content-->
</aside>
4
  • then what is problem in code? Commented Nov 21, 2014 at 9:25
  • A reduced test case as a code snippet or a fiddle, perhaps? The code you've shown so far should work. However, I would suggest using translate so that you can potentially offload the rendering to GPU, and will not cause massive repainting. Commented Nov 21, 2014 at 9:25
  • may be your not added [data-toggle='offcanvas'] to aside tag Commented Nov 21, 2014 at 9:30
  • tried to make a jsfiddle: jsfiddle.net/99o4k67f Commented Nov 21, 2014 at 9:34

2 Answers 2

2

First of all, the css left property has no effect on elements in normal flow. You should position the element by setting a position property value other than static so that left takes effect.

Secondly, the transition happens from one value to another. So if you expect to see a transition, set a default value, for example left:0.

Finally, If you expect the element to animate when a class it removed, you should not add the transition property in the class being toggled, Instead set it using a static selector.


$("#click").click(function(e) {
  e.preventDefault();
  $('.left-side').toggleClass("collapse-left");
});
.left-side {
  position: relative;
  left: 0;
  -webkit-transition: all 1s ease-in-out;
  -moz-transition: all 1s ease-in-out;
  -ms-transition: all 1s ease-in-out;
  -o-transition: all 1s ease-in-out;
  transition: all 1s ease-in-out;
}
.left-side.collapse-left {
  left: -220px;
}
div {
  min-height: 200px;
  max-width: 200px;
  background: #06F;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="click">click</button>
<aside class="left-side sidebar-offcanvas">
  <div>This is test</div>
</aside>

Sign up to request clarification or add additional context in comments.

Comments

0

You mean like this?

http://jsfiddle.net/99o4k67f/1/

$("#click").click(function(e) {
    e.preventDefault();
    $('.left-side').toggle('slow');
});

Or more like this:

http://jsfiddle.net/99o4k67f/2/

$("#click").click(function(e) {
    e.preventDefault();
    $('.left-side').animate({width:'toggle'},900);
});

Btw, you don't need any css for this :)

2 Comments

yah, but only collapse to left. not to top left corner
Lol @MamunSardar look at second fiddle ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.