简体   繁体   English

如何使用主体 onload 事件同时操作 HTML 音频标签和 JavaScript 函数?

[英]How to operate both HTML audio tag and a JavaScript function with body onload event?

I hava a JavaScript code (for slideshow) below and also a HTML audio tag.我在下面有一个 JavaScript 代码(用于幻灯片)和一个 HTML 音频标签。 I want my page to load every component of itself before playing the slideshow and also the audio file.我希望我的页面在播放幻灯片和音频文件之前加载自身的每个组件。

I have no problem with using window.onload to satisfy my need in order to run slideshow.我使用 window.onload 来满足我运行幻灯片的需要没有问题。 but I don't know how to do it with my HTML tag .但我不知道如何处理我的 HTML 标签。

I think I should write my JavaScript inside the body tag and use: body onload="...", but I don't know how!我想我应该在 body 标签中编写我的 JavaScript 并使用:body onload="...",但我不知道怎么做!

Here is my JavaScript code :这是我的 JavaScript 代码:

<script type="text/javascript">

window.onload = a;

function a() {

        $(document).ready(function(){
        $('#slideshow').cycle({
            
x:'curtainX', 
sync:  false,
speed:900,
timeout:10, 
delay: 1500,
});
});

setTimeout(plus1,9000);
function plus1() { 

        $(document).ready(function(){
        $('#slideshow').cycle('pause');
});
}

}

</script>  

And this is my audio tag :这是我的音频标签:

<audio autoplay loop>
  <source src="kooche.mp3">

So it sounds like you can play the slideshow after the page is loaded, but you're not sure how to set the audio to play after the page load.所以听起来您可以在页面加载后播放幻灯片,但您不确定如何设置页面加载后播放的音频。 Correct?正确吗?

If so, instead of including the HTML audio element in markup, you can create the audio element dynamically in your script.如果是这样,您可以在脚本中动态创建音频元素,而不是在标记中包含 HTML 音频元素。

For example:例如:

$(document).ready(function(){
    $('#slideshow').cycle('pause');
    // Create the audio element
    var music = new Audio("kooche.mp3");
    music.play();
});

I also recommend checking if the clients browser supports HTML5 audio playback.我还建议检查客户端浏览器是否支持 HTML5 音频播放。

var audioEl = document.createElement('audio');
// Check this before you try playing your audio
var canPlayAudio = !!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, ''));

Source来源

If jQuery is an option, you can use如果 jQuery 是一个选项,您可以使用

$(document).ready(function(){
   //my slideshow code
});

Or if it's not an option, you can create your own pseudo jQuery-like ready listener或者,如果它不是一个选项,您可以创建自己的伪 jQuery 式就绪侦听器

function onReady (callback){
    var addListener = document.addEventListener || document.attachEvent,
        removeListener =  document.removeEventListener || document.detachEvent
        eventName = document.addEventListener ? "DOMContentLoaded" : "onreadystatechange"

    addListener.call(document, eventName, function(){
        removeListener( eventName, arguments.callee, false )
        callback()
    }, false )
}

Or, put the slideshow code in question at the very bottom of your DOM.或者,将有问题的幻灯片代码放在 DOM 的最底部。 It'll naturally be executed last.它自然会最后执行。

Is this what you're after?这是你追求的吗? It plays the song on load using .trigger and there's no bloated markup.它使用.trigger在加载时播放歌曲,并且没有臃肿的标记。 http://jsfiddle.net/2xH29/ http://jsfiddle.net/2xH29/

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

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