Jquery Youtube Iframe Mouseenter和Mouseleave悬停

时间:2016-08-04 21:47:39

标签: javascript jquery mouseenter mouseleave

我在网站上有很多视频,我在悬停的iframe播放时徘徊。它应该停止播放。

悬停和播放有效。但是Mouseleave不起作用,我也不知道为什么。



$(document).ready(function() {
		  $(".youtube").each(function() {
		    $(this).css('background-image', 'url(//i.ytimg.com/vi/' + this.id + '/hqdefault.jpg)');
		    $(document).delegate('#' + this.id, 'mouseenter', function() {
				if($(this).data('mouseenter')) {
					return;
				}
				$(this).data('mouseenter', true);

		      var iframe_url = "//www.youtube.com/embed/" + this.id + "?autoplay=1&autohide=1";
		      if ($(this).data('params')) iframe_url += '&' + $(this).data('params');
		      var iframe = $('<iframe/>', {'allowfullscreen':'allowfullscreen', 'frameborder': '0', 'src': iframe_url})
		      $(this).append(iframe);
		    });
			  var playersrc=$('.youtube').attr('src');
		  });
		});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

$(document).delegate('#' + this.id, 'mouseleave', function() {
            if($(this).data('mouseleave')) {
                alert("not first time, but mouseleave is still detected");
                return;
            }
            alert("first time your cursor is quitting this iframe, mouseleave detected");
            $(this).data('mouseleave', true);
        });

这对我有用!