我正在制作一个钢琴菜单,我希望能够立即将鼠标移出并重新播放以重播声音,但是当我再次播放该键时,我会感到延迟。
到目前为止,使用Stackoverflow,我已经提出了以下内容,而我可以在悬停时播放关键音符但是它不像钢琴那样感觉太多。
$(function(){
var Key_A = $('.KEY_A');
var Key_B = $('.KEY_B');
var A_Audio = Key_A.find('audio')[0];
var B_Audio = Key_B.find('audio')[0];
Key_A.hover(function(){
A_Audio.play();
}, function(){
A_Audio.stop();
});
Key_B.hover(function(){
B_Audio.play();
}, function(){
B_Audio.stop();
});
});
* {
margin:0;
padding:0;
}
body {background-color:#222}
p {margin:0 0 1.5em}
*,
*:before,
*:after {box-sizing:border-box}
/*
Pure CSS3 Vertical Piano Menu by Taufik Nurrohman (http://gplus.to/tovic)
Licence: Attribution
*/
#nav-piano {
display:block;
background-color:#222;
background-image:linear-gradient(60deg,#111,#333,#111,#3c3c3c,#333 70%);
width:300px;
padding:0 15px 40px 0;
border-right:1px solid #111;
border-bottom:1px solid #111;
border-radius:0 0 7px 0;
box-shadow:2px 0 0 #666,3px 0 0 #555,4px 0 0 #444,6px 0 6px black,inset -1px 0 1px rgba(255,255,255,.5),inset -4px 0 5px black,-5px 0 10px black,-2px 10px 14px black;;
float:left;
margin-top:-10px;
margin-bottom:200px;
}
#nav-piano ul,
#nav-piano li {
margin:0;
padding:0;
list-style:none;
position:relative;
}
#nav-piano ul {box-shadow:0 2px 5px black}
#nav-piano a,
#nav-piano .tut {
display:block;
height:40px;
padding:0 18px 0 0;
background-color:#fafafa;
background-image:linear-gradient(-30deg,#f5f5f5,white);
border:1px solid #ccc;
box-shadow:inset 1px 0 0 white,inset -1px 0 0 white,inset 0 0 0 1px white,4px 0 3px rgba(0,0,0,.4);
border-radius:0 3px 3px 0;
text-align:right;
text-decoration:none;
color:#666;
text-shadow:0 1px 0 white;
font:italic normal 14px/37px Georgia,"URW Bookman L",Serif;
outline:none;
position:relative;
overflow:hidden;
}
#nav-piano a:focus,
#nav-piano a:active {outline:none}
#nav-piano .black-tut a,
#nav-piano .black-tut .tut {
width:58%;
z-index:2;
background-color:#111;
background-image:linear-gradient(-20deg,#333,black,#333);
height:26px;
line-height:24px;
font-size:11px;
text-shadow:0 -1px 0 black;
color:#555;
margin-top:-13px;
margin-bottom:-13px;
border-width:2px 7px 2px 1px;
border-style:solid;
border-color:#666 #222 #111 #555;
box-shadow:inset -1px 0 2px rgba(255,255,255,.4),2px 0 3px rgba(0,0,0,.4);
}
#nav-piano a:active {
background-color:#f5f5f5;
margin-right:1px;
box-shadow:0 2px 2px rgba(0,0,0,.4);
}
#nav-piano .black-tut a:active {
border-right-width:2px;
left:-2px;
box-shadow:inset -1px 0 1px rgba(255,255,255,.4),1px 0 0 rgba(0,0,0,.8),0 0 2px rgba(0,0,0,.4),1px 0 0 black;
}
#nav-piano li ul {
position:absolute;
top:-10px;
left:100%;
margin-left:-10px;
width:200px;
background-color:#ffa;
padding:10px 0;
z-index:999;
overflow:hidden;
visibility:hidden;
opacity:0;
transition:all .2s ease-out;
}
#nav-piano li ul a,
#nav-piano li ul a:active {
background:none;
border:none;
border-radius:0;
box-shadow:none;
height:24px;
line-height:24px;
color:#8b8b55;
text-shadow:none;
margin-right:0;
}
/* Depth Shadow */
#nav-piano > ul > li > a:active:before,
#nav-piano > ul > li > a:active:after {
content:"";
position:absolute;
top:0;
right:0;
left:0;
height:15px;
background-color:rgba(0,0,0,.1);
transform:rotate(1deg) translateY(-15px);
}
#nav-piano > ul > li > a:active:after {
top:auto;
bottom:0;
transform:rotate(-1deg) translateY(14px);
}
#nav-piano li ul a:hover {background-color:#fc6}
#nav-piano li:hover ul {
visibility:visible;
opacity:1;
margin-left:-5px;
}
/* Content area */
#content {
display:block;
margin-left:350px;
padding:30px 40px 0 0;
font:normal normal 13px/1.4 Georgia,"URW Bookman L",Serif;
color:#777;
text-shadow:0 1px 1px black;
max-width:700px;
}
#content h2 {
font:italic normal 300%/normal "Bodoni MT",Didot,"Didot LT STD","Hoefler Text",Garamond,"Times New Roman",Times,Serif;
color:#584D46;
margin:0 0 .4em;
}
#content h2:before {
content:"*";
font-style:normal;
}
#content a {
color:#666;
text-decoration:none;
}
#content a:hover {color:#999}
#content footer {
border-top:1px dashed #333;
padding-top:1.5em;
margin-top:2em;
font-size:11px;
font-style:italic;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<nav id="nav-piano">
<ul>
<li><span class="tut"></span></li>
<li><a href="#">Home</a></li>
<li class="black-tut"><a href="#">Alternate Menu 1</a></li>
<li><a href="#">About</a></li>
<li class="black-tut"><span class="tut"></span></li>
<li><a href="#">Archive</a></li>
<li class="black-tut"><span class="tut"></span></li>
<li><a href="#">Contact</a></li>
<li><span class="tut"></span></li>
<li class="black-tut"><span class="tut"></span></li>
<li><span class="tut">D</span></li>
<li class="black-tut"><span class="tut"></span></li>
<li><span class="tut">C</span></li>
<li>
<span class="tut KEY_B">
B
<audio src="http://rafflebananza.com/Admin/Desktop/piano-b.wav" preload="auto"></audio>
</span>
</li>
<li class="black-tut"><span class="tut"></span></li>
<li>
<span class="tut KEY_A">
A
<audio src="http://rafflebananza.com/Admin/Desktop/piano-a.wav" preload="auto"></audio>
</span>
</li>
<li class="black-tut"><span class="tut"></span></li>
<li><span class="tut"></span></li>
<li class="black-tut"><span class="tut"></span></li>
<li><span class="tut"></span></li>
</ul>
</nav>
<!-- Other stuff -->
<section id="content">
<h2>Lorem Ipsum</h2>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p><p>Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.</p>
<footer>Posted by <a href="http://gplus.to/tovic">Taufik Nurrohman</a></footer>
</section>
答案 0 :(得分:0)
问题是因为你正在调用&#34; audio.stop()&#34;什么时候没有这种方法。在尝试再次播放之前,您应该暂停并重置剪辑,如下所示。
参考:MDN - Media Element Properties and Methods
要查看并尝试完整代码,请点击&#34;显示代码段&#34;然后&#34;运行代码段&#34;。
Key_A.hover(function() {
A_Audio.pause();
A_Audio.currentTime = 0;
A_Audio.play();
});
Key_B.hover(function() {
B_Audio.pause();
B_Audio.currentTime = 0;
B_Audio.play();
});
$(function(){
var Key_A = $('.KEY_A');
var Key_B = $('.KEY_B');
var A_Audio = Key_A.find('audio')[0];
var B_Audio = Key_B.find('audio')[0];
Key_A.hover(function() {
A_Audio.pause();
A_Audio.currentTime = 0;
A_Audio.play();
});
Key_B.hover(function() {
B_Audio.pause();
B_Audio.currentTime = 0;
B_Audio.play();
});
});
&#13;
* {
margin:0;
padding:0;
}
body {background-color:#222}
p {margin:0 0 1.5em}
*,
*:before,
*:after {box-sizing:border-box}
/*
Pure CSS3 Vertical Piano Menu by Taufik Nurrohman (http://gplus.to/tovic)
Licence: Attribution
*/
#nav-piano {
display:block;
background-color:#222;
background-image:linear-gradient(60deg,#111,#333,#111,#3c3c3c,#333 70%);
width:300px;
padding:0 15px 40px 0;
border-right:1px solid #111;
border-bottom:1px solid #111;
border-radius:0 0 7px 0;
box-shadow:2px 0 0 #666,3px 0 0 #555,4px 0 0 #444,6px 0 6px black,inset -1px 0 1px rgba(255,255,255,.5),inset -4px 0 5px black,-5px 0 10px black,-2px 10px 14px black;;
float:left;
margin-top:-10px;
margin-bottom:200px;
}
#nav-piano ul,
#nav-piano li {
margin:0;
padding:0;
list-style:none;
position:relative;
}
#nav-piano ul {box-shadow:0 2px 5px black}
#nav-piano a,
#nav-piano .tut {
display:block;
height:40px;
padding:0 18px 0 0;
background-color:#fafafa;
background-image:linear-gradient(-30deg,#f5f5f5,white);
border:1px solid #ccc;
box-shadow:inset 1px 0 0 white,inset -1px 0 0 white,inset 0 0 0 1px white,4px 0 3px rgba(0,0,0,.4);
border-radius:0 3px 3px 0;
text-align:right;
text-decoration:none;
color:#666;
text-shadow:0 1px 0 white;
font:italic normal 14px/37px Georgia,"URW Bookman L",Serif;
outline:none;
position:relative;
overflow:hidden;
}
#nav-piano a:focus,
#nav-piano a:active {outline:none}
#nav-piano .black-tut a,
#nav-piano .black-tut .tut {
width:58%;
z-index:2;
background-color:#111;
background-image:linear-gradient(-20deg,#333,black,#333);
height:26px;
line-height:24px;
font-size:11px;
text-shadow:0 -1px 0 black;
color:#555;
margin-top:-13px;
margin-bottom:-13px;
border-width:2px 7px 2px 1px;
border-style:solid;
border-color:#666 #222 #111 #555;
box-shadow:inset -1px 0 2px rgba(255,255,255,.4),2px 0 3px rgba(0,0,0,.4);
}
#nav-piano a:active {
background-color:#f5f5f5;
margin-right:1px;
box-shadow:0 2px 2px rgba(0,0,0,.4);
}
#nav-piano .black-tut a:active {
border-right-width:2px;
left:-2px;
box-shadow:inset -1px 0 1px rgba(255,255,255,.4),1px 0 0 rgba(0,0,0,.8),0 0 2px rgba(0,0,0,.4),1px 0 0 black;
}
#nav-piano li ul {
position:absolute;
top:-10px;
left:100%;
margin-left:-10px;
width:200px;
background-color:#ffa;
padding:10px 0;
z-index:999;
overflow:hidden;
visibility:hidden;
opacity:0;
transition:all .2s ease-out;
}
#nav-piano li ul a,
#nav-piano li ul a:active {
background:none;
border:none;
border-radius:0;
box-shadow:none;
height:24px;
line-height:24px;
color:#8b8b55;
text-shadow:none;
margin-right:0;
}
/* Depth Shadow */
#nav-piano > ul > li > a:active:before,
#nav-piano > ul > li > a:active:after {
content:"";
position:absolute;
top:0;
right:0;
left:0;
height:15px;
background-color:rgba(0,0,0,.1);
transform:rotate(1deg) translateY(-15px);
}
#nav-piano > ul > li > a:active:after {
top:auto;
bottom:0;
transform:rotate(-1deg) translateY(14px);
}
#nav-piano li ul a:hover {background-color:#fc6}
#nav-piano li:hover ul {
visibility:visible;
opacity:1;
margin-left:-5px;
}
/* Content area */
#content {
display:block;
margin-left:350px;
padding:30px 40px 0 0;
font:normal normal 13px/1.4 Georgia,"URW Bookman L",Serif;
color:#777;
text-shadow:0 1px 1px black;
max-width:700px;
}
#content h2 {
font:italic normal 300%/normal "Bodoni MT",Didot,"Didot LT STD","Hoefler Text",Garamond,"Times New Roman",Times,Serif;
color:#584D46;
margin:0 0 .4em;
}
#content h2:before {
content:"*";
font-style:normal;
}
#content a {
color:#666;
text-decoration:none;
}
#content a:hover {color:#999}
#content footer {
border-top:1px dashed #333;
padding-top:1.5em;
margin-top:2em;
font-size:11px;
font-style:italic;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<nav id="nav-piano">
<ul>
<li><span class="tut"></span></li>
<li><a href="#">Home</a></li>
<li class="black-tut"><a href="#">Alternate Menu 1</a></li>
<li><a href="#">About</a></li>
<li class="black-tut"><span class="tut"></span></li>
<li><a href="#">Archive</a></li>
<li class="black-tut"><span class="tut"></span></li>
<li><a href="#">Contact</a></li>
<li><span class="tut"></span></li>
<li class="black-tut"><span class="tut"></span></li>
<li><span class="tut">D</span></li>
<li class="black-tut"><span class="tut"></span></li>
<li><span class="tut">C</span></li>
<li>
<span class="tut KEY_B">
B
<audio src="http://rafflebananza.com/Admin/Desktop/piano-b.wav" preload="auto"></audio>
</span>
</li>
<li class="black-tut"><span class="tut"></span></li>
<li>
<span class="tut KEY_A">
A
<audio src="http://rafflebananza.com/Admin/Desktop/piano-a.wav" preload="auto"></audio>
</span>
</li>
<li class="black-tut"><span class="tut"></span></li>
<li><span class="tut"></span></li>
<li class="black-tut"><span class="tut"></span></li>
<li><span class="tut"></span></li>
</ul>
</nav>
<!-- Other stuff -->
<section id="content">
<h2>Lorem Ipsum</h2>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p><p>Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.</p>
<footer>Posted by <a href="http://gplus.to/tovic">Taufik Nurrohman</a></footer>
</section>
&#13;