JavaScript and jQuery code: Trigger the vjs-fade-out class to toggle when the userActive value in video.js changes

I am currently utilizing video.js to develop a player that is compatible with various devices. One crucial feature I have included is a custom "return to menu" button located in the top right corner of the video player. The challenge I am facing is dynamically adding or removing the vjs-fade-out class to this button based on user interactions, such as mouseover, mouseout, tap, play, pause, and more. However, my current implementation has some bugs...

The HTML structure looks like this:

<div class="menu-btn">
  <a href="../index.html">
    <img src="../img/back.svg" width="50">
  </a>
</div>

<video id="myPlayer" class="video-js vjs-sublime-skin" width="1024px" height="768px" controls autoplay preload data-setup='{"nativeControlsForTouch": false}'>
  <source src="../video/test.mp4" type='video/mp4' />
  <track class="caption" kind="captions" src="../video/test.vtt" type="text/plain" srclang="en" label="English" default />
</video>

The corresponding CSS code is as follows:

.menu-btn {
  z-index: 1000;
  position: absolute;
  top: 10px;
  left: 10px;
}

Furthermore, here is the jQuery script responsible for handling user interactions:

videojs("myPlayer").on('mouseout', function() {
  $(".menu-btn").addClass('vjs-fade-out');
});

videojs("myPlayer").on('mouseover', function() {
  $(".menu-btn").removeClass('vjs-fade-out');
});

videojs("myPlayer").on('tap', function() {
  if (videojs("myPlayer").userActive()) {
    $(".menu-btn").removeClass('vjs-fade-out');
  } else {
    //do nothing
  }
}, 1500);

My aim is to simplify the script using the userActive method as shown below:

$(document).ready(function() {
  if (videojs("myPlayer").userActive()) {
    $(".menu-btn").removeClass('vjs-fade-out');
  } else {
    $(".menu-btn").addClass('vjs-fade-out');
  }
});

Answer №1

I believe the solution has been identified

myPlayer.on("useractive", function() {
  $(".menu-btn").removeClass("vjs-fade-out");
});

myPlayer.on("userinactive", function() {
  $(".menu-btn").addClass("vjs-fade-out");
});

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

What is the best way to create an animation where every letter in a word transitions to a different

Is there a way to animate a word so that each letter changes color within a range of 7 colors, with all letters displaying different colors simultaneously in an infinite loop? <div class="box"> <h1 class="logo animate__animated an ...

What could be causing the function to not execute before the rest of the code in the React App?

My lack of expertise may be the reason, but I'm unsure how to address this issue: Here's what I have: A button labeled "Check numbers" <Button fullWidth variant="contained" onClick={this.checkOptOut ...

The error message "Cannot read property 'properties' of undefined" is being thrown by the leaflet-search plugin

I attempted to implement the leaflet-search feature using a Vue-cli component. However, when I initiate a search, I encounter the following error message specifically related to the leaflet search function: Uncaught TypeError: Cannot read property &apo ...

Challenges encountered with the navigation bar in Bootstrap 4

I am facing an issue with positioning the logo on the left side of the top navigation bar. Every time I try to implement this, it ends up breaking the frontend design. Here is a snippet of the code: .top-line { border-bottom: 1px solid #cecece; } ...

Using jQuery's .grep() method on an array will only return the final digit

Could someone help me understand the behavior of jQuery's .grep() method? I'm creating a jQuery object array based on the names of these elements: <div class="small1 other">S1</div> <div class="small2">S2</div> <div c ...

Nav Bar Toggle Button - Refuses to display the dropdown menus

My Django homepage features a Bootstrap navbar that, when resized, displays a toggle button. However, I am experiencing difficulty with dropdown functionality for the navbar items once the toggle button is activated. Can anyone provide guidance on how to a ...

What is preventing Facebook from merging its CSS/JS files together?

I wonder about the reasoning behind Facebook developers' decision not to consolidate their scripts and stylesheets into single files, opting instead to load them on demand through their CDN. Considering the complexity of Facebook as an application, I ...

How to update the image URL using jQuery

Due to challenges with achieving a consistent background across various browsers, I have resorted to using a div with an image to cover the entire page. My goal is to have this image change based on the time of day. Previously, I had success using the tra ...

What could be causing the right padding to be overlooked next to the scroll bar, and what steps can be taken to address this

.outer { padding: 50px; } .inner { width: 500px; height: 1000px; border: 1px solid gray; margin: auto; } <div class="outer"> <div class="inner"> <div class="content">qwerty</div> </div> </div> From my ...

When implementing helmetJS into my project, I noticed that Font Awesome was displaying white squares

Initially, I created an application that utilized this code in the HTML and incorporated classes with Font Awesome icons, which functioned effectively. <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css&q ...

Modifying content on the fly with a dropdownlist in Knockout framework

Currently experimenting with implementing inline editing through knockout. I stumbled upon this informative thread about Knockout Inline Edit Binding After some tweaks to introduce a "Select" for edit mode, the functionality seems to be working fine. Howe ...

Prevent the bootstrap dropdown menu from closing when encountering a login error during form validation using ajax and codeigniter

I encountered an issue with my dropdown menu login that utilizes bootstrap ajax and codeigniter. When I attempt to submit the form and there is an error, I have to click multiple times before the error message appears because the dropdown menu keeps closin ...

Is there a way for me to send a form containing pre-existing data from a different page?

Seeking advice: I realize that utilizing jQuery's ajax function may be the simplest approach, however I am facing an issue with the form field names. The var_dump below displays the typical values submitted by the form using test data. It appears that ...

JavaScript tool: Verify the presence of both jQuery and jQuery UI

I have been working on developing a Javascript Widget (almost complete), and when integrating it into the first site I encountered some issues. My Widget requires loading multiple libraries (jquery, jquery ui, mustache, ...); this is achieved by injecting ...

Adjusting font sizes for both multiline text within a <p> element and labels based on the number of lines

Whenever I have multiline paragraphs or labels, the font size seems to adjust depending on the number of lines. All I want is to keep the font size consistent regardless of the lines. The labels all have the same CSS styling and inheritance. Here is a vis ...

Implementing setTimeout with the copy button: A guide

How can I implement a setTimeout function in the copy button so that when a user clicks on it, the text will change to "copied" and then revert back to "copy" after 3-4 seconds? Please help me find a solution to this problem and also optimize the JavaScrip ...

Extracting Tailwind color palette through API request

After conducting some research, I have not been able to find a definitive answer to my query. I possess a CMS API that supplies branding colors and other assets for a React application that can dynamically change its appearance using a combination of colo ...

Implementing text overlays on images within a Bootstrap 4 navigation bar

I am struggling to grasp the flex structure in Bootstrap. I'm attempting to overlay text on an image within the navbar, but it seems the container of the navbar is causing issues. Perhaps my containers are not set up correctly. Any assistance would be ...

The function named updateContact is not defined, even though it has been declared

Presented below is Angular code snippet: @Component({ selector: 'app-detail-view', templateUrl: './detail-view.component.html', styleUrls: ['./detail-view.component.css'] }) export class DetailViewComponent implements O ...

Ways to eliminate text following a string substitution

When running the code below with keys assigned to summer, spring, fall, and winter, the output for ins would be: ['req.body.summer, req.body.spring, req.body.fall, req.body.winter'] I need to eliminate the surrounding string from the replace co ...