javascript - 如何用 JS 播放动画
问题描述
我在按钮悬停时编码淡入和淡出文本......我必须调用动画悬停......我该怎么做?
const button = document.getElementById("btn");
const disp_text = document.getElementById('disp_text');
button.onmouseover = function(){
//Here goes the animation to play
disp_text.innerText = "Next";
}
我试过了:
const button = document.getElementById("btn");
const disp_text = document.getElementById('disp_text');
button.onmouseover = function(){
animation.Play("fadein");
disp_text.innerText = "Next";
}
但没什么...
如果有人可以提供帮助,我将不胜感激...
解决方案
下面是一些使用 javascript 在按钮悬停时为淡入淡出动画的代码。我还实现了一个纯 CSS 版本。我正要使用 Animate API 实现一个版本,但我看到 @DEEPAK 已经做到了,所以这是第三种选择。
const button = document.getElementById("btn");
const disp_text = document.getElementById('disp_text');
button.onmouseover = function(){
disp_text.classList.add('button-hover');
}
button.onmouseout = function(){
disp_text.classList.remove('button-hover');
}
#disp_text {
opacity:0;
transition: opacity .25s ease-in-out;
}
#disp_text.button-hover {
opacity:1;
}
#disp_text2 {
opacity:0;
transition: opacity .25s ease-in-out;
}
#btn2:hover #disp_text2 {
opacity:1;
}
<button id='btn'>Hover over this to see the animation of the DIV below</button>
<p id='disp_text'>Next</p>
<p>The one below uses css only - no javascript. This is easy because the span is inside the button</p>
<button id='btn2'><span id='disp_text2'>Next</span></button>
推荐阅读
- c# - 如何在 C# 中设置版本名称?
- php - Jquery AJAX 调用不起作用并且总是进入错误函数
- node.js - 具有锐利调整大小图像的 multer(上传前 - 使用 toBuffer)
- docker - HAProxy boshrelease + Docker boshrelease 链接
- sql-server-2008 - 在 SQL Server 中拆分单词
- scala - 减少我在 scala 中处理的行数
- api - 在超级账本作曲家中创建资产资产
- reactjs - setState 没有在 componentWillReceiveProps 内更新
- css - 部署heroku后将引导css规则应用于自定义规则?
- html - 在 Ruby 中将 CSV 转换为 HTML 表格