Antoine
Antoine

Reputation: 1

Set "active" class to the current slider element

I'm trying to make a slider of 6 videos (same width for all of it) for mobile phone view exclusively. And when I'm focusing on a video, it must start and get bigger automatically.

My problem is I don't know how to figure out which slide is displayed to launch the video on this slide.

I thought there was some function to find the slider position but didn't find anyone. I thought about getting the slider middle current coordinate but can't find out how to get it

<body>
    <div class="example-wrapper">
        <div class="container both-scroll both-mandatory align-center">
            <div class="wrapper">
                <div class="empty"></div>
                <div class="element">
                    <video width="" height="" controls>
                        <source src="https://storage.coverr.co/videos/coverr-subway-arriving-1567246574152?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6IjExNDMyN0NEOTRCMUFCMTFERTE3IiwiaWF0IjoxNTY4ODg3OTI4fQ.bwTlDESkfV9tTHITf4WqKvEs3MIOlfik0bw3fX3huuo" type="video/mp4">
                        <source src="https://storage.coverr.co/videos/coverr-subway-arriving-1567246574152?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6IjExNDMyN0NEOTRCMUFCMTFERTE3IiwiaWF0IjoxNTY4ODg3OTI4fQ.bwTlDESkfV9tTHITf4WqKvEs3MIOlfik0bw3fX3huuo" type="video/ogg">
                        Your browser does not support the video tag.
                    </video>
                </div>
                <div class="element">
                    <video width="" height="" controls>
                        <source src="https://storage.coverr.co/videos/coverr-subway-arriving-1567246574152?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6IjExNDMyN0NEOTRCMUFCMTFERTE3IiwiaWF0IjoxNTY4ODg3OTI4fQ.bwTlDESkfV9tTHITf4WqKvEs3MIOlfik0bw3fX3huuo" type="video/mp4">
                        <source src="https://storage.coverr.co/videos/coverr-subway-arriving-1567246574152?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6IjExNDMyN0NEOTRCMUFCMTFERTE3IiwiaWF0IjoxNTY4ODg3OTI4fQ.bwTlDESkfV9tTHITf4WqKvEs3MIOlfik0bw3fX3huuo" type="video/ogg">
                        Your browser does not support the video tag.
                    </video>
                </div>
                <div class="element">
                    <video width="" height="" controls>
                        <source src="https://storage.coverr.co/videos/coverr-subway-arriving-1567246574152?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6IjExNDMyN0NEOTRCMUFCMTFERTE3IiwiaWF0IjoxNTY4ODg3OTI4fQ.bwTlDESkfV9tTHITf4WqKvEs3MIOlfik0bw3fX3huuo" type="video/mp4">
                        <source src="https://storage.coverr.co/videos/coverr-subway-arriving-1567246574152?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6IjExNDMyN0NEOTRCMUFCMTFERTE3IiwiaWF0IjoxNTY4ODg3OTI4fQ.bwTlDESkfV9tTHITf4WqKvEs3MIOlfik0bw3fX3huuo" type="video/ogg">
                        Your browser does not support the video tag.
                    </video>
                </div>
                <div class="element">
                    <video width="" height="" controls>
                        <source src="https://storage.coverr.co/videos/coverr-subway-arriving-1567246574152?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6IjExNDMyN0NEOTRCMUFCMTFERTE3IiwiaWF0IjoxNTY4ODg3OTI4fQ.bwTlDESkfV9tTHITf4WqKvEs3MIOlfik0bw3fX3huuo" type="video/mp4">
                        <source src="https://storage.coverr.co/videos/coverr-subway-arriving-1567246574152?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6IjExNDMyN0NEOTRCMUFCMTFERTE3IiwiaWF0IjoxNTY4ODg3OTI4fQ.bwTlDESkfV9tTHITf4WqKvEs3MIOlfik0bw3fX3huuo" type="video/ogg">
                        Your browser does not support the video tag.
                    </video>
                </div>
                <div class="element">
                    <video width="" height="" controls>
                        <source src="https://storage.coverr.co/videos/coverr-subway-arriving-1567246574152?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6IjExNDMyN0NEOTRCMUFCMTFERTE3IiwiaWF0IjoxNTY4ODg3OTI4fQ.bwTlDESkfV9tTHITf4WqKvEs3MIOlfik0bw3fX3huuo" type="video/mp4">
                        <source src="https://storage.coverr.co/videos/coverr-subway-arriving-1567246574152?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6IjExNDMyN0NEOTRCMUFCMTFERTE3IiwiaWF0IjoxNTY4ODg3OTI4fQ.bwTlDESkfV9tTHITf4WqKvEs3MIOlfik0bw3fX3huuo" type="video/ogg">
                        Your browser does not support the video tag.
                    </video>
                </div>
                <div class="element">
                    <video width="" height="" controls>
                        <source src="https://storage.coverr.co/videos/coverr-subway-arriving-1567246574152?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6IjExNDMyN0NEOTRCMUFCMTFERTE3IiwiaWF0IjoxNTY4ODg3OTI4fQ.bwTlDESkfV9tTHITf4WqKvEs3MIOlfik0bw3fX3huuo" type="video/mp4">
                        <source src="https://storage.coverr.co/videos/coverr-subway-arriving-1567246574152?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6IjExNDMyN0NEOTRCMUFCMTFERTE3IiwiaWF0IjoxNTY4ODg3OTI4fQ.bwTlDESkfV9tTHITf4WqKvEs3MIOlfik0bw3fX3huuo" type="video/ogg">
                        Your browser does not support the video tag.
                    </video>
                </div>
                <div class="empty"></div>
            </div>
        </div>
    </div>
</body>


<style>
    body {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100vh;
    }
    video{
        width: 50vw;
    }

    .align-center .element {
        scroll-snap-align: center;
    }

    .example-wrapper {
        text-align: center;
        width: 350vw;
        margin-left: auto;
        margin-right: auto;
    }

    .container {
        width: 100%;
        height: 60vh;
        margin-left: auto;
        margin-right: auto;
        overflow: auto;
        position: relative;

        -webkit-overflow-scrolling: touch;
        scroll-snap-type: mandatory;
    }

    .element {
        height: 60vh;
        scroll-snap-stop: normal;
        display: flex;
        justify-content: center;
        align-items: center;
        position: relative;
    }

    .both-mandatory {
        scroll-snap-type: both mandatory;
    }

    .both-scroll .wrapper {
        width: 50vw;
        height: 100%;
        display: grid;
        grid-template-columns: 25vw 50vw 50vw 50vw 50vw 50vw 50vw 25vw;
        grid-gap: 10px;
    }


</style>

I expect to determine which video is displayed in the center of the screen to add it a class (active).

Thanks for your helping hand

Upvotes: 0

Views: 118

Answers (1)

Greedo
Greedo

Reputation: 3559

You can listen to the play event of the video tag ([More Info])1

$("video").on("play", function(){
   $(this).addClass("active");
}

And in case remove the active when the video is ended:

$("video").on("ended", function(){
   $(this).removeClass("active");
}

Upvotes: 0

Related Questions