1、css实现宽度是百分比的盒子为正方形
<div style="50%;padding-bottom:50%;height:0px;background:#ccc;"></div>
2、手机端判断是横屏还是竖屏
function checkOrient() { if (window.orientation == 0 || window.orientation == 180){ alert('竖屏'); }else if (window.orientation == 90 || window.orientation == -90){ alert('横屏'); } } // 添加事件监听 addEventListener('load', function(){ checkOrient(); });
3、不固定行数的文字垂直居中
需求:一个盒子div中有一段文字,文字行数不确定,如何实现该段文字在盒子内垂直居中
方法1:
在div盒子上使用两个css属性:display:table-cell;vertical-align:middle;
方法2:
在div盒子内部放置另外一个盒子p元素,把文字放置到p元素中,
然后div盒子设置:position:relative;
p元素设置:position:absolute;top:50%;transform:translate(-50%);
4、动态正则校验
function dynamicReg(text) {
eval("var reg = /^" + text + "$/;");
console.log(reg);
console.log(reg.test('123456'));
}
dynamicReg('34'); //false
dynamicReg('123456'); //true
5、不固定宽高的盒子水平垂直居中
.parent { 60%; height: 500px; position: relative; border: 1px solid #000; } .box{ 30%; height: 20%; position: absolute; top:0; right:0; bottom:0; left:0; margin: auto; background-color: blue; } <div class="parent"> <div class="box"></div> </div>
6、移动设备上实现滚动回弹效果
-webkit-overflow-scrolling: touch; //手指离开屏幕后,还会滚动一段距离 -webkit-overflow-scrolling: auto; //手指离开屏幕后,立即停止滚动
7、盒子四周阴影效果
div { box-shadow: 0px 0px 15px #000; }
8、正则表达式
1、正则校验是否是汉字或全角
/[^x00-xff]/g.test('abc') //false
/[^x00-xff]/g.test('表达') //true
/[^x00-xff]/g.test('.') //false
/[^x00-xff]/g.test('。') //true
2、密码校验:必须包含数字、英文、符号中的至少两种:
/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z~!@#$%^&*()_+<>?.,]{8,20}$/
3、密码校验:必须包含数字、英文字母、特殊符号且大于等于8位:(特殊字符只能为 ~!@#$%^&*()_+<>?., 中的一种或几种)
var reg = /^(?=.*d)(?=.*[a-zA-Z])(?=.*[~!@#$%^&*()_+<>?.,])[da-zA-Z~!@#$%^&*()_+<>?.,]{8,}$/;
4、密码校验:必须包含数字,大写字母,小写字母,特殊字符中的至少三种:(特殊字符只能为 ~!@#$%^&*()_+<>?., 中的一种或几种)
var reg = new RegExp('^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-ZW~!@#$%^&*()_+<>?.,]+$)(?![a-z0-9]+$)(?![a-zW~!@#$%^&*()_+<>?.,]+$)(?![0-9W~!@#$%^&*()_+<>?.,]+$)[a-zA-Z0-9W~!@#$%^&*()_+<>?.,]{8,20}$');
9、获取浏览器相关信息
/**获得屏幕宽度**/ function getScreenWidth () { return window.screen.width; } /**获得屏幕高度**/ function getScreenHeight() { return window.screen.height; }
/**获得浏览器***/ function getBrowse() { var browser = {}; var userAgent = navigator.userAgent.toLowerCase(); var s; (s = userAgent.match(/msie ([d.]+)/)) ? browser.ie = s[1] : (s = userAgent.match(/firefox/([d.]+)/)) ? browser.firefox = s[1] : (s = userAgent.match(/chrome/([d.]+)/)) ? browser.chrome = s[1] : (s = userAgent.match(/opera.([d.]+)/)) ? browser.opera = s[1] : (s = userAgent.match(/version/([d.]+).*safari/)) ? browser.safari = s[1] : 0; var version = ""; if (browser.ie) { version = 'IE ' + browser.ie; } else { if (browser.firefox) { version = 'firefox ' + browser.firefox; } else { if (browser.chrome) { version = 'chrome ' + browser.chrome; } else { if (browser.opera) { version = 'opera ' + browser.opera; } else { if (browser.safari) { version = 'safari ' + browser.safari; } else { version = '未知浏览器'; } } } } } return version; }
/**获得操作系统***/ function getClientOs() { var sUserAgent = navigator.userAgent; var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows"); var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel"); if (isMac) return "Mac"; var isUnix = (navigator.platform == "X11") && !isWin && !isMac; if (isUnix) return "Unix"; var isLinux = (String(navigator.platform).indexOf("Linux") > -1); if (isLinux) return "Linux"; if (isWin) { var isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1; if (isWin2K) return "Win2000"; var isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 || sUserAgent.indexOf("Windows XP") > -1; if (isWinXP) return "WinXP"; var isWin2003 = sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") > -1; if (isWin2003) return "Win2003"; var isWinVista = sUserAgent.indexOf("Windows NT 6.0") > -1 || sUserAgent.indexOf("Windows Vista") > -1; if (isWinVista) return "WinVista"; var isWin7 = sUserAgent.indexOf("Windows NT 6.1") > -1 || sUserAgent.indexOf("Windows 7") > -1; if (isWin7) return "Win7"; } return "other"; }
10、生成唯一的uuid
function guid() { function S4() { return (((1+Math.random())*0x10000)|0).toString(16).substring(1); } return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()); } function guid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); return v.toString(16); }); } function uuid() { var s = []; var hexDigits = "0123456789abcdef"; for (var i = 0; i < 36; i++) { s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); } s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 s[8] = s[13] = s[18] = s[23] = "-"; var uuid = s.join(""); return uuid; }