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

Creating a Video Tag's Custom Controls using jQuery Tutorial

jeromearjoe

Global Connectivity Strategist
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
135
Likes
24
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial, you will learn how to create a Custom Controls for the HTML Video Elements. here, you will know how to create a custom play/pause button, stop button, playback slider, and volume slider. The custom controls functionalities can be achieve using JavaScript and jQuery Library.

Getting Started

Download Bootstrap v5 and jQuery Library. After that compile the libraries in a directory that you will use for storing your source code.

Creating The Custom Style Sheet

Below is the a simple CSS (Cascading Style Sheet) script for the custom design of some part of the application. Save this file as custom.css

  1. :

    root

    {
  2. --bs-success-rgb

    :

    71

    ,

    222

    ,

    152

    !important;
  3. }

  4. html,
  5. body {
  6. height

    :

    100%

    ;
  7. width

    :

    100%

    ;
  8. font-family

    :

    Apple Chancery,

    cursive

    ;
  9. }

  10. .btn-info

    .text-light

    :

    hover

    ,
  11. .btn-info

    .text-light

    :

    focus

    {
  12. background

    :

    #000

    ;
  13. }

  14. #vid-el

    {
  15. height

    :

    60vh

    ;
  16. background

    :

    linear-gradient(

    0deg

    ,

    #3c3b3b

    ,

    #424242

    ,

    #000000

    )

    ;
  17. object-fit

    :

    unset;
  18. object-position

    :

    center

    center

    ;
  19. }

  20. #range-holder

    {
  21. width

    :

    45vw

    ;
  22. }

  23. #playBackSlider

    {
  24. overflow

    :

    hidden

    ;
  25. height

    :

    5px

    ;
  26. -webkit-appearance:

    none

    !important;
  27. width

    :

    100%

    ;
  28. background

    :

    #616060

    ;
  29. }

  30. #playBackSlider

    ::

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

    none

    ;
  32. width

    :

    10px

    ;
  33. height

    :

    10px

    ;
  34. cursor

    :

    e-resize

    ;
  35. background

    :

    #ff0000

    ;
  36. box-shadow

    :

    -45vw

    1px

    1px

    45vw

    #ee3d3d

    ;
  37. }

Creating The Interface

In the script below, it contains all the elements code script such as the video, buttons, range/slider, and more. Save this file as index.html.

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >

  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

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

    >
  7. <title

    >

    Video Custom Controls</

    title

    >
  8. <link

    rel

    =

    "stylesheet"

    href

    =

    "./Font-Awesome-master/css/all.min.css"

    >
  9. <link

    rel

    =

    "stylesheet"

    href

    =

    "./css/bootstrap.min.css"

    >
  10. <link

    rel

    =

    "stylesheet"

    href

    =

    "./css/custom.css"

    >
  11. <script

    src

    =

    "./js/jquery-3.6.0.min.js"

    ></

    script

    >
  12. <script

    src

    =

    "./js/bootstrap.min.js"

    ></

    script

    >
  13. <script

    src

    =

    "./js/script.js"

    ></

    script

    >
  14. </

    head

    >

  15. <body

    class

    =

    "bg-light"

    >
  16. <nav class

    =

    "navbar navbar-expand-lg navbar-dark bg-dark bg-gradient"

    id

    =

    "topNavBar"

    >
  17. <div

    class

    =

    "container"

    >
  18. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >
  19. Sourcecodester
  20. </

    a

    >

  21. <div

    >
  22. <b

    class

    =

    "text-light"

    >

    Video Custom Controls</

    b

    >
  23. </

    div

    >
  24. </

    div

    >
  25. </

    nav>
  26. <div

    class

    =

    "container py-5"

    id

    =

    "page-container"

    >
  27. <!-- Video Container -->
  28. <div

    id

    =

    "vid-container"

    >
  29. <video src

    =

    "sample-mp4-file.mp4"

    id

    =

    "vid-el"

    class

    =

    "w-100"

    ></

    video>
  30. <h4

    ><b

    >

    Custom Controls</

    b

    ></

    h4

    >
  31. <!-- Controls -->
  32. <div

    class

    =

    "d-flex"

    >
  33. <div

    class

    =

    "mx-1"

    >
  34. <button

    class

    =

    "btn btn-sm btn-dark text-light"

    id

    =

    "play-btn"

    data-value

    =

    "play"

    ><i

    class

    =

    "fa fa-play"

    ></

    i

    ></

    button

    >
  35. </

    div

    >
  36. <div

    class

    =

    "mx-1"

    >
  37. <button

    class

    =

    "btn btn-sm btn-dark text-light"

    id

    =

    "stop-btn"

    ><i

    class

    =

    "fa fa-stop"

    ></

    i

    ></

    button

    >
  38. </

    div

    >

  39. <div

    class

    =

    "mx-1"

    >
  40. <span

    id

    =

    "currentTime"

    >

    --:--</

    span

    >
  41. </

    div

    >
  42. <div

    id

    =

    "range-holder"

    class

    =

    "mx-1"

    >
  43. <input

    type

    =

    "range"

    id

    =

    "playBackSlider"

    value

    =

    "0"

    >
  44. </

    div

    >
  45. <div

    class

    =

    "mx-1"

    >
  46. <span

    id

    =

    "duration"

    >

    --:--</

    span

    >
  47. </

    div

    >
  48. <div

    class

    =

    "mx-1"

    >
  49. <span

    id

    =

    "vol-icon"

    ><i

    class

    =

    "fa fa-volume-up"

    ></

    i

    ></

    span

    >

    <input

    type

    =

    "range"

    value

    =

    "100"

    id

    =

    "volume"

    >
  50. </

    div

    >
  51. </

    div

    >
  52. <!-- End of Controls -->
  53. </

    div

    >
  54. <!-- End of Video Container -->
  55. </

    div

    >


  56. </

    body

    >

  57. </

    html

    >

