播放关闭 Fancybox 弹窗时的背景视频。

huangapple go评论81阅读模式
英文:

Play bg video on close Fancybox popup

问题

我已经理解您的请求。以下是代码部分的翻译:

const video = document.querySelector('.hero-section__video-bg video'),
    videoPlayBtn = document.querySelector('.hero-section__video-btn')

videoPlayBtn.addEventListener('click', function () {
    video.pause()
})

window.addEventListener('click', function () {
    Fancybox.bind('[data-fancybox="video"]', {
        on: {
            close: (fancybox, eventName) => {
                video.play()
            },
        }
    })
})

演示链接

英文:

I have video on bg and button for open this video in popup Fancybox
I need when I open video in Fancybox popup video on bg is paused and when I close this popup video on bg is play
I tried this code but it works on the second attempt to close the popup
Fancybox ver 5.0

		const video = document.querySelector('.hero-section__video-bg video'),
			videoPlayBtn = document.querySelector('.hero-section__video-btn')

		videoPlayBtn.addEventListener('click', function () {
			video.pause()
		})

		window.addEventListener('click', function () {
			Fancybox.bind('[data-fancybox="video"]', {
				on: {
					close: (fancybox, eventName) => {
						video.play()
					},
				}
			})
		})

Link to demo

答案1

得分: 1

我认为你不需要窗口点击处理程序
Fancybox事件处理程序会在你点击关闭按钮时触发
所以这会起作用

const video = document.querySelector('.hero-section__video-bg video'),
  videoPlayBtn = document.querySelector('.hero-section__video-btn')

videoPlayBtn.addEventListener('click', function() {
  video.pause()
})

Fancybox.bind('[data-fancybox="video"]', {
  on: {
    close: (fancybox, eventName) => {
      video.play()
    },
  }
});
英文:

I don't think you need window click handler.
Fancy box event handler will kick in when you click on close button.
So this would work.

const video = document.querySelector('.hero-section__video-bg video'),
  videoPlayBtn = document.querySelector('.hero-section__video-btn')

videoPlayBtn.addEventListener('click', function() {
  video.pause()
})

Fancybox.bind('[data-fancybox="video"]', {
  on: {
    close: (fancybox, eventName) => {
      video.play()
    },
  }
});

huangapple
  • 本文由 发表于 2023年5月24日 21:37:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定