Receiving inaccurate video duration when the input bar is modified

Initially, I am uncertain whether the issue stems from Javascript or CSS. The code in question involves displaying the corresponding video time when a user hovers over an input bar.

The calculation utilized is as follows:

((user mouseX position / input width) * total video seconds)
. Although the correct time appears on mouse hover, clicking yields a different result. Identifying whether this discrepancy is due to a JS or CSS bug remains unclear.

Below is the code snippet for review:


document.getElementById('slider').addEventListener('mousemove', function(e) {
   showTooltip(e);
});

document.getElementById('slider').addEventListener('input', function(e) {
   document.getElementById('video').currentTime = document.getElementById('slider').value
   document.getElementById('text').innerText = CurrentTime(document.getElementById('video').currentTime);
});

function CurrentTime(v) {
    let hrs = Math.floor(v / 3600);
    let mins = Math.floor(v / 60) % 60;
    let secs = Math.floor(v) % 60;
    let ret = "";
    if (hrs > 0) {
         ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
  }
  ret += "" + mins + ":" + (secs < 10 ? "0" : "");
  ret += "" + secs;
  return ret;
}
            
function showTooltip(e) {
    // ((user mouseX position / input width) * total video seconds)
    let aa = (e.offsetX / e.target.clientWidth) * (e.target.getAttribute('max'));
       slidertitle.innerText = CurrentTime(aa);
}

let left = document.getElementById('container').getBoundingClientRect().left - document.documentElement.getBoundingClientRect().left;
window.onmousemove = function (e) {
    let x = ((e.clientX + window.pageXOffset) - left) + 'px';
  slidertitle.style.left = (x);
}   
...

Answer №1

The range slider displays incorrect values due to the extended margin or border, which is not accounted for in the JS offset calculation.
The actual value of the slider does not include this margin or border.

Calculating the margin/border at the edges is not possible, posing a challenge.
The ONLY SOLUTION currently is to set the slider value equal to the extended value detected on hover. While not ideal, it is the only feasible solution.
Check out this link and hope for future updates from HTML, CSS, JS that may address this issue. In the meantime, follow the workaround mentioned above.

var valueHover = 0;
function calcSliderPos(e) {
   // ((user mouseX position / input width) * total video seconds)
  return (e.offsetX / e.target.clientWidth) *  parseInt(e.target.getAttribute('max'),10);
}

document.getElementById('slider').addEventListener('mousemove', function(e){
  valueHover = calcSliderPos(e).toFixed(2);

  //value max,min limit
  valueHover= valueHover>e.target.getAttribute('max')?e.target.getAttribute('max'):valueHover;
  valueHover= valueHover<0?0:valueHover;

  //show tooltip
  showTooltip(e);
  document.getElementById('slider').addEventListener('input', function(e) {
  // THE ONLY SOLUTION
  e.target.value = valueHover;

  // just your code,no edits here
  document.getElementById('video').currentTime = valueHover
  document.getElementById('text').innerText = CurrentTime(document.getElementById('video').currentTime);
    
  });
});

document.getElementById('slider').addEventListener('mouseup', function(e) {
  // THE ONLY SOLUTION
  e.target.value = valueHover;

  // just your code,no edits here
  document.getElementById('video').currentTime = valueHover
  document.getElementById('text').innerText = CurrentTime(document.getElementById('video').currentTime);
});



function CurrentTime(v) {
   let hrs = Math.floor(v / 3600);
   let mins = Math.floor(v / 60) % 60;
   let secs = Math.floor(v) % 60;
   let ret = "";
   if (hrs > 0) {
        ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
 }
 ret += "" + mins + ":" + (secs < 10 ? "0" : "");
 ret += "" + secs;
 return ret;
}
           
function showTooltip(e) {

    slidertitle.innerText = CurrentTime(valueHover);
}

let left = document.getElementById('container').getBoundingClientRect().left - document.documentElement.getBoundingClientRect().left;

window.onmousemove = function (e) {
   let x = ((e.clientX + window.pageXOffset) - left) + 'px';
 slidertitle.style.left = (x);
}
#container {
  max-width: 100%;
}
#slider {
  margin-top: 10px;
  width: 100%;
}
.video {
  width: 100%;
  height: 60px;
}
#slidertitle {
  display: none;
  position: relative;
  cursor: default;
  top: 0;
  left: 0;
}
#text {
  position: relative;
  cursor: default;
  top: 0;
  left: 0;
}
#container:hover > #slidertitle {
  display: block;
}
<div id="container">
  <video id="video" controls class="video" src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/Sintel.mp4"></video>
  <input type="range" min="0" max="887" step="0.01" id="slider" value="0"/>
  <p id="slidertitle"></p>
  <p id="text">--</p>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for refreshing an angularjs $scope using $.get jquery

