javascript - How can I ensure this CSS transition animates -
i have image carousel built using knockout & jquery. animate slides want use css transitions , not jquery animate. have working have issue. in code below, 'slideright' part doesn't work, else part works fine. what's happening transition margin-left 0 being skipped, though transition class has been added.
if (slideright) { $(requestedelement).insertbefore(currentelement); slidecontainer.css('margin-left', -$(self.carousel).width()); slidecontainer.addclass('transition'); slidecontainer.css('margin-left', 0); } else { $(requestedelement).insertafter(currentelement); slidecontainer.addclass('transition'); slidecontainer.css('margin-left', -$(self.carousel).width()); }
i've created jsfiddle here: http://jsfiddle.net/vnw57nx0/2/
as you'll see in fiddle, carousel transitions nicely between slides going right left.
but if find line in javascript:
self.setindex(self.currentindex() + 1);
and change to:
self.setindex(self.currentindex() - 1);
the slides should cycle in reverse. do, they're not being animated. interestingly, if debug script , step through works fine. made me think timing issue , maybe need use callback function jquery .insertbefore, .css , .addclass synchronous.
any ideas how can fix code works if debug doesn't if don't?
it seems browser doesn't make transition when revert style value within same context. need in new context using settimeout
:
if (slideright) { $(requestedelement).insertbefore(currentelement); slidecontainer.css('margin-left', -$(self.carousel).width()); settimeout(function() { slidecontainer.addclass('transition'); slidecontainer.css('margin-left', 0); }); } else {
http://jsfiddle.net/vnw57nx0/3/
i found question because of knockout tag, , although you've got knockout references in question, problem has nothing knockout. in fact, code anti-knockout since you're using jquery selectors within "view model" , using observables none needed or useful.
Comments
Post a Comment