分類
<style>
.video-con{ width: 300px; height: 150px; position: relative; margin-bottom: 20px;}
.video-con .video{ width: 100%; height: 100%;}
.video-con .btn{ width: 64px; height: 64px; background: transparent url("img/play-btn.png") no-repeat;
position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%); border: none;
}
.video-con .btn.pause{ background-image: url("img/pause-btn.png");}
</style>
<div class="video-con">
<video class="video" poster="">
<source src="mp4/1.mp4" >
</video>
<button type="button" class="btn js-play-btn"></button>
</div>
<!--html5的video-->
<script type="text/javascript">
// 視頻------視頻截圖 ~~ 視頻播放狀態(tài) ~~
const setMedia = function(video, scale = 0.8) {
// 設(shè)置poster屬性:(非本地視頻資源會(huì)有跨域截圖問(wèn)題)
video.addEventListener('loadeddata', function(e) {
// 拿到圖片
let canvas = document.createElement('canvas');
canvas.width = video.videoWidth * scale;
canvas.height = video.videoHeight * scale;
canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
let src = canvas.toDataURL('image/png');
// 顯示在dom,測(cè)試用
(function(flag = true) {
if (!flag) {return;}
let img = document.createElement('img');
img.src = src;
document.body.appendChild(img);
})(0);
// 設(shè)置屬性
video.setAttribute('poster', src);
});
// -------------------------------------------------------------------------------------
//檢測(cè)視頻播放狀態(tài):
//播放按鈕
let playBtn = video.parentNode.childNodes[2].nextSibling;
//設(shè)置狀態(tài)
function vidplaySate(e) {
if (video.paused) {
video.play();
playBtn.classList.add('pause');
} else {
video.pause();
playBtn.classList.remove('pause');
}
}
//點(diǎn)擊監(jiān)聽(tīng)
video.addEventListener('click', vidplaySate, false);
playBtn.addEventListener('click', vidplaySate, false);
//結(jié)束監(jiān)聽(tīng)
video.addEventListener('ended',function () {
playBtn.classList.remove('pause');
});
};
//視頻:
let videos = document.querySelectorAll('video');
videos.forEach((video) => {
setMedia(video);
});
</script>
1. 本站所有素材(未指定商用),僅限學(xué)習(xí)交流。
2. 會(huì)員在本站下載的原創(chuàng)商用和VIP素材后,只擁有使用權(quán),著作權(quán)歸原作者及17素材網(wǎng)所有。
3. 原創(chuàng)商用和VIP素材,未經(jīng)合法授權(quán),請(qǐng)勿用于商業(yè)用途,會(huì)員不得以任何形式發(fā)布、傳播、復(fù)制、轉(zhuǎn)售該素材,否則一律封號(hào)處理。
4. 本平臺(tái)織夢(mèng)模板僅展示和個(gè)人非盈利用途,織夢(mèng)系統(tǒng)商業(yè)用途請(qǐng)預(yù)先授權(quán)。