用到的方法
[[toc]]
获取给定范围的随机数 export const getRand= (min, max )=> { let range = max - min + 1 ; return Math .floor(Math .random() * range + min); }
吸顶 let offsetTop =[dom].getBoundingClientRect().top;if (offsetTop < 0 ) { this .titleFixed = true } else { this .titleFixed = false } (offsetTop < 0 ) ? this . titleFixed = true : this . titleFixed = false ; this . titleFixed = offsetTop < 0 ;function getOffset (obj, direction ) { let offsetL = 0 ; let offsetT = 0 ; while ( obj !== window . document.body && obj !== null ){ offsetL += obj.offsetLeft; offsetT += obj.offsetTop; obj = obj.offsetParent; } if (direction === 'left' ){ return offsetL; }else { return offsetT }; }
字符串去空 export const trim = (str, is_global ) => { let result; result = str.replace(/(^\s+)|(\s+$)/g , "" ); if (is_global.toLowerCase() === "g" ) { result = result.replace(/\s/g , "" ); } return result; };
经常用到的一些小正则 let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/ ;let reg=/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/ ;let reg = /^[\u4e00-\u9fa5]*$/ ;let reg = /^1\d{10}$/ ;let reg =/\(([^)]*)\)/g let reg =/^(-)?\d{1,3}(,\d{3})*(.\d+)?$/
单击和双击同时存在 dbJumpHome = () => { if (this .clickTimer) { window .clearTimeout(this .clickTimer); this .clickTimer = null ; } }; jumpTo = () => { if (this .clickTimer) { window .clearTimeout(this .clickTimer); this .clickTimer = null ; } this .clickTimer = window .setTimeout(() => { }, 200 ); };
同时调取多个接口 const createSource = await Promise .all([queryWxapps(scenicid()['id' ]), queryWxapps(scenicid()['id' ]), queryWxapps(scenicid()['id' ])])console .log(createSource);axios.all([queryWxapps(scenicid()['id' ]), queryWxapps(scenicid()['id' ]), queryWxapps(scenicid()['id' ]).then(axios.spread((user, aside ) => { console .log(user); console .log(aside); }));
移动端兼容性问题 ;(/iphone|ipod|ipad/i .test(navigator.appVersion )) && document .addEventListener ('blur' , (e ) => { if ( ['input' , 'textarea' ].includes(e.target.localName) ) { document .body.scrollIntoView(true ); } }, true );
获取滚动条的高度 function ScollPostion ( ) {let t, l, w, h; if (document .documentElement && document .documentElement.scrollTop) { t = document .documentElement.scrollTop; l = document .documentElement.scrollLeft; w = document .documentElement.scrollWidth; h = document .documentElement.scrollHeight; } else if (document .body) { t = document .body.scrollTop; l = document .body.scrollLeft; w = document .body.scrollWidth; h = document .body.scrollHeight; } return { top : t, left : l, width : w, height : h }; } window .pageYOffset == window .scrollY; var supportPageOffset = window .pageXOffset !== undefined ;var isCSS1Compat = ((document .compatMode || "" ) === "CSS1Compat" );var x = supportPageOffset ? window .pageXOffset : isCSS1Compat ? document .documentElement.scrollLeft : document .body.scrollLeft;var y = supportPageOffset ? window .pageYOffset : isCSS1Compat ? document .documentElement.scrollTop : document .body.scrollTop;
URL的编码解码 encodeURIComponent (URIstring)encodeURIComponent (",/?:@&=+$#" )decodeURIComponent (URIstring)decodeURIComponent ("%2C%2F%3F%3A%40%26%3D%2B%24%23" )
获取图片的原始尺寸 function getImgNaturalDimensions (oImg, callback ) { var nWidth, nHeight; if (!oImg.naturalWidth) { nWidth = oImg.naturalWidth; nHeight = oImg.naturalHeight; callback({w : nWidth, h :nHeight}); } else { var nImg = new Image(); nImg.onload = function ( ) { var nWidth = nImg.width, nHeight = nImg.height; callback({w : nWidth, h :nHeight}); } nImg.src = oImg.src; } } var img = document .getElementById("oImg" );getImgNaturalDimensions(img, function (dimensions ) { console .log(dimensions.w); });
无尺寸的图像布局偏移,图片占位 https://web.dev/optimize-cls/#images-without-dimensions
布局偏移可能会分散用户的注意力。想象一下,您已经开始阅读一篇文章,可是页面上的元素突然位移,让您措手不及,于是您不得不再次找到先前阅读的位置。 这在网络上十分常见,包括在阅读新闻或尝试单击”搜索”或”添加到购物车”按钮时。这种体验在视觉上十分扎眼且令人郁闷。 这些情况通常是由于另一个元素被突然添加到页面上或是突然调整了大小,使可见元素被迫移动位置而导致的。
现代最佳实践
首先在 img 标签上添加 width height
<img src ="puppy.jpg" width ="640" height ="360" alt ="小狗与气球" />
其次在 css 上添加以下代码
img { height : auto; width : 100% ; }
一个个添加效率低,服务端返回的尺寸也不清晰,我们可以封装一个方法,帮助我们自动添加
export const readImageSize = (imgUrl ) => new Promise ((resolve, reject ) => { const img = new Image() img.src = imgUrl img.onload = () => { resolve({ width: img.naturalWidth || img.width, height: img.naturalHeight || img.height, }) } img.onerror = reject })
然后拿到 img 的链接,动态添加,:width=”xxxx” 、 :height=”xxxx”
export const fillImageSizes = (templateObj ) => { return Promise .all(Object .keys(templateObj).map((key ) => { const item = templateObj[key] if (item.width && item.height) { return } return readImageSize(this .templateImgUrl(key)).then(({ width, height } ) => { Object .assign(item, { width, height }) }, () => { }) })) }