Creating the Main Functions

Below are the codes that makes the video buttons and controls functional. Save this file as script.js.

  1. var

    _video_el;
  2. var

    slider;

  3. $(

    function

    (

    )

    {
  4. _video_el =

    $(

    '#vid-el'

    )

    [

    0

    ]
  5. setTimeout(

    (

    )

    =>

    {
  6. $(

    '#duration'

    )

    .text

    (

    String

    (

    Math

    .floor

    (

    _video_el.duration

    /

    60

    )

    )

    .padStart

    (

    2

    ,

    '0'

    )

    +

    ":"

    +

    String

    (

    Math

    .floor

    (

    60

    *

    Math

    .abs

    (

    (

    _video_el.duration

    /

    60

    )

    -

    Math

    .floor

    (

    _video_el.duration

    /

    60

    )

    )

    )

    )

    .padStart

    (

    2

    ,

    '0'

    )

    )
  7. }

    ,

    500

    )
  8. // Pause/Play Video
  9. $(

    '#play-btn'

    )

    .click

    (

    function

    (

    )

    {
  10. var

    val =

    $(

    this

    )

    .attr

    (

    'data-value'

    )
  11. if

    (

    val ==

    'play'

    )

    {
  12. _video_el.play

    (

    )
  13. slider =

    setInterval(

    (

    )

    =>

    {
  14. var

    current_slide =

    (

    _video_el.currentTime

    /

    _video_el.duration

    )

    *

    100

    ;
  15. $(

    '#playBackSlider'

    )

    .val

    (

    current_slide)
  16. $(

    '#currentTime'

    )

    .text

    (

    String

    (

    Math

    .floor

    (

    _video_el.currentTime

    /

    60

    )

    )

    .padStart

    (

    2

    ,

    '0'

    )

    +

    ":"

    +

    String

    (

    Math

    .floor

    (

    60

    *

    Math

    .abs

    (

    (

    _video_el.currentTime

    /

    60

    )

    -

    Math

    .floor

    (

    _video_el.currentTime

    /

    60

    )

    )

    )

    )

    .padStart

    (

    2

    ,

    '0'

    )

    )
  17. }

    ,

    500

    )
  18. $(

    this

    )

    .html

    (

    '<i class="fa fa-pause"></i>'

    )
  19. $(

    this

    )

    .attr

    (

    'data-value'

    ,

    'pause'

    )

    ;
  20. }

    else

    {
  21. _video_el.pause

    (

    )
  22. $(

    this

    )

    .html

    (

    '<i class="fa fa-play"></i>'

    )
  23. $(

    this

    )

    .attr

    (

    'data-value'

    ,

    'play'

    )

    ;
  24. clearInterval(

    slider)
  25. }
  26. }

    )
  27. // Stop Video
  28. $(

    '#stop-btn'

    )

    .click

    (

    function

    (

    )

    {
  29. $(

    '#play-btn'

    )

    .html

    (

    '<i class="fa fa-play"></i>'

    )
  30. $(

    '#play-btn'

    )

    .attr

    (

    'data-value'

    ,

    'play'

    )

    ;
  31. _video_el.pause

    (

    )
  32. _video_el.currentTime

    =

    0

    ;
  33. $(

    '#playBackSlider'

    )

    .val

    (

    0

    )
  34. clearInterval(

    slider)
  35. $(

    '#currentTime'

    )

    .text

    (

    String

    (

    Math

    .floor

    (

    _video_el.currentTime

    /

    60

    )

    )

    .padStart

    (

    2

    ,

    '0'

    )

    +

    ":"

    +

    String

    (

    Math

    .floor

    (

    60

    *

    Math

    .abs

    (

    (

    _video_el.currentTime

    /

    60

    )

    -

    Math

    .floor

    (

    _video_el.currentTime

    /

    60

    )

    )

    )

    )

    .padStart

    (

    2

    ,

    '0'

    )

    )
  36. }

    )

  37. // Playback Slider Moved Function
  38. $(

    '#playBackSlider'

    )

    .on

    (

    'mousedown'

    ,

    function

    (

    )

    {
  39. $(

    this

    )

    .on

    (

    'mousemove'

    ,

    function

    (

    )

    {
  40. _video_el.pause

    (

    )
  41. _video_el.currentTime

    =

    (

    _video_el.duration

    *

    (

    $(

    this

    )

    .val

    (

    )

    /

    100

    )

    )

    ;
  42. $(

    '#currentTime'

    )

    .text

    (

    String

    (

    Math

    .floor

    (

    _video_el.currentTime

    /

    60

    )

    )

    .padStart

    (

    2

    ,

    '0'

    )

    +

    ":"

    +

    String

    (

    Math

    .floor

    (

    60

    *

    Math

    .abs

    (

    (

    _video_el.currentTime

    /

    60

    )

    -

    Math

    .floor

    (

    _video_el.currentTime

    /

    60

    )

    )

    )

    )

    .padStart

    (

    2

    ,

    '0'

    )

    )
  43. clearInterval(

    slider)
  44. }

    )

  45. }

    )
  46. $(

    '#playBackSlider'

    )

    .on

    (

    'mouseup'

    ,

    function

    (

    )

    {
  47. $(

    this

    )

    .off

    (

    'mousemove'

    )
  48. _video_el.currentTime

    =

    (

    _video_el.duration

    *

    (

    $(

    this

    )

    .val

    (

    )

    /

    100

    )

    )

    ;
  49. $(

    '#currentTime'

    )

    .text

    (

    String

    (

    Math

    .floor

    (

    _video_el.currentTime

    /

    60

    )

    )

    .padStart

    (

    2

    ,

    '0'

    )

    +

    ":"

    +

    String

    (

    Math

    .floor

    (

    60

    *

    Math

    .abs

    (

    (

    _video_el.currentTime

    /

    60

    )

    -

    Math

    .floor

    (

    _video_el.currentTime

    /

    60

    )

    )

    )

    )

    .padStart

    (

    2

    ,

    '0'

    )

    )
  50. if

    (

    $(

    '#play-btn'

    )

    .attr

    (

    'data-value'

    )

    ==

    'pause'

    )

    {
  51. _video_el.play

    (

    )
  52. slider =

    setInterval(

    (

    )

    =>

    {
  53. var

    current_slide =

    (

    _video_el.currentTime

    /

    _video_el.duration

    )

    *

    100

    ;
  54. $(

    '#playBackSlider'

    )

    .val

    (

    current_slide)
  55. }

    ,

    500

    )
  56. }
  57. }

    )

  58. // volume slider
  59. $(

    '#volume'

    )

    .on

    (

    'mousedown'

    ,

    function

    (

    e)

    {
  60. $(

    this

    )

    .on

    (

    'mousemove'

    ,

    function

    (

    )

    {
  61. var

    vol =

    $(

    this

    )

    .val

    (

    )

    /

    100
  62. _video_el.volume

    =

    vol
  63. if

    (

    vol ==

    0

    )

    {
  64. $(

    '#vol-icon'

    )

    .html

    (

    '<i class="fa fa-volume-off"></i>'

    )
  65. }

    else

    if

    (

    vol <

    .5)

    {
  66. $(

    '#vol-icon'

    )

    .html

    (

    '<i class="fa fa-volume-down"></i>'

    )
  67. }

    else

    {
  68. $(

    '#vol-icon'

    )

    .html

    (

    '<i class="fa fa-volume-up"></i>'

    )
  69. }
  70. }

    )
  71. }

    )
  72. $(

    '#volume'

    )

    .on

    (

    'mouseup'

    ,

    function

    (

    e)

    {
  73. $(

    this

    )

    .off

    (

    'mousemove'

    )
  74. }

    )

  75. }

    )

That's it! You can now test the application in your browser and see if we have met our goal on this tutorial. If ever you have encountered any errors, please review your source code by differentiating it from the source code I provided above. You can also test the working source code I created for this tutorial. You can download the source code zip file below this article.

DEMO VIDEO

That is the end of this tutorial. I hope you'll find this Video Element's Custom Controls useful for your current and future projects.

Happy Coding :)


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

452,292

323,526

323,535

Top