add: util.jsの追加

This commit is contained in:
yuki540 2018-02-21 13:30:20 +09:00
parent 5f3dc00164
commit ae7375116a

55
src/scripts/lib/util.js Normal file
View File

@ -0,0 +1,55 @@
/**
* 画像のプリロード
* @param images : 画像パスの配列
* @param fn : コールバック関数
* @param progress : 読み込み状況取得用コールバック関数
*/
export const preload = (images, fn, progress) => {
const len = images.length
let load = 0
images.forEach((image, key) => {
const img = new Image()
img.src = image
img.onload = () => {
load += 1
progress({
size : len,
load : load,
per : load / len
})
if(load >= len) fn()
}
})
}
/**
* スマートフォンかどうかの真偽
* @return bool
*/
export const isSP = () => {
const useragent = navigator.userAgent.toLowerCase()
const reg = /(iphone|ipad|ipod|android|mobile)/
return reg.test(useragent)
}
/**
* PCかどうかの真偽
* return bool
*/
export const isPC = () => {
return !isSP()
}
/**
* 指定した時間に要素を非表示にする
* @param selector : セレクタ名
* @param time : 時間
*/
export const hiddenTimeout = (selector, time) => {
setTimeout(() => {
document.querySelector(selector).style.display = 'none'
}, time)
}