javascript - 将HTML悬停在 block 元素上时运行Javascript?

标签 javascript html audio triggers hover

我目前正在尝试使我的Tumblr页面尽可能的好,并将其与其他页面区分开。边栏带有菜单项,当用户将鼠标悬停在菜单栏上时,菜单项会突出显示并向左移动。但是,当:hover触发时,我也想播放声音。

这是侧边栏菜单代码。

#sidebar{
margin-left:85px;
margin-top:50px;
{block:IfNotStaticSidebar}position:absolute;{block:IfNotStaticSidebar}
{block:IfStaticSidebar}position:fixed;{/block:IfStaticSidebar}
background-color:{color:Sidebar};
width:200px;
padding:10px;
text-align:justify;
-moz-border-radius: 0px 50px 0px 0px;
-webkit-border-radius: 0px 50px 0px 0px;
-o-border-radius: 0px 50px 0px 0px;
border-radius: 0px 50px 0px 0px;
}

#description{
width:90%;
margin:10px;
}

a.block {
display:inline;
text-align: left;
font-size:10pt;
padding:3px;  
margin:5px;  
position:relative;
background-color:{color:color4};
display: inline-block;
font-family : arial;
color: {color:Menu Links Font};
font-weight: bold;
width: 100px;
transform: rotate(0deg); 
-moz-transform: rotate(0deg); 
-webkit-transform: rotate(0deg); 
-o-transform: rotate(0deg);
-webkit-transition: all .5s ease;
-moz-transition: all .5s ease;
-o-transition: all .5s ease;
}

a.block:hover {
color: {color:Menu Links Font Hover};
background-color:{color:color5};
padding-left:20px;
-webkit-transition: all .5s ease;
-moz-transition: all .5s ease;
-o-transition: all .5s ease;
text-shadow:0px 0px 5px {color:Shadow};
}

这是我要适应的声音代码。
<script language="javascript" type="text/javascript">
function playSoun
d(soundfile) {
document.getElementById("dummy").innerHTML=
"<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
}
</script>
<span id="dummy"></span>
<p onmouseover="playSound('http://www.freesound.org/data/previews/141/141259_2555977-lq.mp3');"> Hover over this text </p>

我将不胜感激与此有关的任何帮助!

最佳答案

checkout this article

它解释得非常完美! :)

好吧,这是使用jQuery的一个技巧:

$("p").mouseenter(function(){
    $("<audio></audio>").attr({ 
        'src':'audio_src', 
        'volume':0.4,
        'autoplay':'autoplay'
    }).appendTo("body");
});

参见演示here

关于javascript - 将HTML悬停在 block 元素上时运行Javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17201161/

相关文章:

javascript - 由于 Wistia 嵌入视频,页面加载速度太慢

javascript - IE9 和 self.close()

javascript - $anchorScroll 仅在 ng-view 加载之后,而不是之前

javascript - html 5 计数器的 drop 功能

javascript - 使用 Node Webkit 读取 MP4 文件

audio - 我想知道 'd-vector'进行说话人区分

java.io.IOException : mark/reset not supported

javascript - 如何在 React Native 上更改 iOS 中的 Audio Session 类别?

javascript - jsreport-核心 : using header with phantom-pdf receipe

javascript - 在脚本文本内容中 react setState