Home >Web Front-end >JS Tutorial >Create picture clock effects with jquery_jquery
1. Generate digital clock
<script src="http://code.jquery.com/jquery-latest.js"></script> <script> window.onload=function(){ var oBody=document.body; var oP=document.getElementById("time"); setInterval(fnTime,1000); fnTime(); function fnTime(){ var myTime=new Date(); var iHours=myTime.getHours(); var iMin=myTime.getMinutes(); var iSec=myTime.getSeconds(); var str=toTwo(iHours)+toTwo(iMin)+toTwo(iSec); oP.innerHTML=str; } } /*将数字转换为字符串且一位数显示为两位,*/ function toTwo(n){ return n<10?'0'+n:''+n; } </script> <p id="time"></p>
Effect:
2. Convert numbers into pictures
Method 1:
The picture name is a number, written in the simplest way.
Pictures used:
Write a function strToImg(str) to convert each number in a string str into the corresponding image, and then dynamically create the a1f02c36ba31691bcfe87b2722de723b tag.
Note: The content in the e388a4556c0f65e1904146cc1a846bee tag needs to be cleared each time it is called.
<script src="http://code.jquery.com/jquery-latest.js"></script> <script> window.onload=function(){ var oBody=document.body; var oP=document.getElementById("time"); setInterval(fnTime,1000); fnTime(); function fnTime(){ var myTime=new Date(); var iHours=myTime.getHours(); var iMin=myTime.getMinutes(); var iSec=myTime.getSeconds(); var str=toTwo(iHours)+toTwo(iMin)+toTwo(iSec); //oP.innerHTML=str; strToImg(str); } } /*将数字转换为字符串且一位数显示为两位,*/ function toTwo(n){ return n<10?'0'+n:''+n; } function strToImg(str){ var str=str; $("#time").empty(); for(var i=0;i<str.length;i++){ var oImg=$("<img />"); oImg.attr("src","images/"+str.charAt(i)+".png"); $("#time").append(oImg); } } </script> <p id="time"></p>
Method 2: [Not applicable]
If the image name is not a pure number, save it in an array.
This method operates too many DOMs and is very inefficient. Sometimes the 6 nodes are not fully displayed.
Because my original intention was to check the information and saw that writing like this involves image preloading, and I thought it could speed up the efficiency. I tried it, but now it seems that I still don’t understand the principle of preloading, leaving a pitfall.
<script src="http://code.jquery.com/jquery-latest.js"></script> <script> window.onload=function(){ var oBody=document.body; var oP=document.getElementById("time"); setInterval(fnTime,1000); fnTime(); } function fnTime(){ var myTime=new Date(); var iHours=myTime.getHours(); var iMin=myTime.getMinutes(); var iSec=myTime.getSeconds(); var str=toTwo(iHours)+toTwo(iMin)+toTwo(iSec); //oP.innerHTML=str; strToImg(str); } /*将数字转换为字符串且一位数显示为两位,*/ function toTwo(n){ return n<10?'0'+n:''+n; } function strToImg(str){ var str=str; var imageArray=[]; for(i=0;i<11;i++){ imageArray[i]=new Image(); } //将个图像定义给相应的数组元素,使数组元素下标与图像所对应的数字字符一一对应 imageArray[0].src="images/0.png"; imageArray[1].src="images/1.png"; imageArray[2].src="images/2.png"; imageArray[3].src="images/3.png"; imageArray[4].src="images/4.png"; imageArray[5].src="images/5.png"; imageArray[6].src="images/6.png"; imageArray[7].src="images/7.png"; imageArray[8].src="images/8.png"; imageArray[9].src="images/9.png"; imageArray[10].src="images/fh.png"; $("#time").empty(); for(var i=0;i<str.length;i++){ var oImg=imageArray[str.charAt(i)]; //oImg.attr("src",imageArray[i].src); $("#time").append(oImg); } } </script> <p id="time"></p>
Method 3:
Hardcode the a1f02c36ba31691bcfe87b2722de723b tag in the html.
<p id="time"><img src="images/0.png"/><img src="images/0.png"/><img src="images/0.png"/><img src="images/0.png"/><img src="images/0.png"/><img src="images/0.png"/></p> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> window.onload=function(){ var oBody=document.body; var oP=document.getElementById("time"); setInterval(fnTime,1000); fnTime(); } function fnTime(){ var myTime=new Date(); var iHours=myTime.getHours(); var iMin=myTime.getMinutes(); var iSec=myTime.getSeconds(); var str=toTwo(iHours)+toTwo(iMin)+toTwo(iSec); //oP.innerHTML=str; strToImg(str); } var imageArray=[]; //将个图像定义给相应的数组元素,使数组元素下标与图像所对应的数字字符一一对应 imageArray[0]="images/0.png"; imageArray[1]="images/1.png"; imageArray[2]="images/2.png"; imageArray[3]="images/3.png"; imageArray[4]="images/4.png"; imageArray[5]="images/5.png"; imageArray[6]="images/6.png"; imageArray[7]="images/7.png"; imageArray[8]="images/8.png"; imageArray[9]="images/9.png"; imageArray[10]="images/fh.png"; /*将数字转换为字符串且一位数显示为两位,*/ function toTwo(n){ return n<10?'0'+n:''+n; } function strToImg(str){ var str=str; for(var i=0;i<str.length;i++){ $("#time").find("img").eq(i).attr("src",imageArray[str.charAt(i)]); } } </script>
Method 4: [Recommended]
Dynamicly generate a1f02c36ba31691bcfe87b2722de723b tags and write them efficiently.
<p id="time"></p> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> window.onload=function(){ var oBody=document.body; var oP=document.getElementById("time"); setInterval(fnTime,1000); fnTime(); } function fnTime(){ var myTime=new Date(); var iHours=myTime.getHours(); var iMin=myTime.getMinutes(); var iSec=myTime.getSeconds(); var str=toTwo(iHours)+toTwo(iMin)+toTwo(iSec); //oP.innerHTML=str; strToImg(str); } /*将数字转换为字符串且一位数显示为两位,*/ function toTwo(n){ return n<10?'0'+n:''+n; } var imageArray=[]; //将个图像定义给相应的数组元素,使数组元素下标与图像所对应的数字字符一一对应 imageArray[0]="images/0.png"; imageArray[1]="images/1.png"; imageArray[2]="images/2.png"; imageArray[3]="images/3.png"; imageArray[4]="images/4.png"; imageArray[5]="images/5.png"; imageArray[6]="images/6.png"; imageArray[7]="images/7.png"; imageArray[8]="images/8.png"; imageArray[9]="images/9.png"; imageArray[10]="images/fh.png"; function strToImg(str){ var str=str; var tempHtml=''; for(var i=0;i<str.length;i++){ var imgHtml="<img src="+imageArray[str.charAt(i)]+"/>"; tempHtml+=imgHtml; } $("#time").html(tempHtml); } </script>
The above is all the code for creating picture clock effects with jquery. I hope you like it.