Firing CSS Transition when performing ajax request

Asked

Viewed 241 times

0

I’m trying to do the following, before the element gets the answer ajax opacity from it to 0 and trigger the transition, until then this working beauty, the question is that after I pass the responseText for the element I need to display the element again and trigger the transition, but I’m not able to make it work...

I’ve already done it with the .show and .hide jquery’s most wanted to do just by activating css

document.getElementById( this.element ).addEventListener(
            "transitionend", 
            function(){
                document.getElementById( this.element ).innerHTML = this.xmlhttp.responseText;
                document.getElementById( this.element ).style.opacity = 1;
            }, true);
document.getElementById( this.element ).style.opacity = 0;

css of the element

-webkit-transition: 200ms linear all;
-moz-transition: 200ms linear all;
transition: 200ms linear all;

// The problem is with the this that is returning Undefined, but do not know how to solve s;

  • But you’re using the "transitionend" event, not the Ajax callback! Maybe the element is being displayed, only without content (empty innerHTML). Experiment with innerHTML = '<b>Oi, Mundo!</b>' just to test.

  • already tested it is not changing the innerHTML

2 answers

1


The problem is that the this within the function Event Handler no longer corresponds to this of the execution context outside it. Try it like this:

var self = this;
document.getElementById( self.element ).addEventListener(
            "transitionend", 
            function(){
                document.getElementById( self.element ).innerHTML = self.xmlhttp.responseText;
                document.getElementById( self.element ).style.opacity = 1;
            }, true);
document.getElementById( self.element ).style.opacity = 0;

Another version, improved:

var node = document.getElementById( this.element ),
    xhr  = this.xmlhttp;

node.addEventListener(
    "transitionend", 
    function() {
        if (node.style.opacity != 0) return;
        node.innerHTML = xhr.responseText;
        node.style.opacity = 1;
    }, true);
);

node.style.opacity = 0;
  • So if I do just : opacity:0 element=Sponse and opacity:1 works normal more if I put inside the Event Listener it does not run, if I play only an Alert will more if I put the code does not work

  • Tried with setTimeout? Place console.log to check whether self.xmlhttp.responseText contains something and if self.element is really pointing to the right element.

  • already tested with setTimeout, it’s giving this error on the console for both when I use this and self -> Cannot read Property 'responseText' of Undefined

  • is really that question of this and self pq if I play straight there the name of the element it works ;s

  • Try the new code, where we set the variable self in the correct place (see the amended answer). Perhaps this is not necessary setTimeout.

  • I changed the answer; please check the new solution.

  • Thanks @J. Bruni I had just achieved more by assigning the values of the variable to others outside the Systener, but another problem appeared, the Event Listener is running twice because of the two opacity changes... some idea of how to solve ?

  • I made an if to only run when the opacity is 0 vlw

  • If you can edit your answer to be right add if ai -> if ( Document.getElementById( self.element ).style.opacity == 0 ) {

  • Cool. See the "new improved version".

Show 5 more comments

0

I think with fadein() and fadeOut() or maybe also with Animate() can help you.

  • 1

    then brother as I spoke up there I already managed to do this with jquery, wanted to do without, let possible only activate the Transitions

Browser other questions tagged

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