• We just launched and are currently in beta. Join us as we build and grow the community.

Creating a Video Custom Controls using HTML, CSS, and JS Tutorial

shady19956

Access Control Auditor
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
50
Likes
108
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial, you can learn to create Video Custom Controls using HTML, CSS, and Pure JavaScript. The tutorial aims to provide students and beginners with a reference for learning the JS Web Video API to control the video element. Here, I will be providing simple web page scripts that demonstrate the creation of custom control of the video element.

What are Custom Video Custom Controls?

Basically, the HTML Video element has built-in controls for the video by adding a controls attribute to the element. Custom Video Controls are custom controls that the developer implemented to provide the end-users with more interactive and useful controls that are relevant only to the website.

How to Create a Custom Video Custom Controls?

Custom Video Custom Controls can be achieved simply by using HTML, CSS, and JavaScript. We can simply create the controls using some HTML elements such as div tags and design them using some CSS properties on how we prefer the controls should be looking. Then, using the JS Web Video API methods, we can add some event listeners to the controls we created and manage the video such as playing or pausing the video, changing the video's current play time, changing video volume, and more. Check out the source code scripts that I created and provided below to understand it more.

Sample Web Page Scripts

The following scripts result in a simple web page that contains a video element and custom controls. The custom controls contain a duration slider, volume, slider, backward (-10s) button, forward (+10s), play/pause button, current time text, and remain time text.

Page Interface

Here's the HTML file script known as index.html. The file contains the page layout, video, and custom controls elements. Please don't forget to change the video tag element source path with a video path that is available on your end.

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  6. <title

    >

    Video Custom Control</

    title

    >
  7. <link

    rel

    =

    "stylesheet"

    href

    =

    "style.css"

    >
  8. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.googleapis.com"

    >
  9. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.gstatic.com"

    crossorigin>
  10. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200"

    /

    >
  11. </

    head

    >
  12. <body

    >
  13. <div

    class

    =

    "container"

    >
  14. <h1

    id

    =

    "page-title"

    >

    Video Custom Control using HTML, CSS, and JS</

    h1

    >
  15. <hr

    id

    =

    "title_hr"

    >
  16. <div

    id

    =

    "vid-wrapper"

    >
  17. <video

    src

    =

    "./demo_video.mp4"

    ></

    video

    >
  18. <div

    id

    =

    "custom-controls"

    >
  19. <div

    id

    =

    "vid-slider"

    >
  20. <span

    id

    =

    "current-time"

    >

    00:00</

    span

    >
  21. <span

    id

    =

    "vid-slider-holder"

    >
  22. <input

    type

    =

    "range"

    step

    =

    "any"

    min

    =

    "0"

    max

    =

    "100"

    id

    =

    "vid_duration"

    value

    =

    "0"

    >
  23. </

    span

    >
  24. <span

    id

    =

    "remaining-time"

    >

    00:00</

    span

    >
  25. </

    div

    >
  26. <div

    class

    =

    "other-ctrls"

    >
  27. <div

    class

    =

    "btns"

    >
  28. <div

    id

    =

    "minus-10-sec-btn"

    title

    =

    "10s Backward"

    ><span

    class

    =

    "material-symbols-outlined"

    >

    undo</

    span

    ></

    div

    >
  29. <div

    id

    =

    "pause-play-btn"

    title

    =

    "Play"

    ><span

    class

    =

    "material-symbols-outlined"

    >

    play_arrow</

    span

    ></

    div

    >
  30. <div

    id

    =

    "plus-10-sec-btn"

    title

    =

    "10s Forward"

    ><span

    class

    =

    "material-symbols-outlined"

    >

    redo</

    span

    ></

    div

    >
  31. </

    div

    >
  32. <div

    id

    =

    "vol-slider-holder"

    >
  33. <span

    class

    =

    "material-symbols-outlined"

    >

    volume_up</

    span

    >
  34. <input

    type

    =

    "range"

    step

    =

    "any"

    min

    =

    "0"

    max

    =

    "10"

    value

    =

    "10"

    id

    =

    "vol-slider"

    >
  35. </

    div

    >
  36. </

    div

    >
  37. </

    div

    >
  38. </

    div

    >
  39. </

    div

    >

  40. <script

    src

    =

    "script.js"

    ></

    script

    >
  41. </

    body

    >
  42. </

    html

    >

