Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigering a CSS3 Transition with adding a class with Jquery

Tags:

jquery

css

At the moment I have a running fiddle that I am trying to get a css3 opacity transition to fire off when I add a class to it. The basic set up is that I click a button and then through jquery I add a div to the dom and after I add that element to the dom I then add a class to it. The adding class to that new dom element is suppose to kick off my transition but it is not, the only thing that is happening is that the element is being showed but the opacity transition isnt working. Any help will be much appreciated, I am leaving a link to the fiddle here fiddle link .And yes I know i could do the fade in with just jquery, but this is just a experiment , that would probably be used to kick off other css transition other then opacity

like image 667
Lawrence Avatar asked Oct 10 '11 15:10

Lawrence


People also ask

How do you trigger transition CSS?

Triggering transitions You can trigger CSS transitions directly with pseudo classes like :hover (activates when the mouse goes over an element), :focus (activates when a user tabs onto an element, or when a user clicks into an input element), or :active (activates when user clicks on the element).

What is add class in jQuery?

The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.

What are CSS3 transitions and transform?

The Transform property in CSS moves or modifies the appearance of an element, whereas the Transition property seamlessly and gently transitions the element from one state to another.


1 Answers

I have encountered this problem before, the only workaround I've found is adding a setTimeout to let the DOM notice there's a new element. It can be zero ms and it will still work:

$('button').live('click', function() {
    $(this).after("<div class='fade'>This is just a test</div>")
    setTimeout(function(){$(".fade").addClass("in");}, 0)
});

http://jsfiddle.net/tfmFx/

like image 53
methodofaction Avatar answered Nov 15 '22 04:11

methodofaction