我正在使用HTML5中的<audio>标签播放一个MP3。
<audio controls="controls" autoplay="autoplay">
<source src="song.mp3" type="audio/mp3" />
</audio>这很好用,但我想知道如何在歌曲开始自动播放之前设置延迟?我知道没有delay属性,但我想它可以通过javascript来完成?
发布于 2012-08-16 01:05:24
试试这个:
HTML:
<div id='audio'>This will be replaced with an audio tag</div>jQuery:
$(document).ready(function (){
$("#audio").stop("true").delay('2000').queue(function() {
$(this).html('<audio controls="controls" autoplay="autoplay"><source src="song.mp3" type="audio/mp3" /></audio>');
});
});加在一起就是:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
</head>
<body>
<div id='audio'>This will be replaced with an audio tag</div>
<script type='text/javascript'>
$(document).ready(function (){
$("#audio").stop("true").delay('2000').queue(function() {
$(this).html('<audio controls="controls" autoplay="autoplay"><source src="song.mp3" type="audio/mp3" /></audio>');
});
});
</script>
</body>
</html>您可以在jQuery中使用.delay()来延迟进程。时间以毫秒为单位,因此2000 =2秒。
https://stackoverflow.com/questions/11973673
复制相似问题