【问题标题】:jQuery sometimes not changing video srcjQuery 有时不会改变视频 src
【发布时间】:2020-08-04 13:56:59
【问题描述】:

在 wordpress 页面中。我有一个在页面加载时自动播放的标签。每次我调整窗口大小时,视频都会重新播放。我有一个功能,每次如果 url 包含 (/home/en),它会更改视频 (src) 属性,但由于某种原因,有时它不会。

jQuery(window).resize(function() {
        if(window.location.href.includes("/home/en")){
            jQuery('#testID').attr('src','https://test.mp4');
        }
    });

有谁知道可能是什么问题!

【问题讨论】:

  • 一个小问题,因为我对jquery不太熟悉:在调整大小时不会设置属性几十次吗?
  • 是的。但这不是视频重启的原因,因为我在没有这个功能的情况下尝试了这个
  • 每次加载页面时,您使用什么代码播放视频?您发布的线路看起来不错。
  • 我正在使用这个,但是设置一个短暂的超时似乎可以解决问题。谢谢

标签: javascript html jquery wordpress


【解决方案1】:

无法告诉你为什么它有时会失败,除了你的location 不包括你想要的。但您只需检查一次。

//only need to check this once per window    
var PlayVideo = window.location.href.includes("/home/en");
var PlayTimeout = null;

jQuery(window).resize(function(){
    //adding a timeout to prevent multiple calls
    window.clearTimeout(PlayTimeout);
    PlayTimeout = window.setTimeout(function(){
        if(PlayVideo){
            jQuery('#testID').attr('src','https://test.mp4');
        }
        else{
            //if you reach this point, your condition is not what you want it to be
            console.log(window.location.href)
        }
    }, 1000)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

【讨论】:

  • 这适用于桌面,但在移动设备上,当我向下滚动视频停止时,当我向上滚动视频时,视频 src 会更改为之前的版本
  • 遗憾的是,我没有设备可以帮助您解决这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-13
  • 1970-01-01
  • 2018-08-23
  • 1970-01-01
  • 2021-11-21
  • 2013-06-25
  • 1970-01-01
相关资源
最近更新 更多