gpt4 book ai didi

jquery - 随机音频onmouseover jQuery不起作用

转载 作者:行者123 更新时间:2023-12-03 02:16:55 27 4
gpt4 key购买 nike

我正在尝试使用jquery为所有导航按钮创建随机音频效果,基本上是将鼠标悬停在唯一的导航div上,并播放数组中的随机音频mp3文件

 <!DOCTYPE html>
<html>
<head>
<title>AAARGH CARIBBEAN PIRATES</title>
<meta charset = "utf8">
<link rel = "stylesheet" href = "main.css">
<script src = "jq/jquery-1.10.2.js"></script>
</head>
<body>
<div class = "wrapper">
<div class = "navmain">
<div class = "navbox">
<p class = "navi"><a href = "arms.html">arms</a></p>
</div>
<div class = "navbox">
<p class = "navi"><a href = "map.html">map</a></p>
</div>
<div class = "navbox">
<p class = "navi"><a href = "ship.html">ship</a></p>
</div>
<div class = "navbox">
<p class = "navi"><a href = "facts.html">facts</a></p>
</div>
<div class = "navbox">
<p class = "navi"><a href = "speech.html">speech</a></p>
</div>
</div>
<div id = "home">
<div id = "innerhome">
<p class = "basic">
AAARGH mateys, the caribbean was a ..
</p>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$('.navbox').onmouseover(playSound());
});

function playSound(){
var audio[];
audio[0] = new Audio("assetts/audio/bird.mp3");
audio[1] = new Audio("assetts/audio/cannon.mp3");
audio[2] = new Audio("assetts/audio/fire.mp3");
audio[3] = new Audio("assetts/audio/monkey.mp3");
audio[4] = new Audio("assetts/audio/crows.mp3");
audio[5] = new Audio("assetts/audio/cannon2.mp3");

var x = Math.floor((Math.random()*1000) % 6);

audio[x].play();
}
</script>
</body>

它不工作..不确定我要去哪里错了

最佳答案

给定的代码存在多个问题。

HTML

首先是一个建议。您应该将导航标记重建为更常见,更现代且更易访问的内容,例如:

<nav>
<ul class="navmain">
<li class="navbox"><a href="arms.html">arms</a></li>
<li class="navbox"><a href="map.html">map</a></li>
<li class="navbox"><a href="map.html">map</a></li>
<li class="navbox"><a href="ship.html">ship</a></li>
<li class="navbox"><a href="facts.html">facts</a></li>
<li class="navbox"><a href="speech.html">speech</a></li>
</ul>
</nav>

JavaScript

这是最后一个有效的JavaScript部分:
var current = null;
var audio = [
new Audio("assetts/audio/bird.mp3"),
new Audio("assetts/audio/cannon.mp3"),
new Audio("assetts/audio/fire.mp3"),
new Audio("assetts/audio/monkey.mp3"),
new Audio("assetts/audio/crows.mp3"),
new Audio("assetts/audio/cannon2.mp3")
];

function playSound() {
if (null !== current) {
audio[current].pause();
}

current = Math.floor((Math.random() * 1000) % 6);
audio[current].play();
}

$(document).ready(function() {
$('.navbox').mouseover(playSound);
});

演示

Try before buy

一些进一步的解释
var audio[];将不起作用,例如 string audio[6];有效的C++。但是,您可以像在初始化时那样插入所有元素,也可以创建一个空数组,如下所示:
var audio = [];

jQuery函数不称为 .onmouseover,而是 .mouseover() 。您可以不带花括号地传入函数名称,例如:
$('.navbox').mouseover(playSound);

或者您可以创建一个函数,例如:
$('.navbox').mouseover(function() { playSound(); } );

最后,我将 audio -array设置为全局,这样您就不会在用户每次悬停其中一项时都重新创建它以及所有六个音频元素,并将当前正在播放的一项存储在变量中,以便您可以在停止之前停止它发出新的声音。

希望这可以帮助。

关于jquery - 随机音频onmouseover jQuery不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19048715/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com