layui登陸界面驗證碼


首先是效果圖:

 

 html:

 

 

接下來就是JS:

 

 

 

 

 在調用的兩個函數:

 1 function draw(show_num) {
 2             var canvas_width = $('#canvas').width();
 3             var canvas_height = $('#canvas').height();
 4             var canvas = document.getElementById("canvas");//獲取到canvas的對象,演員
 5             var context = canvas.getContext("2d");//獲取到canvas畫圖的環境,演員表演的舞台
 6             canvas.width = canvas_width;
 7             canvas.height = canvas_height;
 8             var sCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0";
 9             var aCode = sCode.split(",");
10             var aLength = aCode.length;//獲取到數組的長度
11 
12             for (var i = 0; i <= 3; i++) {
13                 var j = Math.floor(Math.random() * aLength);//獲取到隨機的索引值
14                 var deg = Math.random() * 30 * Math.PI / 180;//產生0~30之間的隨機弧度
15                 var txt = aCode[j];//得到隨機的一個內容
16                 show_num[i] = txt.toLowerCase();
17                 var x = 10 + i * 20;//文字在canvas上的x坐標
18                 var y = 20 + Math.random() * 8;//文字在canvas上的y坐標
19                 context.font = "bold 23px 微軟雅黑";
20 
21                 context.translate(x, y);
22                 context.rotate(deg);
23 
24                 context.fillStyle = randomColor();
25                 context.fillText(txt, 0, 0);
26 
27                 context.rotate(-deg);
28                 context.translate(-x, -y);
29             }
30             for (var i = 0; i <= 5; i++) { //驗證碼上顯示線條
31                 context.strokeStyle = randomColor();
32                 context.beginPath();
33                 context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
34                 context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
35                 context.stroke();
36             }
37             for (var i = 0; i <= 30; i++) { //驗證碼上顯示小點
38                 context.strokeStyle = randomColor();
39                 context.beginPath();
40                 var x = Math.random() * canvas_width;
41                 var y = Math.random() * canvas_height;
42                 context.moveTo(x, y);
43                 context.lineTo(x + 1, y + 1);
44                 context.stroke();
45             }
46         }
47 
48         function randomColor() {//得到隨機的顏色值
49             var r = Math.floor(Math.random() * 256);
50             var g = Math.floor(Math.random() * 256);
51             var b = Math.floor(Math.random() * 256);
52             return "rgb(" + r + "," + g + "," + b + ")";
53         }

 

簡單上手,哦對了,樣式得自己根據自己的項目調配噢 下面是我的樣式:

 1    <style>
 2         .code {
 3             width: 100%;
 4             margin: 0 auto;
 5         }
 6         .input-val {
 7             width: 63%;
 8             background: #ffffff;
 9             height: 2.8rem;
10             padding: 0 2%;
11             border-radius: 5px;
12             border: none;
13             border: 1px solid rgba(0,0,0,.2);
14             font-size: 0.9rem;
15         }
16         #canvas {
17             float: right;
18             display: inline-block;
19             border: 1px solid #ccc;
20             border-radius: 5px;
21             cursor: pointer;
22         }
23     </style>

小白上路,請多指教 ,有什么疑問也可以問我哦

天行健,君子以自強不息! 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM