Created
May 25, 2018 02:53
-
-
Save steelydylan/ffd7855e7b68aa6982eb968ea30e78ed to your computer and use it in GitHub Desktop.
jsonからランダムに動画を選んでdomに追加して再生するJavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import randomVideo from 'random-video'; | |
randomVideo('#js-random-video', '/sample.json'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios'; | |
const getRand = (a, b) => ~~(Math.random() * (b - a + 1)) + a; | |
export default (selector, url) => { | |
axios.get(url).then((item) => { | |
const data = item.data; | |
const index = getRand(0, data.length -1); | |
const target = data[index]; | |
const poster = target.poster; | |
const source = target.source; | |
let html = `<video autoplay muted loop playsinline webkit-playsinline poster="${poster}">`; | |
source.forEach((src) => { | |
html += `<source src="${src}">`; | |
}) | |
html += `</video>`; | |
document.querySelector(selector).innerHTML = html; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"poster": "/***/***.png", | |
"source": [ | |
"/***/***.mp4", | |
"/***/***.webm" | |
] | |
}, | |
{ | |
"poster": "/***/***.png", | |
"source":[ | |
"/***/***.mp4", | |
"/***/***.webm" | |
] | |
}, | |
{ | |
"poster": "/***/***.png", | |
"source":[ | |
"/***/***.mp4", | |
"/***/***.webm" | |
] | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://css-tricks.com/how-to-fix-video-slowing-down-page-load-time/