Enjoy watching a video by simply hovering over the highlighted words

I am looking to implement a function where the video plays when hovering over highlighted words and pauses when the mouse moves away. However, I am currently only able to get the video to autoplay upon hover, not the highlighted words. Any assistance on this would be greatly appreciated.

<a href="https://www.google.com" target="_blank"><video width="250px" height="250px"  controls preload onmouseover="this.play()" onmouseout="this.pause()" >
<source src="http://www.w3schools.com/html/movie.mp4" type="video/mp4" >
Not Supporting
</video></a>
<br/><br/>
<a href="#" >Play&Pause</a>

Answer №1

If you prefer not to use jQuery (which is not in your snippet), here's a method to achieve the same result: simply give the video an id, then add onmouseover and onmouseout events to the a tag that targets the element with that id.

  • Assign id="video" to the video tag
  • Add
    onmouseover="document.getElementById('video').play()"
    and
    onmouseout="document.getElementById('video').pause()"
    to the a tag containing the "Play&Pause" text

<a href="https://www.google.com" target="_blank">
  <video width="250px" height="250px" controls preload onmouseover="this.play()" onmouseout="this.pause()" id="video">
    <source src="http://www.w3schools.com/html/movie.mp4" type="video/mp4">
      Not Supporting
  </video>
</a>
<br/>
<br/>
<a href="#" onmouseover="document.getElementById('video').play()" onmouseout="document.getElementById('video').pause()">Play&Pause</a>

To organize your code better, you can centralize this functionality and eliminate inline JavaScript.

  • Give elements that will trigger play and pause events a class="trigger"
  • In JavaScript, iterate through the elements with the trigger class and attach the mouseover and mouseout events

var triggers = document.getElementsByClassName('trigger');
var video = document.getElementById("video");

for (var i = 0; i < triggers.length; i++) {
  triggers[i].addEventListener("mouseover", function(event) {
    video.play()
  }, false);
  triggers[i].addEventListener("mouseout", function(event) {
    video.pause()
  }, false);
}
<a href="https://www.google.com" target="_blank">
  <video class="trigger" width="250px" height="250px" controls preload id="video">
    <source src="http://www.w3schools.com/html/movie.mp4" type="video/mp4">
      Not Supporting
  </video>
</a>
<br/>
<br/>
<a class="trigger" href="#">Play&Pause</a>

Answer №2

When utilizing jQuery exclusively and focusing on elements rather than using inline scripting, you can achieve the following:

var $videoElement = $( "#myVideo" ),
    $playButton   = $( "#play_btn" );

$playButton.hover(function() {
    $videoElement[0].play();
    }, function() {
    $videoElement[0].pause();
});

Check out this example: jsfiddle

I hope this explanation is beneficial to you.

Answer №3

It's quite easy. Just wrap your highlighted Text with a

<span id="text"></span>

$( '#text' ).on('mouseover', function() {
  $( 'video' ).play();
}).on('mouseout', function() {
  $( 'video' ).pause();
});

Answer №4

Check it out here! https://jsfiddle.net/NewUser/abc123/

This example utilizes basic jQuery to simulate mouse actions.

$("#trigger").hover(function() {
    $("image").mouseover();
});

$("#trigger").mouseleave(function() {
    $("image").mouseout();
});

I hope this information is helpful!

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

When the page is resized, the Font-Awesome icons shift position

