通用登陸界面1.1javascript
單獨頁面,不依賴任何js和css庫,直接拿來能夠用css
<!DOCTYPE html> <html lang="zh-cmn-Hans"> <head> <title>通用登陸界面1.1</title> <meta charset='UTF-8'> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/> <meta name="viewport" content="initial-scale=1, maximum-scale=3, minimum-scale=1, user-scalable=no"> <meta name="description" content="通用登陸界面1.1"/> <meta name="keywords" content="登陸"/> <meta name="author" content="lixingwu,lixingwu@aliyun.com"/> <style> body{background-color:#F2F2F2} .login-main{position:relative;left:0;top:0;padding:110px 0;min-height:100%;box-sizing:border-box} .login-box{width:375px;margin:0 auto;box-sizing:border-box;border:solid 2px #e2e2e2;background-color:#fff;position:relative} .login-box .title{padding:20px;text-align:center} .login-box .title h2{line-height:24px;padding:0;margin-bottom:10px;font-weight:bold;font-size:30px;color:#000} .login-box .title p{margin:20px auto 0;font-weight:300;color:#999} .login-box .login-form{padding:20px} .login-form .form-item{position:relative;margin-bottom:15px;clear:both} .login-form .form-item .form-label{position:absolute;left:1px;top:1px;width:38px;line-height:34px;text-align:center;color:#d2d2d2;font-size:16px;font-weight:bold} .login-form .form-item .form-input{box-sizing:border-box;outline:0;padding:0 10px 0 38px;display:block;width:100%;height:38px;line-height:1.3;border:1px solid #e6e6e6;background-color:#fff;border-radius:2px;transition:all .3s;color:#2F4056} input::-webkit-input-placeholder{color:#c2c2c2} .login-form .form-item .col-xs7{width:70.33333333%;float:left} .login-form .form-item .col-xs5{width:29.66666667%;float:left} .login-form .form-item .form-checkbox{padding-right:15px;line-height:18px;background:0 0;color:#666;display:inline-block;margin-top:7px} .form-item .form-checkbox label{display:inline-block;font-size:12px} .login-form .form-item .jump-change{margin-top:7px;float:right;color:#029789;text-decoration:none;cursor:pointer;line-height:24px;font-size:12px} .login-form .form-item .btn-fluid{display:inline-block;height:38px;line-height:38px;padding:0 18px;background-color:#009688;color:#fff;white-space:nowrap;text-align:center;font-size:14px;border:none;border-radius:2px;cursor:pointer;outline:0;-webkit-appearance:none;transition:all .3s;-webkit-transition:all .3s;box-sizing:border-box;width:100%} .btn-fluid:hover{opacity:.8;filter:alpha(opacity=80);color:#fff} .login-box .login-tip{position:absolute;top:-90px;width:370px;color:#999;padding:10px 0;line-height:30px;text-align:center;border:solid 2px #FFB800;border-radius:10px;background-color:#fff} .hide{display:none !important;visibility:hidden !important} </style> </head> <body> <div class="login-main"> <div class="login-box"> <div class="title"> <h2>後臺登陸</h2> <p>通用登陸界面1.1</p> </div> <div class="login-form"> <form id="loginForm"> <!--input的name本身根據後臺接收的參數進行調整,js沒有用到name進行處理--> <div class="form-item"> <label for="username" class="form-label">♛</label> <input type="text" name="loginName" id="username" class="form-input" placeholder="用戶名"/> </div> <div class="form-item"> <label for="password" class="form-label">❄</label> <input type="password" name="loginPass" id="password" class="form-input" placeholder="密碼"> </div> <div class="form-item"> <div class="col-xs7"> <label for="vercode" class="form-label">♻</label> <input type="text" name="checkCode" id="vercode" class="form-input" placeholder="驗證碼"> </div> <div class="col-xs5"> <div style="margin-left: 10px;margin-top: 5px;"> <img src="/vcode?codeKey=admin" class="login-codeimg" id="login-vercode" title="點擊圖片刷新"> </div> </div> </div> <div class="form-item"> <div class="form-checkbox"> <input type="checkbox" id="remember" title="記住密碼"> <label for="remember">記住密碼</label> </div> <a href="javascript:alert('本身想辦法吧')" class="jump-change">忘記密碼?</a> </div> <div class="form-item"> <button class="btn-fluid" id="login-submit">立刻搞事</button> </div> </form> </div> <p class="login-tip hide"></p> </div> </div> </body> <script> window.onload = function () { //判斷是否記住登陸 (function () { var username = localStorage.getItem("username"); var password = localStorage.getItem("password"); if (username !== null && password !== null) { document.getElementById('username').value = username; document.getElementById('password').value = password; document.getElementById('remember').checked = true; } })(); //歡迎提示,可刪除 var tip = showTip("歡迎回來喵!"); //點擊登入按鈕 document.getElementById('login-submit').onclick = function (e) { e.preventDefault(); var _this = this; if (validataForm()) { _this.setAttribute("disabled", "disabled"); var loginForm = document.getElementById('loginForm'); var username = document.getElementById('username'); var password = document.getElementById('password'); var ajax = new XMLHttpRequest(); var url = "/site/loginAction";//這是登陸的act ajax.open("POST", url + "?r_=" + new Date().getTime() + "&" + loginForm.serialize(), true); ajax.send(); ajax.onreadystatechange = function () { if (ajax.readyState === 4 && ajax.status === 200) { var obj = JSON.parse(ajax.responseText); /********************按照後臺返回的數據來處理*******************/ if (obj.success) { rememberLogin(username.value, password.value); tip = showTip("<span style='color: #5FB878;'>登陸成功,正在跳轉到首頁...</span>"); setTimeout(function () { window.location.href = "/redare/controlPanel"; }, 500); } else { tip = showTip(obj.message, false); updateCode(); } /**********************到這裏*********************************/ _this.removeAttribute("disabled"); } else { tip = showTip("服務器請求失敗!" + ajax.status, false); } } } }; //回車登陸 document.getElementById('vercode').onkeyup = function (e) { if (window.event) e = window.event; var code = e.charCode || e.keyCode; if (code === 13) { document.getElementById('login-submit').click(); } }; //點擊圖片驗證刷新 document.getElementById('login-vercode').onclick = function (e) { updateCode(); }; //刷新驗證碼 function updateCode() { var _code = document.getElementById('login-vercode'); _code.src = _code.src + "?t_" + new Date().getTime(); } //記住登陸 function rememberLogin(username, password) { var remember = document.getElementById('remember'); if (remember.checked) { localStorage.setItem("username", username); localStorage.setItem("password", password); } else { localStorage.removeItem("username"); localStorage.removeItem("password"); } } // 判斷是否填寫了相關表單 function validataForm() { clearTimeout(tip);//清除tip,防止影響到下一次提示 var username = document.getElementById('username'); var password = document.getElementById('password'); var vercode = document.getElementById('vercode'); if (!/^[0-9a-zA-Z\_]+$/.test(username.value)) { tip = showTip("用戶名爲字母數字組成!"); return false; } if (!/^.{6,32}$/.test(password.value)) { tip = showTip("密碼爲6到32爲任意字符組成!"); return false; } if (!/^[0-9a-zA-Z]{4}$/.test(vercode.value)) { tip = showTip("驗證碼爲4字母數字組成!"); return false; } return true; } //顯示提示框 function showTip(tipTxt, autoHide) { autoHide = (autoHide === undefined ? true : autoHide); var loginTip = document.getElementsByClassName("login-tip")[0]; loginTip.innerHTML = tipTxt; loginTip.classList.remove("hide"); if (autoHide) { tip = setTimeout(function () { loginTip.classList.add("hide"); }, 3000); } return tip; } // 表單序列化, HTMLFormElement.prototype.serialize = function () { var form = this; // 表單數據 var arrFormData = [], objFormData = {}; [].slice.call(form.elements).forEach(function (ele) { // 元素類型和狀態 var type = ele.type, disabled = ele.disabled; // 元素的鍵值 var name = ele.name, value = encodeURIComponent(ele.value || 'on'); // name參數必須,元素非disabled態,一些類型忽略 if (!name || disabled || !type || (/^reset|submit|image$/i.test(type)) || (/^checkbox|radio$/i.test(type) && !ele.checked)) { return; } type = type.toLowerCase(); if (type !== 'select-multiple') { if (objFormData[name]) { objFormData[name].push(value); } else { objFormData[name] = [value]; } } else { [].slice.call(ele.querySelectorAll('option')).forEach(function (option) { var optionValue = encodeURIComponent(option.value || 'on'); if (option.selected) { if (objFormData[name]) { objFormData[name].push(optionValue); } else { objFormData[name] = [optionValue]; } } }); } }); for (var key in objFormData) { arrFormData.push(key + '=' + objFormData[key].join()); } return arrFormData.join('&'); }; }; </script> </html>
效果圖:html