iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 3
0
Modern Web

師父領進門 修行在個人系列 第 10

JS挑戰-(6)-Custom HTML5 Video Player + Key Sequence Detection

  • 分享至 

  • xImage
  •  

繼續挑戰,這週希望能自己有些想法,可以邊做邊去挑戰。

  1. HTML5 Video Player
  • 算是綜合到目前為主複雜度最高的,頗有挑戰性。
  • 可能要多做幾次&把挑戰:做出一個全螢幕的button
// Get elements

const player = document.querySelector('.player');
const video = player.querySelector('.viewer');
const progress = player.querySelector('.progress');
const progressBar = player.querySelector('.progress__filled');
const toggle = player.querySelector('.toggle');
const skipButton =player.querySelectorAll('[data-skip]')
const ranges = player.querySelectorAll('.player__slider')

// function
function togglePlay(){
  if(video.paused){video.play()}
  else {video.pause()}
}

function updateButton(){
  const icon = this.paused ?  '►' : '❚ ❚';
  toggle.textContent = icon ;
}
//no playing property , only paused

function skip(){
  console.log(this.dataset.skip);
  video.currentTime += parseFloat(this.dataset.skip);
}

function handleRangeUpdate(){
  video[this.name] = this.value;
}

function handleProgess(){
  const percent = (video.currentTime / video.duration) * 100 ;
  progressBar.style.flexBasis = `${percent}%` ;
}
function scrub(e){
  const scrubTime = (e.offsetX / progress.offsetWidth) * video.duration;
  video.currentTime = scrubTime;
}

// Hook up event listener
video.addEventListener('click', togglePlay);
video.addEventListener('play', updateButton);
video.addEventListener('pause',updateButton);
video.addEventListener('timeupdate',handleProgess);

toggle.addEventListener('click', togglePlay);
skipButton.forEach(button => button.addEventListener('click', skip));

ranges.forEach(range => range.addEventListener('change', handleRangeUpdate));
ranges.forEach(range => range.addEventListener('mousemove', handleRangeUpdate));

let mousedown = false ;
progress.addEventListener('click', scrub);
progress.addEventListener('mousemove', (e) => mousedown && scrub(e));
progress.addEventListener('mousedown' ,() => mousedown = true);
progress.addEventListener('mouseup' ,() => mousedown = false);


  1. Key Sequence Detection - Konami Code
  • 重點:push() , splice(), .join('').includes()
  • 幽默的library cornify.js
const pressed = [] ;
const secretCode = 'frank';

window.addEventListener('keyup', (e) => {
  pressed.push(e.key);
  pressed.splice(-secretCode.length-1 ,pressed.length - secretCode.length);
  console.log(pressed);

  if (pressed.join('').includes(secretCode)) {
    console.log('!!!YAYAYA!!!');
    cornify_add(); /// cornify.js的效果
  }
})

上一篇
休息一下,看看其他人-筆記(1)
下一篇
JS挑戰—(7)— Slide in on Scroll+Reference and Copy
系列文
師父領進門 修行在個人30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言