/* icon sect*/ .social { margin-top: px; padding: 0; position: absolute; top: 60%; left: 50%; transform: translate(-50%,-50%); } .social li { list-style: none; float: left; margin: 0px; width: 60px; height: 10px; line-height: 60px; ...

Managing additional data in jQuery autocomplete

I'm encountering an issue with my current project. I am using the jQuery autocomplete plugin to retrieve a list of suggested values from the server. The format of the list is as follows: <input> field would be set to "Username1". However, I act ...

Using an image link in CodeIgniter will not function properly

Could someone please clarify why this particular code is not functioning properly in CodeIgniter? .linkBack{ background-image:url('/myBlog/CodeIgniter_1.7.2/pictures/arrow.gif'); display:block; height:58px; width:105px; text-indent:-999px ...

Steps for inserting new rows into a table from text fields1. Begin

As someone who is brand new to jquery/html, I am eager to create a diary specifically for logging my long distance running times in a neat table format. Here's the HTML code I have been working on: <head> <title>Running Diary</titl ...

Using Laravel 4 in combination with AJAX serialization, submitting a form with a check box

Currently working on creating a Student Information System with Laravel. Utilizing JQuery Ajax for CURD Operations. Facing an issue where I need to send checkbox values to the controller class. The form has around 20 different inputs, making it difficult ...

validating price ranges with the use of javascript or jquery

<!DOCTYPE html> <html lang="en"> <head> <title>My Page Title</title> </head> <body> <form method="post" action="process-form.php"> Price Range: From <input type="text" id="price-from"> ...

Automatically choose the initial <select> option when loading asynchronous choices in Vue 3

My challenge is to bind a <select> HTML element with the v-model directive to a ref value in Vue.js 3's setup() method. I want the Form.ProductID ref to be updated when a user selects a different option. This is the code for the <select> ...

Showing a button link on a mobile device when images are disabled

I'm facing an issue with the page on a third-party site. When I access it on my PC, the linked button works perfectly. However, on my iPhone, I am redirected to a mobile version of the site where all images are missing. Is there a way for me to save t ...

Effectively handle multiple connections from nodejs to postgres using the pg library

I need to run a script that performs multiple queries using the pg library for managing connections. However, I am facing an issue where my program stops working when the connection pool is full and does not queue future queries. I have tried setting the p ...

Chrome is experiencing compatibility issues with Datatable

I'm encountering an issue with Datatables and I'm at a loss on how to resolve it. In my custom cms, Datatables functions perfectly on my Mac (Safari, Chrome, Firefox) and also works seamlessly for my colleagues on Windows machines. However, we ...

Display 3 buttons only on screens smaller than 576 pixels, and hide them on wider screens

Struggling to get these buttons to show up, but nothing seems to work! Just stumbled upon a code that should make them visible under 576 px: However, only the navbar icon is appearing... https://i.sstatic.net/sDMJv.png I even tried consulting two AI ...

Innovative User Handle Generation using Firebase in a React/Javascript Environment

My goal is to create new users using Firebase authentication and store their information in Firestore. Everything was going smoothly until I encountered the challenge of generating unique usernames. What would be the most effective approach to tackle this ...

Troubleshooting Angular Toaster issues with TypeScript

After downloading the angular-toaster.d.ts file from Nuget and setting up a notification service, everything seems error-free, but the notification service is not functioning as expected. export class notificationService { constructor(private toaster ...

Accelerating the process of compiling subwords using the letters found in different words

The purpose of this code is to generate a list of arrays containing words based on their length, and each word includes an array of subwords that can be spelled using the characters from the word itself. I am in the process of optimizing the code to handle ...

Remove the initial section of the text and provide the rest of the string

I am a beginner in the world of Javascript and unfortunately I have not been able to find an answer to my current problem. Here is the situation. On a webpage, I am receiving a URL that is sometimes presented in this format: http://url1.come/http://url2.c ...

Locate the index of the item I am selecting within the Owl Carousel

I have some HTML code below, where I am attempting to determine the index of the video that was clicked on and also retrieve the "href" attribute of the anchor tag with the class "owl-video". Take a look at the code snippet I have tried. To retrieve ...

Droppable TextArea in JQuery now has fixed sections

Looking for a text area where I can set fixed labels and easily drag and drop components under the appropriate label. The functionality to rearrange components if mistakenly dropped under the wrong label is crucial. Any suggestions on how I can implement ...

Leveraging Async/Await to track error counts across three distinct loops, each invoking an asynchronous function in every iteration

While I have experience with Callbacks, Async/Await and Promises are new concepts to me. In my node.JS server project, I am faced with the challenge of counting errors generated by thousands of asynchronous calls from three different async functions. My g ...

Encountering a problem during the transition from webpack 1.x to webpack 2.x, specifically related to the absence of an

Currently in the process of transitioning a project from Webpack 1 to Webpack 2, I've hit a snag with integrating eslint. In my existing Webpack 1 configuration, eslint is set up as follows: eslint: { configFile: path.join(__dirname, "eslint.core ...

Panel floating with Bootstrap framework

I have created a unique two-column layout using Bootstrap, utilizing the col-md-6 class. The layout consists of a row with a panel at the top containing a table, a left column panel displaying a list of media items, and a right column panel containing text ...