Home > Web Front-end > JS Tutorial > Javascript implements background color that changes at any time_javascript skills

Javascript implements background color that changes at any time_javascript skills

WBOY
Release: 2016-05-16 16:06:05
Original
1159 people have browsed it

A very simple piece of code that changes the background color all the time

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    #dv {width: 100px;height: 100px;}
  </style>
</head>
<script>
  window.onload=function(){
    var oDv = document.getElementById('dv');
      oDv.style.backgroundColor='red'
    setInterval(function(){
      var arrRandNumber = [
          getRandomNumber(0,255),
          getRandomNumber(0,255),
          getRandomNumber(0,255)   
        ];
 
      oDv.style.backgroundColor='rgb('+arrRandNumber[0]+','+arrRandNumber[1]+','+arrRandNumber[2]+')'
         
        function getRandomNumber(rMin,rMax){
          var cha = rMax-rMin;
          var rand = Math.random();
          //return (rMin+Math.round(cha*rand))
        }
       
    },500)
     
    //alert(getRandomNumber(0,255))
  };
</script>
<body>
  <div id="dv"></div>
</body>
</html>
Copy after login

The above is all the content shared with you in this article. I hope it will be helpful to everyone in learning to use javascript.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template