简体   繁体   中英

How to make old text slide down as new text is added above it?

I am feeding text to a div from the top.

The following code gives the newest paragraph a slide-down animation as it is added to the div.

How can I make all paragraphs slide down as the new one is created? Also, any tips on my code will be greatly appreciated!

HTML:

<div id="text">

Javascript:

function addText(message) {
    var el = document.getElementById('text');
    var p = document.createElement('p');
    p.appendChild(document.createTextNode(message));
    el.insertBefore(p, el.childNodes[0] || null);
    $(p).addClass("reveal");
}

CSS:

.reveal {
    animation: slide-down 1s;
}

@keyframes slide-down {
  from {
    -moz-transform: translateY(-50px);
    -webkit-transform: translateY(-50px);
    transform: translateY(-50px);
    opacity: 0;
  }
  to {
    opacity: 1;
    -moz-transform: translateY(0px);
    -webkit-transform: translateY(0px);
    transform: translateY(0px);
    -moz-animation: linear;
    -webkit-animation: linear;
    animation: linear;
  }
}

Can you try this.

function addText(message) {
     $('p').removeClass("reveal");
     setTimeout(function(){ 
        var el = document.getElementById('text');
        var p = document.createElement('p');
        p.appendChild(document.createTextNode(message));
        el.insertBefore(p, el.childNodes[0] || null);
        $('p').addClass("reveal");
     }, 500);
}
addText('Welcome to first paragrap');

Then you call second one in console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM