javascript - 如何使文本淡入淡出并上升

标签 javascript

如何制作具有特定 id 的 div、淡入淡出和上升? 这是一些伪代码:

y=400
opacity=0

while (y > 100) {
document.getElementById('text').style.position = y
document.getElementById('text').style.opacity = opacity

y--
opacity-=0.01
}

最佳答案

来 self 的评论

I would actually use css to handle the animations because you should let the native implementation do the hard work (it's faster too). CSS-Tricks is a good reference. You could also use something like animate.css if you want to use a css framework.

编辑片段:见下文

给你。使用animate.css 。够简单吗?

删除infinite以表示非无限。查看animate css github for more info

<小时/>

Animate.css 是一个 css 框架,这意味着它们为您提供 css 样式表,您可以通过添加类来应用它们。在底层,animate.css 使用 CSS 关键帧。查看 fadeOutUp here 的源代码(CSS 实现) .

<小时/>

来自您的评论:

How would I make the text animate when I click a button?

添加EventListener单击以使用 classList 添加类

(再次编辑片段)

<小时/>

Quick question, how do I make it so the button will work more than once? Because right now you can only click it once and then it won't work anymore.

在动画结束时添加一个事件监听器,然后从元素中删除类。

<link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css" rel="stylesheet"/>

<h1 class="thing-to-fade">Animate.css</h1>
<button id="my-button">Fade Out Up</button>
<script>
    const element = document.querySelector('.thing-to-fade'); // define the element
    document.querySelector('#my-button').addEventListener('click', event => {
        element.classList.add('animated',  'fadeOutUp');
    });
    
    // add an eventlistener for the 'animationend' event
    element.addEventListener('animationend', event => {
        // after the animation ends, these classes will be removed
        element.classList.remove('animated',  'fadeOutUp');
    });
</script>

关于javascript - 如何使文本淡入淡出并上升,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42325242/

相关文章:

javascript - 防止为未打开的扩展面板呈现内部 html

javascript - 对每个元素应用自定义 ng-attr ng-repeat

javascript - 停止 Angular 2 中旋转数的解决方案

javascript - Vue.js/webpack : how do I clear out old bundle main-*. js 文件,当热重载转译它们时?

javascript - 使用 Owl Carousel Url Hash Navigation 更改按钮样式

javascript - ExtJS 中的 afterLayout 与布局、afterRender 与渲染等?

java - 使用其键和值(Javascript 或 jQuery)从嵌套 json 中删除对象

javascript - 如何使用vue动态添加before after css代码?

javascript - Node.js,向google api发出https post请求

javascript - 在禁用浏览器 Flash 插件的情况下运行 Karma 测试