Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

javascript - How can I put one number in front of another in the style of an LCD clock?

I am generating a number in the style of lcd clocks.

enter image description here

I would like to know how I can put one number in front of another. for example I would like to show 123.

enter image description here

but I do not know how to do it. thank you very much.

 <div id="preformatted">

 </div>

 <div class="number-entry-pane">
 <label>Enter a digit:
 <input type="number" id="numInput" min=0 max=9 /></label>

  // Save references to my two control elements.
  var myPre = document.getElementById("preformatted");
  var numInput = document.getElementById("numInput");

  // Create an array of the numbers 0 through 9 as 7 segment digits.
  var numberStrings = [
   " __ <br/>|  |<br/>|__|<br/> ",
   "    <br/>   |<br/>   |<br/>",
   " __ <br/> __|<br/>|__ <br/>",
   " __ <br/> __|<br/> __|<br/>",
   "    <br/>|__|<br/>   |<br/>",
   " __ <br/>|__ <br/> __|<br/>",
   " __ <br/>|__ <br/>|__|<br/>",
   " __ <br/>   |<br/>   |<br/>",
   " __ <br/>|__|<br/>|__|<br/>",
   " __ <br/>|__|<br/> __|<br/>"];

  console.log(numberStrings)
  // Attach the listeners for the input changes.
  numInput.addEventListener("keyup", changeNumbers);
  numInput.addEventListener("change", changeNumbers);

  function changeNumbers(){
    // Simply use the element from the array associated with
    //  the entered number to update the preformatted display.
    myPre.innerHTML = numberStrings[numInput.value];
  }

https://jsfiddle.net/5e1dnt8m/

My idea is not to use several divs.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this solution, it will work. I have removed the max limit on the input for testing with bigger numbers.

// Save references to my two control elements.
var myPre = document.getElementById("preformatted");
var numInput = document.getElementById("numInput");

// Create an array of the numbers 0 through 9 as 7 segment digits.
var numberStrings = [
 " __ <br/>|  |<br/>|__|<br/> ",
 "    <br/>   |<br/>   |<br/>",
 " __ <br/> __|<br/>|__ <br/>",
 " __ <br/> __|<br/> __|<br/>",
 "    <br/>|__|<br/>   |<br/>",
 " __ <br/>|__ <br/> __|<br/>",
 " __ <br/>|__ <br/>|__|<br/>",
 " __ <br/>   |<br/>   |<br/>",
 " __ <br/>|__|<br/>|__|<br/>",
 " __ <br/>|__|<br/> __|<br/>"];

// Attach the listeners for the input changes.
numInput.addEventListener("keyup", changeNumbers);
numInput.addEventListener("change", changeNumbers);

function changeNumbers(){
  // Simply use the element from the array associated with
  //  the entered number to update the preformatted display.
  var digits = numInput.value.split('');
  var line=['','',''];
  for (i in digits) {
  	var numString = numberStrings[digits[i]].split('<br/>');
    line[0] += numString[0];
    line[1] += numString[1];
    line[2] += numString[2];
  }
  myPre.innerHTML = line.join('<br/>');
}
#preformatted {
    font-family: monospace;
    white-space: pre;
    padding: 5px;
    margin: 5px;
    border: 1px dotted red;
    width: 500px;
    height: 50px;
    position: relative;
    text-align: center;
}
label {
  display: block;
}
<div id="preformatted"></div>

<div class="number-entry-pane">
  <label>Enter a digit:
<input type="number" id="numInput" min=0  />
</label>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...