js-奇技淫巧
- 执行浏览器复制命令
注:只能复制文本框内的文字( input / textarea )
function jsCopy(){ var e = document.getElementById("content");//对象是content e.select(); //选择对象 document.execCommand("Copy"); //执行浏览器复制命令 alert("已复制好,可贴粘。"); }
- 文件上传进度条 - 原生ajax & 跨域问题
- 用JavaScript将canvas保存成图片格式
// Converts canvas to an image function convertCanvasToImage(canvas) { var image = new Image(); image.src = canvas.toDataURL("image/png"); return image; }
- 低级浏览器升级提示
<!--IE7以下极低级浏览器升级提示--> <!--[if lte IE 7]> <script>window.location.href='http://cdn.dmeng.net/upgrade-your-browser.html?referrer='+location.href;</script> <![endif]-->
- 判断浏览器类型
// 判断浏览器类型 function getBrowser() { let ua = window.navigator.userAgent; let isIE = window.ActiveXObject != undefined && ua.indexOf("MSIE") != -1; let isEdge = ua.indexOf("Trident") != -1; let isFirefox = ua.indexOf("Firefox") != -1; let isOpera = window.opr != undefined; let isChrome = ua.indexOf("Chrome") && window.chrome; let isSafari = ua.indexOf("Safari") != -1 && ua.indexOf("Version") != -1; if (isIE) { return "IE"; } else if (isFirefox) { return "Firefox"; } else if (isOpera) { return "Opera"; } else if (isChrome) { return "Chrome"; } else if (isSafari) { return "Safari"; } else if (isEdge) { return "isEdge"; } else { return "Unkown"; } } //判断是移动端还是pc function isMobileOrPC(){ let map = { phone:'phone', pad:'pad', pod:'pod', iPhone:'iPhone', iPod:'iPod', ios:'ios', iPad:'iPad', Android:'Android', Mobile:'Mobile', BlackBerry:'BlackBerry', IEMobile:'IEMobile', MQQBrowser:'MQQBrowser', JUC:'JUC', Fennec:'Fennec', wOSBrowser:'wOSBrowser', BrowserNG:'BrowserNG', WebOS:'WebOS', Symbian:'Symbian', "Windows Phone":"Windows Phone" } let reg = new RegExp(Object.values(map).join('|'),'i') if ((navigator.userAgent.match(reg))) { return 'mobile'; } return 'pc'; } export default { getBrowser, isMobileOrPC }
- 禁止、恢复H5页面滑动
//禁止H5页面滑动 document.body.style.overflow = 'hidden'; function _preventDefault(e) { e.preventDefault(); } window.addEventListener('touchmove', _preventDefault); //恢复滑动 document.body.style.overflow = 'auto'; window.removeEventListener('touchmove', _preventDefault);
以自己现在的努力程度,还没有资格和别人拼天赋