jQuery - Play video when hover on parent div

前端 未结 4 1272
臣服心动
臣服心动 2020-12-29 16:19

I\'m creating a video gallery consisting of thumbnails that play a short video on hover. I\'ve been able to get them to play while hovering over the video itself, but I nee

4条回答
  •  情深已故
    2020-12-29 17:05

    The method pay and pause belongs to the dom element(video object), not the jquery object so

    $(document).ready(function () {
        $(".thumbnail").hover(function () {
            $(this).children("video")[0].play();
        }, function () {
            var el = $(this).children("video")[0];
            el.pause();
            el.currentTime = 0;
        });
    });
    

    $(this).children("video") returns a jQuery object which does not have the pay/pause methods so your code should through an error(unless you have included some plugins which adds those methods).

    You can access the underlying dom element by using the index of the child like $(this).children("video")[0] then call those methods on that element.

提交回复
热议问题