Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE HTML>
<html lang="ja">
  <head>
  	<meta charset="utf-8">
  	<title>Video player example</title>
  	<link rel="stylesheet" type="text/css" href="style.css">
  </head>
  <body>
    <div class="player">
      <video controls>
        <source src="video/sintel-short.mp4" type="video/mp4">
        <source src="video/sintel-short.webm" type="video/webm">
        <!-- fallback content here -->
      </video>
      <div class="controls">
        <input type="range" min="0" max="1" step="0.01" value="1" class="volume" />
        <button class="play" data-icon="P" aria-label="play pause toggle"></button>
        <button class="stop" data-icon="S" aria-label="stop"></button>
        <div class="timer"><div></div><span aria-label="timer">00:00</span></div>
        <button class="rwd" data-icon="B" aria-label="rewind"></button>
        <button class="fwd" data-icon="F" aria-label="fast forward"></button>
      </div>
    </div>
    <p>Sintel &copy; copyright Blender Foundation | <a href="http://www.sintel.org">www.sintel.org</a>.</p>
    <script src="custom-player.js"></script>
  </body>
</html>

              
            
!

CSS

              
                @font-face {
    font-family: 'HeydingsControlsRegular';
    src: url('fonts/heydings_controls-webfont.eot');
    src: url('fonts/heydings_controls-webfont.eot?#iefix') format('embedded-opentype'),
         url('fonts/heydings_controls-webfont.woff') format('woff'),
         url('fonts/heydings_controls-webfont.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;
}

video {
  border: 1px solid black;
}

p {
  position: absolute;
  bottom: 15px;
  right: 30px;
}

.player {
  position: absolute;
}

.controls {
  visibility: hidden;
  opacity: 0.5;
  width: 600px;
  border-radius: 10px;
  position: absolute;
  bottom: 20px;
  left: 50%;
  margin-left: -300px;
  background-color: black;
  box-shadow: 3px 3px 5px black;
  transition: 1s all;
  display: flex;
}

button, .controls {
  background: linear-gradient(to bottom,#222,#666);
}

.player:hover .controls, player:focus .controls {
  opacity: 1;
}

button:before {
  font-family: HeydingsControlsRegular;
  font-size: 20px;
  position: relative;
  content: attr(data-icon);
  color: #aaa;
  text-shadow: 1px 1px 0px black;
}

.play:before {
  font-size: 22px;
}


button, .timer {
  height: 38px;
  line-height: 19px;
  box-shadow: inset 0 -5px 25px rgba(0,0,0,0.3);
  border-right: 1px solid #333;
}

button {
  position: relative;
  border: 0;
  flex: 1;
  outline: none;
}

.play {
  border-radius: 10px 0 0 10px;
}

.fwd {
  border-radius: 0 10px 10px 0;
}

.timer {
  line-height: 38px;
  font-size: 10px;
  font-family: monospace;
  text-shadow: 1px 1px 0px black;
  color: white;
  flex: 5;
  position: relative;
}

.timer div {
  position: absolute;
  background-color: rgba(255,255,255,0.2);
  left: 0;
  top: 0;
  width: 0;
  height: 38px;
  z-index: 2;
}

.timer span {
  position: absolute;
  z-index: 3;
  left: 19px;
}

button:hover,button:focus {
  box-shadow: inset 1px 1px 2px black;
}

button:active {
  box-shadow: inset 3px 3px 2px black;
}

.active:before {
  color: red;
}

              
            
!

JS

              
                const media = document.querySelector('video');
const controls = document.querySelector('.controls')
const volumeSlider = document.querySelector('.volume');

const play = document.querySelector('.play');
const stop = document.querySelector('.stop');
const rwd = document.querySelector('.rwd');
const fwd = document.querySelector('.fwd');

const timerWrapper = document.querySelector('.timer');
const timer = document.querySelector('.timer span');
const timerBar = document.querySelector('.timer div');

media.removeAttribute("controls");
controls.style.visibility = "visible";

play.addEventListener('click', playPauseMedia);
stop.addEventListener('click', stopMedia);
media.addEventListener('ended', stopMedia);
media.addEventListener('timeupdate', setTime);
rwd.addEventListener("click", mediaBackward);
fwd.addEventListener("click", mediaForward);
timerWrapper.addEventListener('click', timerSeeking); 

volumeSlider.addEventListener('change', () => {
    media.volume = volumeSlider.value;
});

function timerSeeking (e) {
    const timerX = Math.floor(timerWrapper.getBoundingClientRect().x);
    const timerWidth = Math.floor(timerWrapper.getBoundingClientRect().width);
    
    const currentSeek = (e.x - timerX) / timerWidth;
        
    media.currentTime = Math.floor(media.duration * currentSeek);
    timerBar.style.width = `${Math.floor(timerWidth * currentSeek)}px`;
};


function playPauseMedia() {
    rwd.classList.remove('active');
    fwd.classList.remove('active');
    clearInterval(intervalRwd);
    clearInterval(intervalFwd);

    if (media.paused) {
        play.setAttribute('data-icon', 'u');
        media.play();
    } else {
        play.setAttribute('data-icon', 'P');
        media.pause();
    }
}

function stopMedia() {
    rwd.classList.remove('active');
    fwd.classList.remove('active');
    clearInterval(intervalRwd);
    clearInterval(intervalFwd);

    media.pause();
    media.currentTime = 0;
    play.setAttribute('data-icon', 'P');
}

let intervalFwd;
let intervalRwd;

function mediaBackward() {

    if (rwd.classList.contains('active')) {
        rwd.classList.remove('active');
        clearInterval(intervalRwd);
        media.play();
    } else {
        rwd.classList.add('active');
        media.pause();
        intervalRwd = setInterval(windBackward, 200);
    }
}

function mediaForward() {

    if (fwd.classList.contains('active')) {
        fwd.classList.remove('active');
        clearInterval(intervalFwd);
        media.play();
    } else {
        fwd.classList.add('active');
        media.pause();
        intervalFwd = setInterval(windForward, 200);
    }
}

function windBackward() {
    if (media.currentTime <= 3) {
        rwd.classList.remove('active');
        clearInterval(intervalRwd);
        stopMedia();
    } else {
        media.currentTime -= 3;
    }
}

function windForward() {
    if (media.currentTime >= media.duration - 3) {
        fwd.classList.remove('active');
        clearInterval(intervalFwd);
        stopMedia();
    } else {
        media.currentTime += 3;
    }
}

function setTime() {
    console.log(media.currentTime);

    const hours = Math.floor(media.currentTime / 3600);
    const minutes = Math.floor(media.currentTime / 60 - hours * 60);
    const seconds = Math.floor(media.currentTime - minutes * 60);

    const hourValue = hours.toString().padStart(2,'0');
    const minuteValue = minutes.toString().padStart(2, '0');
    const secondValue = seconds.toString().padStart(2, '0');

    let videoDur;

    const durHours = Math.floor(media.duration / 3600).toString().padStart(2, '0').toString().padStart(2, '0');
    const durMinutes = Math.floor(media.duration / 60 - durHours * 60).toString().padStart(2, '0').toString().padStart(2, '0');
    const durSeconds = Math.floor(media.duration - durMinutes * 60).toString().padStart(2, '0').toString().padStart(2, '0');

    if (media.duration > 3600) {
        durSecond = durSeconds - (durHours * 3600);
        videoDur = `${durHours}:${durMinutes}:${durSecond.toString().padStart(2, '0')}`;
    } else {
        videoDur = `${durMinutes}:${durSeconds}`;
    }

    console.log(hourValue, minuteValue, secondValue);

    if (hours > 0) {
        const second = secondValue - (hours * 3600);
        const mediaTime = `${hourValue}:${minuteValue}:${second.toString().padStart(2, '0')}`;
        timer.textContent = mediaTime +" / "+ videoDur;
    } else {
        const mediaTime = `${minuteValue}:${secondValue}`;
        timer.textContent = mediaTime +" / "+ videoDur;  
    };
    const barLength = 
    timerWrapper.clientWidth * (media.currentTime / media.duration);
    timerBar.style.width = `${barLength}px`;
}
              
            
!
999px

Console