The pause button will still function, even if the scrolling feature is enabled

I've successfully implemented a feature that pauses and plays the video on scroll when it reaches a certain pixel height. However, I'm facing an issue where the code automatically plays the video whenever the scroll position is more than 13500px from the top. Is there a way to prevent the video from playing if it has been manually paused using the control button, even during scrolling? Currently, the video resumes playing even after being paused.

 $(document).ready(function() {

                $(window).scroll(function() {

                    var vid = document.getElementById("video_1");

                        if($(document).scrollTop() > 13500) { 

                            vid.play(); 

                        }

                        else {

                         vid.pause(); 
                        }

                });
        });

Answer №1

To keep track of whether a user has paused or played the video, you need to listen for 'pause' and 'play' events. The code snippet below demonstrates how this can be achieved:

let isPaused = false;
let videoElement;

$(document).ready(function() {
    videoElement = document.getElementById("video_1");

    videoElement.onpause = function() {
        isPaused = true;
    };

    videoElement.onplaying = function() {
        isPaused = false;
    }

    $(window).scroll(function() {
        if($(document).scrollTop() > 13500 && !isPaused) { 
            videoElement.play(); 
        } else {
            videoElement.pause();
            isPaused = true;
        }
    });
});

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

Using Bootstrap 3 to create a carousel with a seamless fading transition as the background

I want to utilize Bootstrap Carousel as the background for my website. I've successfully incorporated a standard carousel as the background, but I'm encountering difficulties with making fade transitions. Here is my current implementation: HTML: ...

Having trouble executing "npm install" following the clone from GitHub in React

After cloning a repository from GitHub, I attempted to run "npm install" but encountered the following error: https://i.sstatic.net/QM4RZ.png Since the project is still in development, should I install or add anything else to successfully run it? ...

Step-by-step guide on loading an external javascript file once a webpage is fully loaded, and incorporating a function from it

After the initial page load, I have a need to incorporate an external JavaScript file into an HTML file. This is necessary because a variable in this external JS file only updates after the HTML file has fully loaded. Additionally, I want to utilize the fu ...

Managing the React Router component as a variable

I'm currently working on integrating React-Router into an existing React app. Is there a way to use react-router to dynamically display components based on certain conditions? var displayComponent; if(this.state.displayEventComponent){ {/* ...

Variables for HTML comment form using PHP mail

How do I properly pass HTML form variables to a PHP mailer? The PHP code is: $comment = $_POST['comment']; $email = $_POST['email']; $to = '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="60131313 ...

The useEffect hook is able to fetch data even when the state stored in the dependency array remains constant

I have been working on developing a quiz page that utilizes the useEffect hook to fetch data. The data retrieved includes the question as well as multiple-choice options. There is a button labeled Check Answer which, when clicked, reveals the user's f ...

Displaying Keystonejs list items on every page

Is there a more effective method to efficiently load the content of a Keystone's list on every page, rather than calling it individually in each view? Could this be achieved through middleware.js? The objective is to create a dropdown for the navigat ...

Is there a way to activate a function in a sibling component without using Prop Drilling?

Looking at the image, you can see the structure of components I have. <Invoice> <Left></Left> <Right></Right> </Invoice> In the Left component, there is a form, and in the Right component, there is a Submit button l ...

Make sure that the HTML5 application is fixed inlandscape mode

I'm in the process of developing a basic HTML5/CSS3/Javascript application that needs to be compatible with all major mobile platforms - android, ios, and windows-phone. My goal is to have the app only function in landscape mode, even if the user hold ...

Prevent the mouseup event in jQuery when the mouse has previously been moved

I have a div with a row of span buttons displayed horizontally. Since there are too many buttons to fit on the screen, I want to enable the user to click and drag the entire row of buttons. However, my challenge is to ensure that the button's mouseup ...

What steps are involved in setting up a single form for entering orders, complete with both an order table and an order_item table that incorporates a foreign key

As I contemplate the best way to structure my page and forms for a customer ordering process, a few questions arise due to the tables' organization. To adhere to normalization standards, I have separated the order table and the order items table, link ...

Dynamic routes in NextJS automatically append a .txt extension to the end of the URL

Issue: When using NextJS, the link <Link href="/link">link</Link> redirects to /link.txt For a simple link like this, HTML <a href="/link">link</a> can be used instead The real problem arises when using NextJS ...

Issues with JQuery draggable event not functioning properly on Internet Explorer 9

Every browser handles the following code without any issue, except for Internet Explorer 9; $(".dragbox").draggable({ handle: ".header", grid: [1, 1], containment: 'parent', reflow: true, stop: functio ...

How can I attach a jQuery plugin to a textbox with a changing class name?

I have a function that converts a regular text field into a datepicker: <input type="text" class="datepicker form-control" name="text-150" > var DatePicker = function () { if ($(".datepicker").length === 0) { return; } $(".datepic ...

Split the cards into 2 columns vertically on smaller screens, positioning the header above both columns

I am having issues with my cards breaking incorrectly on wide screens. I would like to display 4 larger cards followed by 2 columns of smaller cards, all with the correct header above them. Here is the link to my jsfiddle: jsfiddle.net/ad5qa/41tbgk75/ I ...

The functionality to Toggle submenus within a navigation menu using jQuery is not performing as anticipated

I implemented a horizontal menu using CSS, HTML, and jQuery. Clicking on a menu item displays a sub-menu. The issue I'm facing is that when one submenu is open and I click on another menu item, the new submenu opens but doesn't hide the previous ...

Emphasize entries in an index that match the currently visible content as you scroll

I have a table of contents in my HTML page with the CSS attribute position: fixed;. I want to emphasize the current reading position by highlighting it (bold or italics) as I scroll down the page. | yada yada yada ... 1. Secti ...

It appears that the jQuery.post() method is being overlooked or bypassed for unknown reasons

Trying to understand why the call to jQuery.post() isn't fetching the data or running the function after the fetch. Three files are included: an HTML file, a JavaScript file, and a PHP file. The HTML contains the modal element intended to appear when ...

Utilize Express.js routes to deliver static files in your web application

I am looking to serve a collection of HTML files as static files, but I want the routes to exclude the .html extension. For example, when someone visits example.com/about, I'd like it to display the contents of about.html In my research on serving ...

Changing the font family for a single element in Next.js

One unique aspect of my project is its global font, however there is one element that randomly pulls font families from a hosted URL. For example: https://*****.com/file/fonts/Parnian.ttf My page operates as a client-side rendered application (CSR). So, ...