简体   繁体   English

如何在滚动时自动静音/取消静音 html 视频

[英]How to automatically mute/unmute html video on scroll

I'm editing a website for a client, and he never mentioned that there will be some javascript edits included, but here we are.我正在为客户编辑一个网站,他从未提到将包含一些 javascript 编辑,但我们在这里。 Since I'm not familiar with js at all, I could really really use your help.因为我根本不熟悉 js,所以我真的可以使用你的帮助。 So, the thing is, there is a video (so called playable ad for mobile) in the header.所以,问题是,标题中有一个视频(所谓的可播放移动广告)。 When you click on it, you get to play some game with the sound ON.当你点击它时,你可以在声音打开的情况下玩一些游戏。 What my client wants is when you scroll down and the video is not visible anymore to MUTE the sound, and UNMUTE it when you scroll up.我的客户想要的是当您向下滚动并且视频不再可见以将声音静音,并在向上滚动时取消静音。 I haven't built this website, I'm just editing it.我还没有建立这个网站,我只是在编辑它。

I've spent days on this, reading different posts and solutions here and on Google, but whatever I tried didn't work.我花了几天时间在这里和谷歌上阅读不同的帖子和解决方案,但我尝试过的任何方法都不起作用。 Since I'm not familiar with Javascript, I suppose that I'm doing something wrong.由于我不熟悉 Javascript,我想我做错了什么。 I suppose that I can't just copy someone else's code and expect it to work, but I probably have to change variable name or something else.我想我不能只是复制别人的代码并期望它可以工作,但我可能必须更改变量名称或其他内容。 But I don't know how to change it and make it work.但我不知道如何改变它并让它发挥作用。

<a class="img" href="">
<iframe src="https://appsposure.com/wp-content/uploads/2019/06/bask.html" scrolling="no" frameborder="0"></iframe>  
<img class="phone-img lazyloaded" src="https://appsposure.com/wp-content/uploads/2019/06/phone-2.png" alt="" data-lazy-src="https://appsposure.com/wp-content/uploads/2019/06/phone-2.png" data-was-processed="true"><noscript><img class="phone-img" src="https://appsposure.com/wp-content/uploads/2019/06/phone-2.png" alt=""></noscript>                                
</a>

Here is the solution that I've tried multiple times, but it doesn't work since I probably don't know how to adjust it for my website. 是我尝试过多次的解决方案,但它不起作用,因为我可能不知道如何为我的网站调整它。

Also, here is the link to the website I'm working on (I'm talking about the video in the header, the one with basketball player on it).另外,这里是我正在处理的网站的链接(我指的是标题中的视频,上面有篮球运动员的视频)。

If there is anyone willing to help me, I would really appreciate it, since I'm struggling for days, and I'm just too stupid for this.如果有人愿意帮助我,我真的很感激,因为我已经挣扎了好几天,而且我太愚蠢了。

added basketball-player id on the iframe在 iframe 上添加了篮球运动员 ID

<a class="img">
<iframe id='basketball-player' src="https://appsposure.com/wp-content/uploads/2019/06/bask.html" scrolling="no" frameborder="0"></iframe>  
<img class="phone-img lazyloaded" src="https://appsposure.com/wp-content/uploads/2019/06/phone-2.png" alt="" data-lazy-src="https://appsposure.com/wp-content/uploads/2019/06/phone-2.png" data-was-processed="true"><noscript><img class="phone-img" src="https://appsposure.com/wp-content/uploads/2019/06/phone-2.png" alt=""></noscript>                                
</a>

using embedded scripts in html.在 html 中使用嵌入式脚本。

<script>
window.addEventListener("scroll", muteOnScroll);

// function for the eventlistener
function muteOnScroll() {
    // if the iframe is out of view
    if (document.documentElement.scrollTop > 610) {
        // targeting the iframe with the id of basketball-player to turn off sound
        document.getElementById("basketball-player").volume = 0.0;
    // else the iframe must be in the view so the sound should be on
    } else {
        document.getElementById("basketball-player").volume = 1.0;
    }
}
</script>

used 610 because that it was I found the top to be when I scrolled the iframe out of view.使用 610 是因为当我将 iframe 滚动到视图之外时,我发现顶部是。

EDIT编辑

used part of what you provided and some of this documentation使用了您提供的部分内容和本文档的一部分

$(window).scroll(function() { 
    var iframe = document.querySelector('iframe');
    if ($(this).scrollTop() > 900){ 
        iframe.mute();
    } else { 
        iframe.unmute(); 
    } 
});

You can check if an element is visible with this function您可以使用此功能检查元素是否可见

function checkVisible(elm) {
  var rect = elm.getBoundingClientRect();
  var viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight);
  return !(rect.bottom < 0 || rect.top - viewHeight >= 0);
}

from the question Check if element is visible on screen .从问题检查元素是否在屏幕上可见

Then you attach a function to the onscroll event handler of the window with然后你将一个函数附加到窗口的onscroll事件处理程序

window.onscroll = function() {
  // CODE
};

You can handle the video being scrolled with some code like this:您可以使用如下代码处理正在滚动的视频:

video.muted = !checkVisible(video);

with video being the video's element. video是视频的元素。

This snippet shows this in action:此代码段显示了这一点:

 var video = document.getElementById('video'); var wrapper = document.getElementById('wrapper'); var videoPreviouslyVisible = false; window.onscroll = function() { wrapper.style.backgroundColor = checkVisible(video) ? '#4f4' : '#f44'; video.muted = !checkVisible(video); }; function checkVisible(elm) { var rect = elm.getBoundingClientRect(); var viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight); return !(rect.bottom < 0 || rect.top - viewHeight >= 0); }
 body, html { height: 100%; } #wrapper { height: 400%; background: none repeat scroll 0 0 #f44; } #video { position: absolute; top: 150%; left: 33%; width: 200px; height: 100px; background: none repeat scroll 0 0 #fff; border: 5px solid black; }
 <div id="wrapper"> <div id="video"></div> </div>

EDIT:编辑:

I've looked into this further and it seems like you're trying to mute an IFrame instead of a video .我已经对此进行了进一步研究,似乎您正在尝试将IFrame而不是video静音。 This is not possible as it is a seperate html website embedded into yours.这是不可能,因为它是嵌入到你的一个单独的HTML网页。 You may be able to access the embedded JavaScript code though it would most probably require the assistance of the original developer of this browser game.您或许能够访问嵌入的 JavaScript 代码,但它很可能需要此浏览器游戏的原始开发人员的帮助。

My answer still applies to any user looking to mute any DOM element with the muted property upon disappearing from view.我的回答仍然适用于任何希望在从视图中消失时将具有muted属性的任何 DOM 元素静音的用户。

I would advise you to contact the original developer and ask him to implement a convenient way to mute the JavaScript game engine the "playable ad" is implemented in.我建议您与原始开发人员联系,并要求他实施一种方便的方法来使实现“可玩广告”的 JavaScript 游戏引擎静音。

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

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