简体   繁体   English

jQuery mouseleave - mouseenter 只在第一个元素中工作

[英]jQuery mouseleave - mouseenter just work in the First Element

I'm struggling trying to set a mouseenter and mouseleave event in several videos.我正在努力尝试在几个视频中设置mouseentermouseleave事件。 Everything seems that work perfectly, the video plays when I mouseenter and stop when mouseleave .一切似乎都很完美,视频在我mouseenter时播放并在mouseleave时停止。

However, when I add more than one video , just play the first video.但是,当我添加多个视频时,只需播放第一个视频。 I am trying to figure out what it's missing what I don't find the way to do it.我试图弄清楚它缺少什么我没有找到方法。

Here is the link to codepen: https://codepen.io/felixgonzalo/pen/poJoXRW这是 codepen 的链接: https ://codepen.io/felixgonzalo/pen/poJoXRW

My code is:我的代码是:

<div class="main-wrapper">

        <div class="video-container">
            <video id="video" class="video" controls class="border" controls="false" muted loop>
                <source src="https://www.errorerror.studio/wp-content/uploads/2020/01/ee.st-honesty-clip-00.mp4" preload="auto" type="video/mp4" autoplay />
            </video>
            <div class="overlay"></div>
        </div>

        <div class="video-container">
            <video id="video-2" class="video" controls class="border" controls="false" muted loop>
                <source src="https://www.errorerror.studio/wp-content/uploads/2020/01/ee.st-honesty-clip-00.mp4" preload="auto" type="video/mp4" autoplay />
            </video>
            <div class="overlay"></div>
        </div>

    </div>

jQuery jQuery

$('.video').prop('currentTime', 0)

        function Play() {
            $('.video').get(0).play();
        }
        function Stop() {
            $('.video').prop('currentTime', 0).get(0).pause();
        }

        $(document).ready(function(){
            $('.video-container').mouseenter(function(){
                $('.overlay', this).addClass("hide")
                Play();
            }).mouseleave(function(){
            $('.overlay', this).removeClass("hide")
            Stop();
        });
        });

I also tried to make it in JS but didn't work out:我也尝试在 JS 中实现,但没有成功:


        var container = document.querySelector('.video-container');

        container.addEventListener('mouseleave', function() {
        this.querySelector('.overlay').classList.remove('hide');
        Stop();
        });

        container.addEventListener('mouseenter', function() {
        this.querySelector('.overlay').classList.add('hide');
        Play()
        });

Really appreciate any help真的很感谢任何帮助

Your function play gets only the first video because you specified the 0 index.您的函数 play 仅获取第一个视频,因为您指定了0索引。 You need to pass the current video index you're hovering to trigger the playback.您需要传递当前悬停的视频索引以触发播放。 EDIT: Working code.编辑:工作代码。 I made your functions taking an argument, which I'm setting to the first video element children of the element triggering the mouse events.我让你的函数接受了一个参数,我将它设置为触发鼠标事件的元素的第一个视频元素子元素。

$('.video').prop('currentTime', 0)

$(document).ready(function() {
    $('.video-container').mouseenter(function() {
        $('.overlay', this).addClass("hide")
        $(this).children('.video')[0].play();
    }).mouseleave(function() {
        $('.overlay', this).removeClass("hide")
        $($(this).children('.video')[0]).prop('currentTime', 0)
        $(this).children('.video')[0].pause();
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM