JavaScript tool functions commonly used in front-end business are commonly used in browser environments and can be copied directly in the project. This article will be updated continuously.
1. File to base64
/** * file to base64 * @param {*} file file object * @param {*} callback */ export const fileToDataURL = (file, callback) => { let freader = new FileReader(); (file); = function (e) { callback(); } }
2. Convert blob stream to base64
/** * blob stream converted to base64 * @param {*} blob blob object * @param {*} callback */ export const blobToDataURL = (blob, callback) => { let freader = new FileReader(); (blob); = function (e) { callback(); } }
3. Convert base64 to blob
/** * Base64 converted to blob * @param {*} dataurl base64 */ export const dataURLtoBlob = (dataurl) => { let arr = (','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = , u8arr = new Uint8Array(n); while (n--) { u8arr[n] = (n); } return new Blob([u8arr], { type: mime }); }
4. Convert base64 to file, IE lower version is incompatible
/** * Convert base64 to file, IE lower version is incompatible * @param {*} dataurl base64 * @param {*} filename filename */ export const dataURLtoFile = (dataurl, filename) => { let arr = (','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = , u8arr = new Uint8Array(n); while (n--) { u8arr[n] = (n); } return new File([u8arr], filename, { type: mime }); }
5. Convert picture url to base64
/** * Image url is converted to base64 * @param {*} url Picture url * @param {*} callback * @param {*} outputFormat image format */ export const imgUrlToDataURL = (url, callback, outputFormat) => { let canvas = ('canvas'), ctx = ('2d'), img = new Image; = 'Anonymous'; = url + "?timeStamp=" + new Date().getTime(); = function () { = ; = ; // (img, 0, 0); (this, 0, 0, , ); let dataURL = (outputFormat || 'image/png'); // (this, dataURL); callback && callback(dataURL) canvas = null; }; }
6. Get window size
export function getViewportSize() { let w = 0; let h = 0; if () { w = h = } else if ( && ) { w = h = } else if ( && ) { w = h = } return { w, h } }
7. Browser environment detection
const ua = () // Is it WeChat or notexport const isWx = () => (/MicroMessenger/i) == 'micromessenger' // Is it ipadexport const isIpad = () => ('ipad') > -1 // Is it an iphoneexport const isIphone = () => ('iphone os') > -1 // Is it ucexport const isUc = () => ('ucweb') > -1 // Is it windows pcexport const isWindows = () => ('windows') > -1 // Is it androidexport const isAndroid = () => ('android') > -1 || ('adr') > -1 // Is it iosexport const isIos = () => /(iphone|ipod|ipad|ios)/(ua) // Is it a qq browserexport const isQq = () => ('mqqbrowser') > -1 && (' qq') < 0 // Is it a built-in browser for QQ?export const isQQInstalled = () => (' qq') > -1 && ('mqqbrowser') < 0
8. Turn on and off full screen
// Turn on full screenexport function launchFullscreen(element) { element = element || if () { () } else if () { () } else if () { () } else if () { () } } // Turn off full screenexport function exitFullscreen() { if () { () } else if () { () } else if () { () } else if () { () } }
9. Return to the top/specified position to realize scrolling animation
/** * @param {*} number Distance from the top of the page * @param {*} time time required to scroll Unit ms */ const scrolling = (number = 0, time) => { if (!time) { = = number; return number; } // Set the interval time of the cycle. The smaller the value, the higher the consumption performance. const spacingTime = 20; // Calculate the number of cycles let spacingInex = time / spacingTime; // Get the current scroll bar position let nowTop = + ; // Calculate the distance for each slide let everTop = (number - nowTop) / spacingInex; let scrollTimer = setInterval(() => { if (spacingInex > 0) { spacingInex--; ScrollTop(nowTop += everTop); } else { clearInterval(scrollTimer); // Clear the timer } }, spacingTime); }; // Scroll to a position 500px from the top of the page. The animation time is 300ms// scrolling(500, 300);
10. Anchor point rolling
// Use H5's scrollIntoView method, which is a function in an experiment. It is incompatible with IE 8 or below, Safari 6 or below, and Safari on iOS 5 or below.const scrollToAnchor = (anchorName) => { if (anchorName) { // Find the anchor point let anchorElement = (anchorName); // If the anchor point of the corresponding id exists, jump to the anchor point if (anchorElement) { ({ behavior: 'auto', // Define the animation transition effect, one of "auto" or "smooth". Default is "auto" block: 'start', // Define the alignment of the vertical direction, one of "start", "center", "end", or "nearest". Default is "start" inline: 'nearest', // Define the alignment of the horizontal direction, one of "start", "center", "end", or "nearest". Default is "nearest" }); } } }
The above is the detailed content of the summary of JavaScript's commonly used tool functions (browser environment). For more information about JavaScript tool functions, please pay attention to my other related articles!