javascript - 试图将重复的代码放在一个函数中,我想我在某个地方弄乱了字符串
问题描述
我建立了一个小小的呼吸应用程序http://cheel.me/但我知道 javascript 特别像意大利面条,而不是我应该如何做。
我想将所有重复的东西放入一个函数中,但我认为我插入动画持续时间的方式是错误的,但似乎无法解决。
这是原始的 Javascript 想要制作成更好的功能。
var breatheAnimation = document.getElementById("circleanim");
$('#5seconds').click(function () {
breatheAnimation.style.animation = "breathe 10s infinite ease both";
document.getElementById("in").style.animation = "in 10s infinite ease";
document.getElementById("out").style.animation = "out 10s infinite ease";
this.classList.add("active");
document.getElementById("6seconds").classList.remove("active");
document.getElementById("7seconds").classList.remove("active");
document.getElementById("8seconds").classList.remove("active");
});
$('#6seconds').click(function () {
breatheAnimation.style.animation = "breathe 12s infinite ease both";
document.getElementById("in").style.animation = "in 12s infinite ease";
document.getElementById("out").style.animation = "out 12s infinite ease";
this.classList.add("active");
document.getElementById("5seconds").classList.remove("active");
document.getElementById("7seconds").classList.remove("active");
document.getElementById("8seconds").classList.remove("active");
});
$('#7seconds').click(function () {
breatheAnimation.style.animation = "breathe 14s infinite ease both";
document.getElementById("in").style.animation = "in 14s infinite ease";
document.getElementById("out").style.animation = "out 14s infinite ease";
this.classList.add("active");
document.getElementById("5seconds").classList.remove("active");
document.getElementById("6seconds").classList.remove("active");
document.getElementById("8seconds").classList.remove("active");
});
$('#8seconds').click(function () {
document.getElementById("circleanim").style.animation = "breathe 16s infinite ease both";
document.getElementById("in").style.animation = "in 16s infinite ease";
document.getElementById("out").style.animation = "out 16s infinite ease";
this.classList.add("active");
document.getElementById("5seconds").classList.remove("active");
document.getElementById("7seconds").classList.remove("active");
document.getElementById("6seconds").classList.remove("active");
});
这就是我要去的地方,但我很确定我添加持续时间的方式是错误的,但我正在努力寻找正确的答案
var breatheAnimation = document.getElementById("circleanim");
function changeDuration(duration) {
breatheAnimation.style.animation = "breathe " . duration . "s infinite ease both";
document.getElementById("in").style.animation = "in " . duration . "s infinite ease";
document.getElementById("out").style.animation = "out " . duration . "s infinite ease";
this.classList.add("active");
};
$('#5seconds').click.changeDuration(10);
$('#6seconds').click.changeDuration(12);
$('#7seconds').click.changeDuration(14);
$('#8seconds').click.changeDuration(16);
我不断在控制台中收到“意外字符串”错误。我知道我没有添加循环或 if/else 语句来删除活动类。但我会做到的!
解决方案
使用 JavaScript 连接运算符+
代替.
(例如 PHP 的)
breatheAnimation.style.animation = "breathe " + duration + "s infinite ease both";
推荐阅读
- c# - WPF 绑定命令到 Datagrid 组标题中的复选框
- android - Android - AWS s3 谷歌登录
- angular - 使用 @Input 的 Angular 5 参数化组件
- c# - 使用 lambda 表达式在选择内选择
- matlab - 如何在matlab中结合步进频率和相位编码波形?
- idris - Idris:为长度为 Fin 而不是 Nat 的向量编写初始段函数
- php - php表单没有插入到sql数据库
- .htaccess - 为什么我收到 404 服务器错误 htaccess 重写规则?
- ruby-on-rails - Rails current_user ip 因未知原因返回 nil
- autohotkey - autohotkey:两个卷的比较 - 结果错误