This article example shares the specific code of layui to implement the login interface verification code for your reference. The specific content is as follows Effect picture: html: <div class="layui-form-item"> <div class="layui-col-xs6" > <input type="text" value="" placeholder="Please enter the verification code (not case sensitive)" class="input-val"> <canvas id="canvas" width="100" height="30"></canvas> </div> <div> <input type="button" value="Login" class="layui-btn layui-btn-fluid" lay-submit lay-filter="login"> </div> </div> Next is JS: var show_num=[]; $(function() { draw(show_num); $("#canvas").on('click',function() { draw(show_num); }) }); Then call two functions: function draw(show_num) { var canvas_width = $('#canvas').width(); var canvas_height = $('#canvas').height(); var canvas = document.getElementById("canvas"); //Get the canvas object, the actor var context = canvas.getContext("2d"); //Get the canvas drawing environment, the actor's performance stage canvas.width = canvas_width; canvas.height = canvas_height; 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"; var aCode = sCode.split(","); var aLength = aCode.length; //Get the length of the array for (var i = 0; i <= 3; i++) { var j = Math.floor(Math.random() * aLength); //Get a random index value var deg = Math.random() * 30 * Math.PI / 180; //Generate a random radian between 0 and 30 var txt = aCode[j]; //Get a random content show_num[i] = txt.toLowerCase(); var x = 10 + i * 20; //x coordinate of the text on the canvasvar y = 20 + Math.random() * 8; //y coordinate of the text on the canvascontext.font = "bold 23px Microsoft YaHei"; context.translate(x, y); context.rotate(deg); context.fillStyle = randomColor(); context.fillText(txt, 0, 0); context.rotate(-deg); context.translate(-x, -y); } for (var i = 0; i <= 5; i++) { //Display lines on the verification code context.strokeStyle = randomColor(); context.beginPath(); context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height); context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height); context.stroke(); } for (var i = 0; i <= 30; i++) { //Show small dots on the verification code context.strokeStyle = randomColor(); context.beginPath(); var x = Math.random() * canvas_width; var y = Math.random() * canvas_height; context.moveTo(x, y); context.lineTo(x + 1, y + 1); context.stroke(); } } function randomColor() { //Get a random color value var r = Math.floor(Math.random() * 256); var g = Math.floor(Math.random() * 256); var b = Math.floor(Math.random() * 256); return "rgb(" + r + "," + g + "," + b + ")"; } The style must be adjusted according to the project. Here is my style: <style> .code { width: 100%; margin: 0 auto; } .input-val { width: 63%; background: #ffffff; height: 2.8rem; padding: 0 2%; border-radius: 5px; border: none; border: 1px solid rgba(0,0,0,.2); font-size: 0.9rem; } #canvas { float: right; display: inline-block; border: 1px solid #ccc; border-radius: 5px; cursor: pointer; } </style> The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: HTML dynamically loads css styles and js scripts example
>>: Linux operation and maintenance basic process management real-time monitoring and control
dig - DNS lookup utility When a domain name acces...
[LeetCode] 175.Combine Two Tables Table: Person +...
Table of contents Preface props context state Sum...
Shell is a program written in C language, which i...
Installation Steps 1. Install Redis Download the ...
<br />What is web2.0? Web2.0 includes those ...
Table of contents Prepare Five weapons for…in Obj...
Overview Nginx load balancing provides upstream s...
Table of contents MySQL query tree structure 1. A...
I encountered several problems when installing My...
This article uses examples to illustrate the opti...
Moore's Law no longer applies Starting with F...
This article shares with you a detailed tutorial ...
Table of contents What is a skeleton screen? Demo...
Sometimes, we need to use the hyperlink <a> ...