1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/** * @function Monitor whether the document tree is loaded. * @param fn */ function domReady(fn) { if (document.addEventListener) { // 标准浏览器 document.addEventListener('DOMContentLoaded', function () { //注销时间,避免重复触发 document.removeEventListener('DOMContentLoaded', arguments.callee, false); fn(); // 运行函数 }, false); } else if (document.attachEvent) { // IE浏览器 document.attachEvent('onreadystatechange', function () { if (document.readyState == 'complete') { document.detachEvent('onreadystatechange', arguments.callee); fn(); // 函数运行 } }); } } domReady(loadStart); |
标签:JavaScript
被引用的外部JS存在window.onload时,判断当前页面是否已存在window.onload,并进行相应处理
原生ajax方法封装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
/** * @function ajax request * @fields ajaxName:请求名称,method:请求方法,headers:setRequestHeader自定义部分,url:接口地址,async:是否异步请求,withCredentials:是否支持跨域发送cookie,dataType:数据类型 ,data:post请求参数 * @param data:{ajaxName:"ajaxNameString",headers:{},method:"GET/POST",url:"",async:true/false,withCredentials:true/false,dataType:"json",data:""} * @result ajaxName.responseText */ function ajaxRequest (data, callback) { data = data || {} data.dataType = data.dataType || 'json' var sendParams = null var headers = data.headers || {} var ajax = data.ajaxName // 新建请求 if (window.XMLHttpRequest) { ajax = new XMLHttpRequest() } else { ajax = new ActiveXObject('Microsoft.XMLHTTP') } // 打开请求 ajax.open(data.method.toUpperCase(), data.url, data.async) // 是否支持跨域发送cookie ajax.withCredentials = data.withCredentials ajax.setRequestHeader("Content-type", data.contentType || "application/x-www-form-urlencoded") // POST请求设置 if (data.method == 'POST') { for (var i in headers) { ajax.setRequestHeader(i, headers[i]) } if (data.data) { sendParams = data.data } } // 发送请求 ajax.send(sendParams ? sendParams : null) // 注册事件 ajax.onreadystatechange = function () { if (window.location.origin + '/login/index' === ajax.responseURL) { window.location.reload() window.location.href = window.location.origin + '/login/index' return } callback(ajax) } } /** * GET * @param ajaxName 请求名称 * @param requestUrl 请求接口地址 * @param async 是否异步请求 * @param callBack 回调函数 * @param contentType 请求类型 */ function ajaxGetData (ajaxName, requestUrl, async, callBack, contentType) { ajaxRequest({ ajaxName: ajaxName, contentType: contentType || "application/json;charset=utf-8", method: "GET", url: requestUrl, async: async, cache: false, withCredentials: true, dataType: "json" }, function callback (ajax) { if (ajax.status == 200 && ajax.readyState == 4) { callBack(ajax.responseText) } }) } /** * 拼接GET请求url参数 * @param url * @param params * @returns {string} */ function formateGetUrl (url, params) { var resultParams = '' for (var key in params) { resultParams = resultParams + '&' + key + '=' + params[key] } return url + '?' + resultParams.substr(1) } /** * POST * @param ajaxName 请求名称 * @param requestUrl 请求接口地址 * @param async 是否异步请求 * @param callBack 回调函数 * @param contentType 请求类型 */ function ajaxPostData (ajaxName, requestUrl, params, async, callBack, contentType) { var resultParams = '' if (!contentType || contentType === "application/x-www-form-urlencoded;charset=utf-8") { for (var key in params) { resultParams = resultParams + '&' + key + '=' + encodeURIComponent(params[key]) } } else { resultParams = params && JSON.stringify(params) } ajaxRequest({ ajaxName: ajaxName, headers: {}, contentType: contentType || "application/x-www-form-urlencoded;charset=utf-8", method: "POST", dataType: "json", url: requestUrl, processData: true, async: async, data: resultParams }, function callback (ajax) { if (ajax.status == 200 && ajax.readyState == 4) { callBack(ajax.responseText) } }) } |
JS格式化时间(支持小程序,兼容IOS)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
const REGEX = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/ /** * @function format time * @param val, format * @return {string} * @example * <template> * <div> * <span>{{item.time | formatTime('yyyy/MM/dd hh:mm:ss')}}</span> * </div> * </template> * import {formatTime} from '../../library/timeFormat' * export default { * filters: {formatTime} * } */ export const formatTime = (val, format) => { if (val) { /** * @instructions 如果不是时间戳格式,且含有字符 '-' 则将 '-' 替换成 '/' && 删除小数点及后面的数字 * @reason 将 '-' 替换成 '/' && 删除小数点及后面的数字 的原因是safari浏览器仅支持 '/' 隔开的时间格式 */ if (val.toString().indexOf('-') > 0) { val = val.replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '').replace(/(-)/g, '/') // 将 '-' 替换成 '/' val = val.slice(0, val.indexOf('.')) // 删除小数点及后面的数字 } let date = new Date(val) date.setHours(date.getHours() + 8) const [whole, yy, MM, dd, hh, mm, ss] = date.toISOString().match(REGEX) const year = new Date().getFullYear() const month = new Date().getMonth() + 1 const dates = new Date().getDate() if (format) { return format .replace('yyyy', yy) .replace('yy', yy.slice(2)) .replace('MM', MM) .replace('dd', dd) .replace('hh', hh) .replace('mm', mm) .replace('ss', ss) } else { return [yy, MM, dd].join('-') + ' ' + [hh, mm, ss].join(':') } } else { return '--' } } |
JS让任意图片垂直水平居中且页面不滚动
JS判断IE版本并在页面显示内容
判断当前系统当前浏览器是否安装启用 Adobe Flash Player,检查在chrome中的状态
一、判断当前所在系统
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
let sUserAgent = navigator.userAgent; let isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows"); let isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel"); if (isMac) return "Mac"; let isUnix = (navigator.platform == "X11") && !isWin && !isMac; if (isUnix) return "Unix"; let isLinux = (String(navigator.platform).indexOf("Linux") > -1); if (isLinux) return "Linux"; if (isWin) { let isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1; if (isWin2K) return "Windows2000"; let isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 || sUserAgent.indexOf("Windows XP") > -1; if (isWinXP) return "WindowsXP"; let isWin2003 = sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") > -1; if (isWin2003) return "Windows2003"; let isWinVista = sUserAgent.indexOf("Windows NT 6.0") > -1 || sUserAgent.indexOf("Windows Vista") > -1; if (isWinVista) return "WindowsVista"; let isWin7 = sUserAgent.indexOf("Windows NT 6.1") > -1 || sUserAgent.indexOf("Windows 7") > -1; if (isWin7) return "Windows7"; let isWin8 = sUserAgent.indexOf("Windows NT 6.2") > -1 || sUserAgent.indexOf("Windows 8") > -1; if (isWin8) return "Windows8"; let isWin10 = sUserAgent.indexOf("Windows NT 10.0") > -1 || sUserAgent.indexOf("Windows 10") > -1; if (isWin10) return "Windows10"; } return "OtherOS"; |
二、判断当前浏览器内核
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
let Sys = {}; let ua = navigator.userAgent.toLowerCase(); let s; (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] : (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] : (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] : (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0; if (Sys.ie) { console.log('ie core') } if (Sys.firefox) { console.log('gecko core') } if (Sys.chrome || Sys.safari) { console.log('webkit core') } |
三、判断浏览器是否安装 Adobe Flash Player
[crayon-662ae5c001472[……]
Js跑马灯效果 && 在Vue中使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
DEMO: <!DOCTYPE html> <html> <head> <title>滚动播报</title> <meta charset="UTF-8"> <style> .content { height: 60px; background-color: #2c2c34; overflow: hidden; } .content ul { white-space: nowrap; } .content ul li { position: relative; font-size: 14px; vertical-align: middle; line-height: 35px; padding: 0 8px; white-space: nowrap; display: inline-block; color: #fff } #scrollBox { overflow: hidden; } #scrollBox .scrollWrap { width: 500% } .scrollCont { float: left; } </style> </head> <body> <div class="content"> <ul> <div id="scrollBox"> <div class="scrollWrap"> <div id="scrollContOne" class="scrollCont"> <li>我是第一条数据</li> <li>我是第二条数据</li> <li>我是第三条数据</li> <li>我是第四条数据</li> <li>我是第五条数据</li> <li>我是第六条数据</li> <li>我是第七条数据</li> <li>我是第八条数据</li> </div> <div id="scrollContTwo" class="scrollCont"></div> </div> </div> </ul> </div> <script> let speed = 40 let scrollBox = document.getElementById("scrollBox"); let scrollContOne = document.getElementById("scrollContOne"); let scrollContTwo = document.getElementById("scrollContTwo"); scrollContTwo.innerHTML = scrollContOne.innerHTML; scrollBox.scrollLeft = 0; function scrollRadio() { if (scrollBox.scrollLeft >= scrollContTwo.offsetWidth) { scrollBox.scrollLeft -= scrollContOne.offsetWidth } else { scrollBox.scrollLeft += 2; } } let MyScrollRadio = setInterval(scrollRadio, speed); scrollBox.onmouseover = function() { clearInterval(MyScrollRadio) }; scrollBox.onmouseout = function() { MyScrollRadio = setInterval(scrollRadio, speed) }; </script> </body> </html> 在Vue中使用: <template> <div class="content"> <ul> <div id="scrollBox"> <div class="scrollWrap"> <div id="scrollContOne" class="scrollCont"> <li v-for="item in items"> <a href="{{item}" target="_blank"></a> </li> </div> <div id="scrollContTwo" class="scrollCont"></div> </div> </div> </ul> </div> </template> <style scoped> .content { height: 60px; background-color: #2c2c34; overflow: hidden; } .content ul { white-space: nowrap; } .content ul li { position: relative; font-size: 14px; vertical-align: middle; line-height: 35px; padding: 0 8px; white-space: nowrap; display: inline-block; } .content ul li a { text-decoration: none; color:#fff; } #scrollBox { overflow: hidden; margin-left: 36px; } #scrollBox .scrollWrap { width: 500% } .scrollCont { float: left; } </style> <script> export default { data: () => ({ canScrollTimer: 0 }), vuex: { getters: { scrollLists: state => state.scrollLists } }, watch:{ scrollLists:{ deep:true, handler(v,ov){ if(v.length){ this.run(); } } } }, methods: { run() { let speed = 40; let scrollBox = document.getElementById("scrollBox"); let scrollContOne = document.getElementById("scrollContOne"); let scrollContTwo = document.getElementById("scrollContTwo"); scrollContTwo.innerHTML = scrollContOne.innerHTML; scrollBox.scrollLeft = 0; function scrollRadio() { if (scrollBox.scrollLeft >= scrollContTwo.offsetWidth) { scrollBox.scrollLeft -= scrollContOne.offsetWidth } else { scrollBox.scrollLeft += 2; } } let MyScrollRadio = setInterval(scrollRadio, speed); scrollBox.onmouseover = function() { clearInterval(MyScrollRadio) }; scrollBox.onmouseout = function() { MyScrollRadio = setInterval(scrollRadio, speed) }; } }, ready() { //接口调用 } } </script> |
JS获取当前时间及时间戳相互转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
1.获取当前时间的 时间戳 Date.parse(new Date()) 结果:1486347562000 2.获取当前 时间 new Date() 结果:Mon Feb 06 2017 10:19:42 GMT+0800 (CST) 3.将 时间戳 转换成 时间 new Date(1486347562000) 结果:Mon Feb 06 2017 10:19:22 GMT+0800 (CST) 4.将 时间 转换成 时间戳 Date.parse(new Date("2017-02-06 10:30:50")) 结果:1486348250000 |
根据窗口大小改变悬浮窗显示方式并自动刷新页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
如果一个页面有个浮动的二维码,当页面窗口缩小时二维码会遮盖住页面内容,这时候可以根据浏览器大小来决定显示方式: 1.当页面宽度足够大时,完全显示二维码 2.当页面窗口缩小时,这时候需要显示一个按钮,点击按钮才显示二维码 这时候需要添加onresize来监听窗口变化,以此来刷新页面: window.onresize = function() { //监听窗口变化 window.location.reload(); //兼容chrome safari window.location.href = ""; //兼容火狐 } 至于上面二维码的显示方式,在 vue 里使用 v-show 实现非常方便: HTML: <!-- 按钮 --> <div class="qr-btn" v-show="qrBtn" @click="showQr"><img src="../assets/images/tangulunyin.jpg" alt="谈股论银"></div> <!-- 二维码 --> <div class="qr-code" v-show="qrCode"> <ul> <li><img src="../assets/images/yinruyi.png" alt="银如意" title="银如意"><span>扫描下载银如意app</span></li> <li><img src="../assets/images/tangulunyin.jpg" alt="谈股论银"><span>扫描关注谈股论银公众号</span></li> </ul> </div> CSS: .qr-btn { width: 30px; height: 30px; background-color: #064491; display: block; position: absolute; left: 20px; bottom: 50px; border-radius: 50%; box-shadow: 2px 2px 8px rgba(0, 0, 0, .6); cursor: pointer } .qr-btn>img { width: 20px; height: 20px; position: relative; top: 5px; display: block; padding: 0 auto; margin: 0 auto; } .qr-code { position: fixed; bottom: 10%; left: 1%; background-color: aliceblue; font-size: 12px; text-align: center; } .qr-code>ul>li { padding: .8em; } .qr-code>ul>li:last-child { margin: 0 0 1em 0; } .qr-code>ul>li>img { width: 130px; height: 130px; display: block; } VUE: export default { data:()=>({ qrCode: false, qrBtn: false }) }, methods:{ showQr() { if (this.qrCode == false) { this.qrCode = true; } else { this.qrCode = false; } } }, ready(){ let w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; let h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; if (w < 1330) { //根据屏幕宽度决定是显示二维码还是按钮 this.qrCode = false; this.qrBtn = true; } else { this.qrCode = true; this.qrBtn = false; } window.onresize = function() { //监听窗口变化 window.location.reload(); //兼容chrome safari window.location.href = ""; //兼容火狐 } } ------------------------------------------------------------------- 以上的方法比较耗性能,而且不安全,另一个方法: VUE: export default { data:()=>({ qrCode: false, qrBtn: false }) }, methods:{ showQr() { if (this.qrCode == false) { this.qrCode = true; } else { this.qrCode = false; } }, listen() { let w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; if (w < 1330) { //根据屏幕宽度决定是显示二维码还是按钮 this.qrCode = false; this.qrBtn = true; } else { this.qrCode = true; this.qrBtn = false; } } }, ready(){ this.listen(); window.onresize = () => { //监听窗口变化 this.listen(); } } } |