Attempting to implement the code below using $scope: var scopes = "https://www.googleapis.com/auth/contacts.readonly"; setTimeout(authorize(), 20); function authorize() { gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, h ...

Implementing dynamic attribute id addition to select option

Here's the code snippet I'm currently working with: Included HTML <select id="drop_opts"> <option>1<option> <option>2<option> <option>3<option> </select> ...

Utilizing Firebase's real-time database for executing specific conditional queries

I am currently utilizing Express along with the Firebase Realtime Database Admin SDK to handle my database operations. My goal is to retrieve all tickets with a status of pending or open that belong to the logged-in user. However, I am facing difficulty in ...

Utilizing JavaScript and PHP to dynamically load HTML content on a webpage

Below is the code snippet I'm working with: <?php if ($x == 1){ ?> <b>Some html...</b> <?php } else if ($x==2){ ?> <b> Other html...</b> <?php } ?> Now, I want to create two links (a ...

Ensure that the child div is anchored to the bottom of the parent div container

I am looking for a way to ensure that the subfooter div stays at the bottom of the childbox div, similar to a footer. You can view the code on this jsfiddle link <div class="parentbox"> <div class="childbox"> <div id="subfooter" ...

PHP does not seem to be compatible with Ajax

I attempted to utilize ajax for inserting data into my database, but it was unsuccessful. To troubleshoot, I created a basic call to a PHP file which also failed: Below is the HTML code snippet: <form> <h4 class="header-title m-t-0 m-b-30"& ...

Achieving a consistent fixed width for tbody even when scrollbar is present

thead{ width: calc(100% - 17px); display:block; } tbody{ display:block; overflow-y: auto; height:100px; } http://jsfiddle.net/mkgBx/ Is there a way to adjust the width of the tbody element to match that of the thead element plus the scrollbar? Currently ...

The Geolocation API popup on Safari for iOS kept appearing repeatedly

I have successfully implemented a code using the HTML5 Geolocation API to retrieve the user's current position within a mobile website. Although the code functions properly, there is an issue with Safari on iOS. Whenever the page is reloaded, a syste ...

Removing a Dom element using stage.removeChild( )

When the number 6 is typed and entered into the game, the function correct() in the code snippet below determines what action to take. I would like to remove the DOM element gg (the equation 3+3=input) from the stage after typing 6 and pressing enter. How ...

Display a full-width card beneath each small card in every row using CSS

In a scenario where I have designed a layout consisting of 3 column/row cards across multiple rows. Each card displays basic information, and when a user clicks on any specific card, a full-width card below that row will display more detailed information. ...

Are there any resources available to ensure my jQuery script is compatible with multiple browsers?

After realizing that Internet Explorer does not support certain selectors in jQuery, I began to question how to ensure my code will function properly during the writing process. As a Linux user, my testing options are limited to Chrome and Firefox. Is ther ...

Difficulty encountered when attempting to toggle visibility of div elements in Internet Explorer version 10

Working on my .aspx webpage, I have a set of div tags that play a key role in the layout. Let me show you an example with the following snippet: <div id="homeownerquestion" runat="server"> Are you the property owner? <p> <asp:RadioButto ...

Issues arise with AJAX authentication request

Currently, I'm working on incorporating a basic login form with an AJAX request that forwards the user to a page for querying my database. Depending on the results, the user is then redirected to the main index page or prompted back to the login form. ...

CSS has inexplicably ceased to function properly, with the exception of the body

My roommate submitted his project, but only the CSS for the body tag and Bootstrap elements are working properly. When inspecting the webpage, I noticed that none of the CSS changes were showing up, even after editing the Sublime file (except for the body ...

Can a href from a "<Link>" component be passed through a Higher Order Component (HOC) into an "<a>" tag?

I am currently facing a situation with the main component where I have the following code: <Link href={'test'}> <PrimaryAnchor>Welcome</PrimaryAnchor> </Link> Within the PrimaryAnchor component, the code looks like ...

Uncovering Secret Information with Beautiful Soup 4

I've hit a roadblock with my current project. I'm attempting to extract player names and projections from this website: The plan involves running a script that loops through various PID values, but that part is not causing any issues. The real c ...

Default Value for Null in Angular DataTable DTColumnBuilder

What is the best way to define a default value in case of null? $scope.dtOptions = DTOptionsBuilder .fromSource('api/Restt/List'); $scope.dtColumns = [ DTColumnBuilder.newColumn('modi ...

Using Modernizr to determine the presence of nth-child in a test

I'm attempting to utilize modernizr in order to check for browser support of the :nth-child selector, but I am unsure of how to proceed. I came across this particular example on http://jsfiddle.net/laustdeleuran/3rEVe/ which tests for :last-child. How ...

Tips for emphasizing a row of various list boxes when hovering in asp.net

I am facing an issue with two listboxes on my website. Currently, I can only highlight one item at a time by mousing over the listbox. However, I would like to be able to highlight items in both listboxes simultaneously when I mouseover either listbox1 or ...

Pass a parameter to an AJAX request in order to retrieve data from a JSON file that is limited to a specific

I am working with a JSON file named example.json, structured as follows: { "User1": [{ "Age":21, "Dogs":5, "Cats":0 }], "User2": [{ "Age":19, "Dogs":2, "Cats":1 }] "User3 ...