javascript - 我在 javascript 中的函数不正确地重叠

标签 javascript html css time

出于某种原因,我的 javascript 的两个“部分”相互干扰。我知道是这种情况,因为如果删除变色部分,时钟会突然出现。预期的结果是时钟出现在颜色的前面。我无法确定为什么会这样。谢谢!

var div = document.getElementById("full");

function pad(n, width, z) {
  z = z || '0';
  n = n + '';
  return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}

function getclockColor() {
  var today = new Date();
  var h = String(today.getHours());
  var m = String(today.getMinutes());
  var s = String(today.getSeconds());
  var color = '#' + pad(h, 2) + pad(m, 2) + pad(s, 2);
  return color;
}

function changeColor() {
  div.style.backgroundColor = getclockColor();
}

setInterval(changeColor, 1000);

function clock() {
  var time = new Date(),

    hours = time.getHours(),

    minutes = time.getMinutes(),


    seconds = time.getSeconds();

  document.querySelectorAll('.clock')[0].innerHTML = harold(hours) + ":" + harold(minutes) + ":" + harold(seconds);

  function harold(standIn) {
    if (standIn < 10) {
      standIn = '0' + standIn
    }
    return standIn;
  }
}
setInterval(clock, 1000);
body {
  overflow: hidden;
  margin: 0;
  padding: 0;
}

#full {
  position: absolute;
  height: 100%;
  width: 100%;
}

.clock {
  font-size: 4em;
  z-index: 1;
  color: red;
}
<!Doctype html>
<html>

<head>
  <link rel="stylesheet" type="text/css" href="/Users/zanolon/Desktop/Color Clock/Clock.css">
</head>

<body>
  <div id="full"></div>
  <div class="clock"></div>
</body>
</html>

最佳答案

#full 元素的 z-index 设置为 -1 以将其推到时钟后面。

body {
  overflow: hidden;
  margin: 0;
  padding: 0;
}

#full {
  position: absolute;
  height: 100%;
  width: 100%;
    z-index: -1;
}
.clock {
  font-size: 4em;
  color: red;
}
<!Doctype html>
<html>

<head>
    <link rel="stylesheet" type="text/css" href="/Users/zanolon/Desktop/Color Clock/Clock.css">
</head>

<body>
    <div id="full"></div>
    <div class="clock"></div>
    <script type="text/javascript">
    var div = document.getElementById("full");

    function pad(n, width, z) {
        z = z || '0';
        n = n + '';
        return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
    }

    function getclockColor() {
        var today = new Date();
        var h = String(today.getHours());
        var m = String(today.getMinutes());
        var s = String(today.getSeconds());
        var color = '#' + pad(h, 2) + pad(m, 2) + pad(s, 2);
        return color;
    }

    function changeColor() {
        div.style.backgroundColor = getclockColor();
    }

    setInterval(changeColor, 1000);
    </script>
    <script type="text/javascript">
    function clock() {
        var time = new Date(),

            hours = time.getHours(),

            minutes = time.getMinutes(),


            seconds = time.getSeconds();

        document.querySelectorAll('.clock')[0].innerHTML = harold(hours) + ":" + harold(minutes) + ":" + harold(seconds);

        function harold(standIn) {
            if (standIn < 10) {
                standIn = '0' + standIn
            }
            return standIn;
        }
    }
    setInterval(clock, 1000);
    </script>
</body>

</html>

关于javascript - 我在 javascript 中的函数不正确地重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48427584/

相关文章:

python - Eclipse 中的 HTML/CSS/JS 语法高亮显示

ios - IOS Ipad mini 中的选择框选择问题

html - 删除由 CSS 引起的随机空白

html - 查询有关 Html &lt;input type="password> 标签...?

javascript - 如何替换数据?

javascript - 当遇到第一个字母 "x"时分割段落中的内容?

javascript - 具有 onclick 事件的 HTML anchor 链接仅有效一次

html - 位置固定且宽度 25% 未采用正确的宽度

javascript - Angular carousel js 有时加载有时不加载

javascript - 如何获取JS中嵌套Element.prototype函数中引用的元素?