首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在带有JavaScript的超文本标记语言中使用闪烁元素

在带有JavaScript的超文本标记语言中使用闪烁元素
EN

Stack Overflow用户
提问于 2018-08-24 06:42:45
回答 4查看 76关注 0票数 2

因此,我希望我的登录门户页面的标题后有一个闪烁的下划线。所有的超文本标记语言都很好,它在JavaScript中的样式改变之间得到了一种延迟。我尝试过这样做:

代码语言:javascript
复制
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;
}

但这显然是行不通的。因此,当我有点困惑的时候,我向社区寻求想法。Image of what I'm trying to achieve

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-08-24 06:48:01

考虑使用setInterval而不是setTimeout,这样就可以避免循环。然后,您可以执行以下操作:

代码语言:javascript
复制
setInterval(function() {
    underscore.style.display = underscore.style.display == 'none' ? 'block' : 'none';
}, 1000);
票数 1
EN

Stack Overflow用户

发布于 2018-08-24 06:47:44

对当前状态使用window.setInterval和布尔值

代码语言:javascript
复制
const underscore = document.getElementById('flashingUnderscore');
var displayed = true;
function flash() {
  if(displayed) {
    underscore.style.display = 'none';
  } else {
    underscore.style.display = 'block';
  }
  displayed = !displayed;
}
window.setInterval(flash, 1000);
代码语言:javascript
复制
<div id="flashingUnderscore">_</div>

票数 0
EN

Stack Overflow用户

发布于 2018-08-24 06:49:28

简单

代码语言:javascript
复制
window.setInterval(function(){
  var yourDiv = document.getElementById('flashingUnderscore');
  if(yourDiv.style.visibility == "visible")
        yourDiv.style.visibility == "hidden")
  else
        yourDiv.style.visibility == "visible")
}, 1000);

其他解决方案会更改显示值,但是,如果它占用空间,则可能会更改布局。可见性只会隐藏它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51995233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档