|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="zh-TW"> |
| 3 | + |
| 4 | +<head> |
| 5 | + <meta charset="UTF-8"> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 7 | + <meta http-equiv="X-UA-Compatible" content="ie=edge"> |
| 8 | + <meta name="description" content="背不起來的就寫下來吧。"> |
| 9 | + <meta property="og:type" content="website"> |
| 10 | + <meta property="og:title" content="Gua's Note"> |
| 11 | + <meta property="og:url" content="https://guahsu.io/"> |
| 12 | + <meta property="og:site_name" content="Gua's Note"> |
| 13 | + <script> |
| 14 | + (function (i, s, o, g, r, a, m) { |
| 15 | + i['GoogleAnalyticsObject'] = r; |
| 16 | + i[r] = i[r] || function () { |
| 17 | + (i[r].q = i[r].q || []).push(arguments) |
| 18 | + }, i[r].l = 1 * new Date(); |
| 19 | + a = s.createElement(o), |
| 20 | + m = s.getElementsByTagName(o)[0]; |
| 21 | + a.async = 1; |
| 22 | + a.src = g; |
| 23 | + m.parentNode.insertBefore(a, m) |
| 24 | + })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga'); |
| 25 | + ga('create', 'UA-84594235-4', 'auto'); |
| 26 | + ga('send', 'pageview'); |
| 27 | + </script> |
| 28 | + <title>JavaScript30 - 28 - Video Speed Controller | Gua's Note</title> |
| 29 | + <link rel="stylesheet" href="style.css"> |
| 30 | +</head> |
| 31 | + |
| 32 | +<body class="bod"> |
| 33 | + |
| 34 | + <style> |
| 35 | + body { |
| 36 | + margin-top: 48px; |
| 37 | + } |
| 38 | + |
| 39 | + .GuaHsu-header { |
| 40 | + position: absolute; |
| 41 | + left: 0; |
| 42 | + top: 0; |
| 43 | + width: 100%; |
| 44 | + background-color: #333; |
| 45 | + text-align: center; |
| 46 | + padding: 10px; |
| 47 | + color: #7ff3cb; |
| 48 | + font-size: 20px; |
| 49 | + font-weight: 100; |
| 50 | + z-index: 999; |
| 51 | + } |
| 52 | + |
| 53 | + .GuaHsu-header span { |
| 54 | + margin: 0 5px; |
| 55 | + } |
| 56 | + |
| 57 | + .GuaHsu-header a { |
| 58 | + text-decoration: none; |
| 59 | + color: unset; |
| 60 | + } |
| 61 | + </style> |
| 62 | + <div class="GuaHsu-header"> |
| 63 | + <span> |
| 64 | + <a href="https://guahsu.io/categories/JavaScript30/" target="_blank">JavaScript30 心得</a> |
| 65 | + </span> |
| 66 | + <span>|</span> |
| 67 | + <span> |
| 68 | + <a href="https://github.com/guahsu/JavaScript30" target="_blank">GitHub</a> |
| 69 | + </span> |
| 70 | + </div> |
| 71 | + |
| 72 | + <div class="wrapper"> |
| 73 | + <video class="flex" width="765" height="430" src="https://player.vimeo.com/external/194837908.sd.mp4?s=c350076905b78c67f74d7ee39fdb4fef01d12420&profile_id=164" loop controls></video> |
| 74 | + <div class="speed"> |
| 75 | + <div class="speed-bar">1×</div> |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + |
| 79 | +<script> |
| 80 | + const speed = document.querySelector('.speed'); |
| 81 | + const bar = speed.querySelector('.speed-bar'); |
| 82 | + const video = document.querySelector('.flex'); |
| 83 | + |
| 84 | + /** 滑鼠移動事件 **/ |
| 85 | + function handleMove (e) { |
| 86 | + // 取得觸發點位置(滑鼠位於整頁頂端的Y軸定位-speed框到整頁頂端的距離) |
| 87 | + const y = e.pageY - this.offsetTop; |
| 88 | + // 設定百分比(y / speed框的高度) |
| 89 | + const percent = y / this.offsetHeight; |
| 90 | + const min = 0.4; |
| 91 | + const max = 4; |
| 92 | + // 用Math.round來計算取得四捨五入的百分比值 |
| 93 | + const height = Math.round(percent * 100) + '%'; |
| 94 | + // 取得播放速率(0.4一跳,最多4倍速) |
| 95 | + const playbackRate = percent * (max - min) + min; |
| 96 | + // 調整speed-bar的樣式高度 |
| 97 | + bar.style.height = height; |
| 98 | + // 用toFixed(2)來設定最多取得小數點後兩位,顯示於speed-bar上 |
| 99 | + bar.textContent = playbackRate.toFixed(2) + 'x'; |
| 100 | + // 控制影片的速率 |
| 101 | + video.playbackRate = playbackRate; |
| 102 | + } |
| 103 | + |
| 104 | + /** 滑鼠移動 **/ |
| 105 | + speed.addEventListener('mousemove', handleMove); |
| 106 | + |
| 107 | +</script> |
| 108 | +</body> |
| 109 | +</html> |
0 commit comments