0

I have a button:

<div id="headBtn"> </div> 

I have a class:

.slideout { -webkit-transform: translate3d(265px, 0, 0); position:absolute; left:0; transition: -webkit-transform .5s ease; } 

I have a function:

$('#headBtn').on('click',function(){ var wrap = $('#wrapper'); wrap.toggleClass('slideout'); }); 

The aim is to have my #wrapper element move to the right using webkit-tansform, I then want to toggle that css so it snaps back to the left again.

Right now the #wrapper element does snap back to the left for a second but it moves to the right again, as if the css is removed but then applied again.

Any ideas why this is happening?

5
  • 1
    I can't reproduce the problem. It does snap back left and stay there: jsfiddle.net/8HmEt Where is your javascript block-located maybe that is the issue? Commented Jan 30, 2014 at 13:27
  • Thanks @Esa I thought I was just going crazy, so my problem is elsewhere. Why would the position of my block matter? Commented Jan 30, 2014 at 13:31
  • 1
    @UzumakiDev You will need to post more of your code, because with the limited code you gave us, what you described isn't happening. Commented Jan 30, 2014 at 13:33
  • 1
    If your code is run multiple times, there would be many click-handlers, which could explain the problem. Commented Jan 30, 2014 at 13:36
  • Ohhhh, wait, fixed it, no worries, I'm just stupid. I had another function using hammer.js that had on('tap',function(){ $(this).removeClass('slideout'); }); this was on the wrapper element itself. SOrry for wasting your time. But you did confirm it wasn't the toggle function itself. Commented Jan 30, 2014 at 13:36

1 Answer 1

1

It's because when you are removing the class, you are removing the CSS transition defined in it. If you apply that transition to all elements, it still works when you remove the class:

.slideout { -webkit-transform: translate3d(265px, 0, 0); position:absolute; left:0; } * { transition: -webkit-transform .5s ease; } 

http://jsfiddle.net/6Eq4B/

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

2 Comments

Hmm he said the problem was that it snaps to the left and then moves to the right again. This doesn't answer that problem. Though I didn't experience that problem myself when I tested the original code.
I'm not sure what you are saying?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.