Stylesheet (CSS)

Next, here's the CSS file script known as style.css. The file contains the page layout and custom controls elements design or stylesheet codes.

  1. @import

    url

    (

    'https://fonts.googleapis.com/css2?f...00;1,200;1,300;1,400;1,500;1,600&display=swap" rel="stylesheet'

    )

    ;
  2. *

    {
  3. margin

    :

    0

    ;
  4. padding

    :

    0

    ;
  5. box-sizing

    :

    border-box

    ;
  6. font-family

    :

    'Dongle'

    ,

    sans-serif

    ;
  7. font-family

    :

    'Roboto Mono'

    ,

    monospace

    ;
  8. }
  9. ::

    selection

    {
  10. color

    :

    #fff

    ;
  11. background

    :

    #4db2ec

    ;
  12. }
  13. body{
  14. display

    :

    flex;
  15. align-items

    :

    center

    ;
  16. justify-content

    :

    center

    ;
  17. min-height

    :

    100vh

    ;
  18. background

    :

    #4facfe

    ;
  19. background-image

    :

    linear-gradient(

    to right

    ,

    #4facfe

    0%

    ,

    #00f2fe

    100%

    )

    ;
  20. padding

    :

    2em

    0

    ;
  21. }
  22. #page-title

    {
  23. color

    :

    #fff

    ;
  24. text-align

    :

    center

    ;
  25. font-weight

    :

    500

    ;
  26. text-shadow

    :

    0px

    0px

    15px

    #0000003a

    ;
  27. }
  28. #title_hr

    {
  29. width

    :

    60px

    ;
  30. border

    :

    2px

    solid

    #ffffff

    ;
  31. margin

    :

    .35em

    auto

    ;
  32. }
  33. @media

    (

    min-width

    :

    780px

    )

    {
  34. #page-title

    {
  35. width

    :

    780px

    ;
  36. }
  37. }
  38. /* Table Wrapper */
  39. #vid-wrapper

    {
  40. width

    :

    700px

    ;
  41. background-color

    :

    #000

    ;
  42. border

    :

    1px

    solid

    #2e2e2e

    ;
  43. padding

    :

    1em

    .75em

    ;
  44. margin

    :

    1em

    auto

    ;
  45. border-radius

    :

    5px

    ;
  46. box-shadow

    :

    1px

    1px

    10px

    #00000046

    ;
  47. }
  48. /* Video Element */
  49. #vid-wrapper

    video{
  50. width

    :

    100%

    ;
  51. height

    :

    350px

    ;
  52. object-fit

    :

    scale-down;
  53. object-position

    :

    center

    center

    ;
  54. }
  55. /* Video Duration Slider */
  56. div#vid-slider

    {
  57. color

    :

    #fff

    ;
  58. display

    :

    flex;
  59. width

    :

    100%

    ;
  60. justify-content

    :

    center

    ;
  61. align-items

    :

    center

    ;
  62. }
  63. span#current-time

    ,
  64. span#remaining-time

    {
  65. display

    :

    block

    ;
  66. flex-shrink

    :

    1

    ;
  67. }
  68. #vid-slider-holder

    {
  69. display

    :

    block

    ;
  70. flex-grow

    :

    1

    ;
  71. padding

    :

    0

    .35em

    ;
  72. }
  73. #vid-slider-holder

    input{
  74. width

    :

    100%

    ;
  75. }
  76. /* duration range */
  77. #vid-slider-holder

    input {
  78. -webkit-appearance:

    none

    ;
  79. appearance:

    none

    ;
  80. width

    :

    100%

    ;
  81. cursor

    :

    pointer

    ;
  82. outline

    :

    none

    ;
  83. border-radius

    :

    15px

    ;
  84. height

    :

    6px

    ;
  85. background

    :

    #645a5a

    ;
  86. }

  87. #vid-slider-holder

    input::

    -webkit-slider-thumb {
  88. -webkit-appearance:

    none

    ;
  89. appearance:

    none

    ;
  90. height

    :

    15px

    ;
  91. width

    :

    15px

    ;
  92. background-color

    :

    #eeeeee

    ;
  93. border-radius

    :

    50%

    ;
  94. border

    :

    1px

    solid

    #c2c1c1

    ;
  95. transition

    :

    .2s

    ease-in-out;
  96. }

  97. #vid-slider-holder

    input::

    -moz-range-thumb {
  98. height

    :

    15px

    ;
  99. width

    :

    15px

    ;
  100. background-color

    :

    #eeeeee

    ;
  101. border-radius

    :

    50%

    ;
  102. border

    :

    none

    ;
  103. box-shadow

    :

    0px

    0px

    3px

    #000

    ;
  104. transition

    :

    .2s

    ease-in-out;
  105. }

  106. #vid-slider-holder

    input::

    -webkit-slider-thumb:

    hover

    {
  107. box-shadow

    :

    0

    0

    0

    10px

    #2aaaff10
  108. }
  109. #vid-slider-holder

    input:

    focus

    ::

    -webkit-slider-thumb {
  110. box-shadow

    :

    0

    0

    0

    13px

    #2aaaff1a
  111. }

  112. #vid-slider-holder

    input::

    -moz-range-thumb:

    hover

    {
  113. box-shadow

    :

    0

    0

    0

    10px

    #2aaaff10
  114. }
  115. #vid-slider-holder

    input:

    focus

    ::

    -moz-range-thumb {
  116. box-shadow

    :

    0

    0

    0

    13px

    #2aaaff1a
  117. }

  118. /* Btns and other controls */
  119. .other-ctrls

    {
  120. display

    :

    flex;
  121. width

    :

    100%

    ;
  122. color

    :

    #fff

    ;
  123. align-items

    :

    center

    ;
  124. justify-content

    :

    space-between;
  125. }
  126. .other-ctrls

    .btns{
  127. display

    :

    flex;
  128. align-items

    :

    center

    ;
  129. justify-content

    :

    center

    ;
  130. flex-shrink

    :

    1

    ;
  131. }
  132. div#minus-10-sec-btn

    ,
  133. div#pause-play-btn

    ,
  134. div#plus-10-sec-btn

    {
  135. width

    :

    20px

    ;
  136. height

    :

    20px

    ;
  137. margin

    :

    0.35em

    0.5em

    ;
  138. cursor

    :

    pointer

    ;
  139. }
  140. div#minus-10-sec-btn

    :

    hover>

    span,
  141. div#pause-play-btn

    :

    hover>

    span,
  142. div#plus-10-sec-btn

    :

    hover>

    span {
  143. color

    :

    #cccccc

    !important;
  144. }

  145. /* Volume Slider */
  146. #vol-slider-holder

    {
  147. width

    :

    150px

    ;
  148. display

    :

    flex;
  149. align-items

    :

    center

    ;
  150. justify-content

    :

    center

    ;
  151. }

  152. #vol-slider

    {
  153. -webkit-appearance:

    none

    ;
  154. appearance:

    none

    ;
  155. width

    :

    100%

    ;
  156. cursor

    :

    pointer

    ;
  157. outline

    :

    none

    ;
  158. border-radius

    :

    15px

    ;
  159. height

    :

    6px

    ;
  160. background

    :

    #645a5a

    ;
  161. }

  162. #vol-slider

    ::

    -webkit-slider-thumb {
  163. -webkit-appearance:

    none

    ;
  164. appearance:

    none

    ;
  165. height

    :

    15px

    ;
  166. width

    :

    15px

    ;
  167. background-color

    :

    #eeeeee

    ;
  168. border-radius

    :

    50%

    ;
  169. border

    :

    1px

    solid

    #c2c1c1

    ;
  170. transition

    :

    .2s

    ease-in-out;
  171. }

  172. #vol-slider

    ::

    -moz-range-thumb {
  173. height

    :

    15px

    ;
  174. width

    :

    15px

    ;
  175. background-color

    :

    #eeeeee

    ;
  176. border-radius

    :

    50%

    ;
  177. border

    :

    none

    ;
  178. box-shadow

    :

    0px

    0px

    3px

    #000

    ;
  179. transition

    :

    .2s

    ease-in-out;
  180. }

  181. #vol-slider

    ::

    -webkit-slider-thumb:

    hover

    {
  182. box-shadow

    :

    0

    0

    0

    10px

    #2aaaff10
  183. }
  184. #vol-slider

    :

    focus

    ::

    -webkit-slider-thumb {
  185. box-shadow

    :

    0

    0

    0

    13px

    #2aaaff1a
  186. }

  187. #vol-slider

    ::

    -moz-range-thumb:

    hover

    {
  188. box-shadow

    :

    0

    0

    0

    10px

    #2aaaff10
  189. }
  190. #vol-slider

    :

    focus

    ::

    -moz-range-thumb {
  191. box-shadow

    :

    0

    0

    0

    13px

    #2aaaff1a
  192. }

