Stopping video playback when hovering over it

Can anyone help me modify this code so that a video plays only when hovering over it, and stops playing once the hover ends? What changes should I make to the JavaScript portion of the code for this functionality?

<div class="padding-box height-40">
                <div>
                    <div class="media_link_overlay" id="video-home"></div>
                </div>
                <div class="background">
                    <div class="content">
                        <div class="text">
                            <video id="video-hover" width="100%" height="100%" preload="auto" onMouseover="mouseoversound.playclip()">
                                 <source type="video/mp4" src="<?php echo get_template_directory_uri().'/videos/homepage_thx.mp4';?>" />
                            </video>
                        </div>
                    </div>      

<script>
$("#video-home").hover(function(){
    $("#video-home")[0].play();
});
</script>

Answer №1

Learn how to utilize the .hover() method which consists of two function handlers: handlerIn and handlerOut. Enhance your video experience by incorporating both functions, allowing the video to play on hover-in and pause on hover-out:

$("#video-home").hover(
  function() {
    $("#video-home")[0].play();
  },
  function() {
    $("#video-home")[0].pause();
  }
);

If you seek to completely halt the video's playback and restart it from the beginning upon next hover-in, follow this approach:

$("#video-home").hover(
  function() {
    $("#video-home")[0].play();
  },
  function() {
    $("#video-home")[0].pause();
    $("#video-home")[0].currentTime = 0;
  }
);

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

The function chartobject-1.render() is returning an error message stating: "Unable to locate the specified container within the document. This issue is occurring in the

I've encountered an issue while trying to integrate FusionCharts into my Vue.js project. Every time I attempt to render the charts within my components, I consistently run into this error. Here's a snippet of one of the components: <template&g ...

A 403 error is thrown by the Microsoft Graph API when attempting to delete the authentication method known as "Microsoft Authenticator"

I'm trying to remove a user's Microsoft Authenticator Authentication Method (#microsoft.graph.microsoftAuthenticatorAuthenticationMethod) from their list of authentication methods, but I keep getting a 403 Forbidden error. Below is the code snip ...

Choose all or none of the items from the list with a single click - v-list-item-group

I am interested in incorporating Vuetify's v-list-item-group component into my Vue application. This list is intended to represent nodes that are related to a graph, allowing users to select none, some, or all of them and delete the selected nodes. T ...

Exploring the distinctions between ajax, await, async, and

English is not my strong suit, so please bear with me if my writing seems odd. We have discovered two methods for transitioning from asynchronous ajax calls to synchronous ones. Using async: false Utilizing await Both achieve the same outcome, but I am ...

Exploring the retrieved data from the AJAX/ASP.NET controller update for a fresh perspective

When selectbox is called, it triggers the 'getDepAndMan()' function. A value is extracted from the selectbox (successful). The function calls methods in the 'GetDepartmentAndManager' controller (successful). The controller returns ...

Angular $watch | obtaining the result from a function

I'm curious why I consistently have to use this $scope.$watch( function() { return $scope.someData; }, function( value ) { console.log( value ); }); in Angular in order for it to watch the data. It's frustrating to me because it seems un ...

What is the preferred convention for defining AngularJS directives as "attributes" versus "elements"?

When creating Angular directives, I've noticed that some directives could also be defined as either an HTML element or an attribute. I'm curious to know what the community convention is for choosing between the two, and the reasons behind it. Fo ...

The Vuetify accordion template is not appearing due to a v-for loop issue in Nuxt.js

My goal is to set up an FAQ page using Nuxt.js. The template I obtained from Vuetify doesn't display correctly on my localhost. Instead, I'm encountering errors. If I replace 'v-for "(item,i) in 5" : key="i"' as per the template source ...

Ensuring the Selection with jQuery

I've successfully implemented jQuery validation on a text field, but when I try to apply it to a radio button, it doesn't seem to work. I'm not sure what I'm doing wrong. Here is my HTML and jQuery code: <input type="text" placehol ...

exploring alternatives to ng-container in angular-4.x for selecting elements

Currently in my Angular 4.x project, I have a component using the Selector 'abc' as shown below: @Component({ selector: "Abc", templateUrl: "Abc.html", styleUrls: [ "Abc.css" ] }) However, the "Abc" tag is also present in the DOM, b ...

Express.js encountered a FetchError due to receiving an invalid JSON response body from https://api.twitter.com

I am currently working on a project that involves getting the tweet id as form input and using the Twitter API to retrieve data about that specific tweet. However, I have encountered an issue where the JSON data is not being returned properly. router.post( ...

Problem encountered with imaskJS in handling the formatting of data with forward slashes

After conducting tests on the frontend of the imask website at , it has been verified that utilizing a forward slash for date formatting results in the exclusion of the final digit of the date. Various attempts were made, including: IMask( field, ...

Converting HTML Form Data into CSV with Multiple Rows

I currently have a form that generates a CSV file with data from select elements in the HTML form. The columns in the CSV correspond to the selected options in the form, but I need to extend this functionality to populate multiple rows in the CSV. Expecte ...

Unable to detect HTML special characters in the text

In my current task, I am facing the challenge of matching two URLs. One URL is retrieved from a MySQL database, while the other one is sourced from an HTML page. When comparing both URLs as strings using Regex, the result shows match.Success = false. Despi ...

The entire title serves as a hyperlink, not just the text portion

Apologies if I'm making mistakes, I'm just starting out with coding and navigating this website. So here's the deal: I am trying to add advertisements to my website on the left and right sides of the centered "Logo" at the top. The issue i ...

Switch between displaying certain values in a dropdown menu by using a checkbox and jQuery

The dropdown box on my website retrieves options from a database, each with an active indicator value of 'Y' or 'N. I also have a checkbox that allows users to include all options or just the active ones. I've successfully implemented ...

Customizable Designs in Oracle Application Express version 4.2

In my work supporting the organization, I am using APEX 4.2 to create a Training Calendar. While I am not a DBA or programming expert, I have been learning through trial and error. The calendar is already set up, but I am looking for a way to customize ho ...

Module 'prompt-sync' not found

When attempting to take user input in Node.js using the prompt module, I encountered the following error message: node:internal/modules/cjs/loader:998 throw err; ^ Error: Cannot find module 'prompt-sync' Require stack: - D:\Code\C+ ...

I am having trouble with using document.getElementById().value to retrieve text input. Can anyone help me understand why it's not

It's puzzling to me why document.getelementbyid().value isn't working as expected. Upon inspecting the console, no input seems to be sent or printed out in the console. function callApi() { var keyword = document.getElementById('user_ ...

"Utilizing JSON, jQuery, and HTML in web development

Is there a way to retrieve the index of a specific object from a JSON file? The following is an example of a JSON file content: [ { "time": "2017-04-11T22:31:50.3369265Z", "score": 0, "comment": "Meep meep! ", "handle ...