简体   繁体   English

如何添加动画消失的文本,而不添加新行

[英]How to add animated disappearing text, without adding new line

I am trying to add pop-up messages, that confirms various actions, by showing up moving a bit to top, and then disappear.我正在尝试添加弹出消息,通过显示向顶部移动一点,然后消失来确认各种操作。 I used CSS3 animations to achieve that:我使用 CSS3 动画来实现:

#message-boxOK {
width: 200px;
background-color: #80b95c;
position: relative;
float: middle;
padding: 10px 10px;
text-align: center;
border-radius: 5px;
margin: 0 auto;
-webkit-animation-name: rise; /* Chrome, Safari, Opera */
-webkit-animation-duration: 2s; /* Chrome, Safari, Opera */
animation-name: rise;
animation-duration: 2s;
}
/* Standard syntax */
@keyframes rise {
0%   {bottom:25px;}
100% {bottom:100px;}
}

And I used JavaScirpt to start these messages:我使用 JavaScirpt 来启动这些消息:

function messageBox(message,type) {
if(type=="ok"){
    document.getElementById("dump-message").innerHTML ="<div id='message-  boxOK'>"+message+"</div>";
    setTimeout("document.getElementById('message-boxOK').remove()",1900);
}
else {
    document.getElementById("dump-message").innerHTML ="<div id='message-box'>"+message+"</div>";
    setTimeout("document.getElementById('message-box').remove()",1900);
}

}` }`

But when JS adds a new div with message, a new line appears in document and other content moves.但是当JS添加一个带有消息的新div时,文档中会出现一个新行并且其他内容移动。 When it is removed again line disappears and other content moves.当它再次被删除时,行消失,其他内容移动。

So how could I make appear this animation on top off other content without it moving.那么我怎么能在不移动的情况下让这个动画出现在其他内容之上。 I need that message somewhere in the middle of document.我需要在文档中间某处的消息。 Or maybe there some kind off other way or plug-in to display temporary pop-ups.或者也许有某种其他方式或插件来显示临时弹出窗口。

position: absolute will help here. position: absolute在这里会有所帮助。

Absolute positioning takes the element completely out of the flow of the document therefore not affecting other content when your message appears and disappears.绝对定位使元素完全脱离文档流,因此当您的消息出现和消失时不会影响其他内容。 In contrast, floated elements nonetheless are still part of the document flow, which results in the shifting of content that you see on appearance/disappearance.相比之下,浮动元素仍然是文档流的一部分,这会导致您在出现/消失时看到的内容发生变化。

See the example below:请参阅下面的示例:

 function messageBox(message, type) { if (type == "ok") { document.getElementById("dump-message").innerHTML = "<div id='message-boxOK'>" + message + "</div>"; setTimeout("document.getElementById('message-boxOK').remove()", 1900); } else { document.getElementById("dump-message").innerHTML = "<div id='message-box'>" + message + "</div>"; setTimeout("document.getElementById('message-box').remove()", 1900); } } messageBox('Passing by!', 'ok');
 *, :before, :after { box-sizing: border-box; margin: 0; padding: 0; } .bg-top { background: red; width: 100%; height: 50px; } .bg-bottom { background: blue; width: 100%; height: 50px; } #dump-message { margin-top: 100px; } #message-boxOK { width: 200px; background-color: #80b95c; position: absolute; left: 50%; margin-left: -100px; padding: 10px; text-align: center; border-radius: 5px; -webkit-animation-name: rise; /* Chrome, Safari, Opera */ -webkit-animation-duration: 2s; /* Chrome, Safari, Opera */ animation-name: rise; animation-duration: 2s; } /* Standard syntax */ @keyframes rise { 0% { bottom: 25px; } 100% { bottom: 100px; } }
 <div class='bg-top'></div> <div id='dump-message'></div> <div class='bg-bottom'></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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