JavaScript

Finally, here's the JavaScript file script known as script.js. The file contains the JS codes that make the custom controls functional and control the video element.

  1. // Select Video Element
  2. const

    video =

    document.querySelector

    (

    "#vid-wrapper video"

    )
  3. // Select Duration Slider Element
  4. const

    durationSliderEl =

    document.querySelector

    (

    "#vid-slider-holder input"

    )
  5. // Select Volume Slider Element
  6. const

    volumeSliderEl =

    document.querySelector

    (

    "#vol-slider"

    )
  7. // Select Backward icon Element
  8. const

    backward =

    document.getElementById

    (

    'minus-10-sec-btn'

    )
  9. // Select Forward icon Element
  10. const

    forward =

    document.getElementById

    (

    'plus-10-sec-btn'

    )
  11. // Select Play/Pause icon Element
  12. const

    playPause =

    document.getElementById

    (

    'pause-play-btn'

    )
  13. // Select Current Time Text Element
  14. const

    vidCurrentTime =

    document.getElementById

    (

    'current-time'

    )
  15. // Select Remaining Time Text Element
  16. const

    vidRemainTime =

    document.getElementById

    (

    'remaining-time'

    )
  17. // for duration interval
  18. let durationInterval;

  19. // Update duration slider fill color
  20. const

    durationSlider=

    (

    )

    =>

    {
  21. // get progress
  22. const

    progress =

    (

    durationSliderEl.value

    /

    durationSliderEl.max

    )

    *

    100

    ;

  23. // Update Fill Color depending to progress
  24. durationSliderEl.style

    .background

    =

    `linear-

    gradient(

    to right,

    #3aa8ff ${

    progress}

    %,

    #645a5a ${

    progress}

    %

    )

    `;
  25. }
  26. // Update volume slider fill color
  27. const

    volumeSlider =

    (

    )

    =>

    {
  28. // get progress
  29. const

    progress =

    (

    volumeSliderEl.value

    /

    volumeSliderEl.max

    )

    *

    100

    ;

  30. // Update Fill Color depending to progress
  31. volumeSliderEl.style

    .background

    =

    `linear-

    gradient(

    to right,

    #3aa8ff ${

    progress}

    %,

    #645a5a ${

    progress}

    %

    )

    `;
  32. }

  33. // Update Duration Slider, current time, and remaining time elements
  34. const

    updateDuration =

    (

    )

    =>

    {
  35. // get current time
  36. var

    currentTime =

    video.currentTime
  37. // get video total duration
  38. var

    totalTime =

    video.duration
  39. // Compute Remaining Time
  40. var

    remain =

    totalTime -

    currentTime
  41. // Compute duration slider value
  42. var

    sliderValue =

    (

    currentTime /

    totalTime)

    *

    100

    ;
  43. // Reset Duration Slider Value
  44. durationSliderEl.value

    =

    sliderValue
  45. // update duration slider
  46. durationSlider(

    )
  47. // compute Current Time (m:s)
  48. var

    curtimeMins =

    Math

    .floor

    (

    currentTime /

    60

    )
  49. var

    curtimeSecs =

    Math

    .floor

    (

    (

    (

    currentTime /

    60

    )

    -

    curtimeMins)

    *

    60

    )
  50. // compute Remaining Time (m:s)
  51. var

    remainTimeMins =

    Math

    .floor

    (

    remain /

    60

    )
  52. var

    remainTimeSecs =

    Math

    .floor

    (

    (

    (

    remain /

    60

    )

    -

    remainTimeMins)

    *

    60

    )
  53. // make current time minimum 2 digits and add 0 before the digit if only single digits
  54. curtimeMins =

    String

    (

    curtimeMins)

    .padStart

    (

    2

    ,

    0

    )

    ;
  55. curtimeSecs =

    String

    (

    curtimeSecs)

    .padStart

    (

    2

    ,

    0

    )

    ;
  56. // make remaining time minimum 2 digits and add 0 before the digit if only single digits
  57. remainTimeMins =

    String

    (

    remainTimeMins)

    .padStart

    (

    2

    ,

    0

    )

    ;
  58. remainTimeSecs =

    String

    (

    remainTimeSecs)

    .padStart

    (

    2

    ,

    0

    )

    ;
  59. // Update current Time Element Text
  60. vidCurrentTime.innerText

    =

    `${

    curtimeMins}

    :

    ${

    curtimeSecs}

    `
  61. // Update remaining Time Element Text
  62. vidRemainTime.innerText

    =

    `${

    remainTimeMins}

    :

    ${

    remainTimeSecs}

    `
  63. }
  64. window.onload

    =

    (

    )

    =>

    {
  65. volumeSlider(

    )
  66. updateDuration(

    )
  67. playPause.addEventListener

    (

    'click'

    ,

    e=>

    {
  68. e.preventDefault

    (

    )
  69. if

    (

    !!

    playPause.dataset

    .playing

    )

    {
  70. delete

    playPause.dataset

    .playing

    ;
  71. video.pause

    (

    )
  72. playPause.innerHTML

    =

    `<

    span class

    =

    "material-symbols-outlined"

    >

    play_arrow</

    span>

    `
  73. }

    else

    {
  74. playPause.dataset

    .playing

    =

    'true'
  75. video.play

    (

    )
  76. playPause.innerHTML

    =

    `<

    span class

    =

    "material-symbols-outlined"

    >

    pause</

    span>

    `
  77. }
  78. }

    )
  79. video.addEventListener

    (

    'play'

    ,

    e =>

    {
  80. durationInterval =

    setInterval(

    (

    )

    =>

    {
  81. updateDuration(

    )
  82. }

    ,

    100

    )
  83. }

    )
  84. video.addEventListener

    (

    'pause'

    ,

    e =>

    {
  85. if

    (

    durationInterval !=

    undefined

    )

    {
  86. clearInterval(

    durationInterval)
  87. }
  88. }

    )
  89. backward.addEventListener

    (

    'click'

    ,

    e=>

    {
  90. e.preventDefault

    (

    )
  91. var

    currentTime =

    video.currentTime
  92. currentTime -=

    10

    ;
  93. currentTime =

    (

    currentTime <

    0

    )

    ?

    0

    :

    currentTime
  94. video.currentTime

    =

    currentTime
  95. updateDuration(

    )
  96. }

    )
  97. forward.addEventListener

    (

    'click'

    ,

    e=>

    {
  98. e.preventDefault

    (

    )
  99. var

    currentTime =

    video.currentTime
  100. currentTime +=

    10

    ;
  101. currentTime =

    (

    currentTime >

    video.duration

    )

    ?

    video.duration

    :

    currentTime
  102. video.currentTime

    =

    currentTime
  103. updateDuration(

    )
  104. }

    )
  105. durationSliderEl.addEventListener

    (

    "input"

    ,

    (

    event)

    =>

    {
  106. durationSlider(

    )
  107. var

    duration =

    video.duration
  108. var

    newCurrentTime =

    (

    durationSliderEl.value

    /

    100

    )

    *

    duration
  109. video.currentTime

    =

    newCurrentTime
  110. updateDuration(

    )
  111. }

    )
  112. volumeSliderEl.addEventListener

    (

    "input"

    ,

    (

    event)

    =>

    {
  113. volumeSlider(

    )
  114. var

    vol =

    (

    volumeSliderEl.value

    /

    10

    )
  115. video.volume

    =

    vol
  116. }

    )
  117. video.addEventListener

    (

    "ended"

    ,

    e=>

    {
  118. delete

    playPause.dataset

    .playing

    ;
  119. video.pause

    (

    )
  120. playPause.innerHTML

    =

    `<

    span class

    =

    "material-symbols-outlined"

    >

    play_arrow</

    span>

    `
  121. video.currentTime

    =

    0
  122. }

    )
  123. }

Snapshots

Here are some snapshots of the overall result of the web page scripts I have provided.

Page UI

Video and Custom Control Block

Custom Controls

Video Block when Playing

There you go! I have also provided the complete source code zip file of the web page scripts on this website and it is free to download. The download button is located below this tutorial's content. Feel free to download and modify the source code the way you desire.

DEMO VIDEO

That's it! I hope this Creating a Video Custom Controls using HTML, CSS, and JS Tutorial will help you with what you are looking for and you'll find this useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

336,529

336,537

Top