使用JavaScript在HTML中具有闪烁元素
因此,我希望我的登录门户页面在标题后有一个闪烁的下划线。所有的HTML都很好,在JavaScript中更改样式之间有一种形式的延迟。我尝试过这样做:使用JavaScript在HTML中具有闪烁元素,javascript,Javascript,因此,我希望我的登录门户页面在标题后有一个闪烁的下划线。所有的HTML都很好,在JavaScript中更改样式之间有一种形式的延迟。我尝试过这样做: const underscore = document.getElementById('flashingUnderscore'); function flash() { while (true) { underscore.style.display = 'none'; window.setTimeout(du
const underscore = document.getElementById('flashingUnderscore');
function flash() {
while (true) {
underscore.style.display = 'none';
window.setTimeout(dump,1000);
underscore.style.display = 'block';
window.setTimeout(dump,1000);
};
};
function dump() {
return;
}
但这显然行不通。所以我向社区寻求想法,因为我有点被难住了 使用
window.setInterval
和当前状态的布尔值
const下划线=document.getElementById('flashingUnderscore');
显示的var=真;
函数flash(){
如果(显示){
下划线.style.display='none';
}否则{
下划线.style.display='block';
}
显示=!显示;
}
设置间隔(闪光,1000)代码>
<代码> < <代码> > p>而不是<代码> SETTIMEOUT,请考虑使用<代码> SETIFATION>代码>,这样您就可以避免循环。然后,您可以执行以下操作:
setInterval(function() {
underscore.style.display = underscore.style.display == 'none' ? 'block' : 'none';
}, 1000);
轻松的
其他解决方案会更改显示值,但如果布局占用空间,则可能会更改布局。可见性只会将其隐藏。您可以将css用作动画:
@关键帧闪烁{
0% {
不透明度:0;
}
100% {
不透明度:1;
}
}
#闪现得分{
颜色:红色;
显示:内联块;
动画:闪烁。3s无限;
/*动画名称:闪烁;
动画持续时间:2秒*/
}
您好
没有您的HTML
代码,我们无法为您提供合适的解决方案。请通过添加等效的HTML
编辑您的帖子。你可以只使用<代码> CSS 来实现你的目标。你可能想考虑使用CSS动画而不是无限循环。例如,如果您想使用JavaScript,您可能会发现很有趣。这会引发一个语法错误:无效或意外的令牌编辑:nvm,在第2行末尾,在结束冒号之前缺少一个“
。我的错误。我修改了答案以处理语法错误:)
window.setInterval(function(){
var yourDiv = document.getElementById('flashingUnderscore');
if(yourDiv.style.visibility == "visible")
yourDiv.style.visibility == "hidden")
else
yourDiv.style.visibility == "visible")
}, 1000);