【发布时间】:2017-03-07 14:58:45
【问题描述】:
好的。。
我有这个网站:http://stephaniie.com/_testlab/beta1/
{ 完整的html代码:http://pastebin.com/u4HpxcB2 }
如果你向下滚动,在右边,塔旁边有一个骑士..
- 如果您将鼠标从他的右侧移动.. 将弹出一个更大的骑士图像。
- 如果您将鼠标移近他,动画就会开始,他开始“说话”
这是通过一些非常简单的编码完成的。
->
为了让骑士形象出现,使用了这个 CSS 和 Javascript:
原文来源:http://clba.nl/sitepoint/img-hover-demo-js1.htm
<style>
#img2{
position: absolute;
top: 1400px;
left: 50px;
display: none;
}
#img1:hover + #img2 {display:block}
</style>
<script>
var img1 = document.getElementById("sol,
img2 = document.getElementById("img2");
img1.onmouseover = function(){
img2.style.display = "block";
}
img1.onmouseout = function(){
img2.style.display = "none";
}
</script>
使用 PlaySound/StopSound Javascript 开始播放音乐。
原文来源:Javascript play sound on hover. stop and reset on hoveroff
<script>
function PlaySound(soundobj) {
var thissound=document.getElementById(soundobj);
thissound.play();
}
function StopSound(soundobj) {
var thissound=document.getElementById(soundobj);
thissound.pause();
thissound.currentTime = 0;
}
</script>
问题:当我将 Knight 悬停时如何结合这两个功能?
对于网站上的 HTML,我使用“”和“”来制作图像地图。
代码是:
<map name="map12" id="img_id12">
<area class="youtube" coords="1497,52,1588,128" shape="rect" href="http://www.youtube.com/embed/GPbUA6dCR8k" style="outline:none;"
onmouseover="if(document.images) document.getElementById('img_id12').src= 'assets/images/text/day/12.gif';"
onmouseout="if(document.images) document.getElementById('img_id12').src= 'assets/images/no-text/day/12.gif';" />
<area class="youtube" coords="3878,24,3957,96" shape="rect" href="https://www.youtube.com/embed/skV-q5KjrUA" style="outline:none;"
onmouseover="if(document.images) document.getElementById('img_id12').src= 'assets/images/text/day/12solaire-ani.gif'; PlaySound('solaire'); "
onmouseout="if(document.images) document.getElementById('img_id12').src= 'assets/images/no-text/day/12.gif'; StopSound('solaire'); PlaySound('solaire-stop');" />
<area id="sol" coords="3890,23,3970,100" shape="rect" style="outline:none;" />
</map>
我唯一想要的就是在我的 代码中添加一个图像显示功能。 所以,
<img id="img2" src="solaire.gif" alt="" style="display: none;">
当“鼠标悬停功能”被激活时变成这个。
<img id="img2" src="solaire.gif" alt="" style="display: block;">
评论:正如您在上面的代码和网站上看到的那样,我可以使用图像悬停和播放/停止声音,但不能同时使用。有没有办法同时使用这两个脚本?如果您想知道 "onmouseover="if(document.images) document.getElementById('img_id12').src= 'assets/images/text/day/12solaire-ani.gif';" 的作用。它用于更改背景使用 ID 发送给另一个人。
<img src="assets/images/no-text/day/12.gif" usemap="#map12" id="img_id12" class="first" />
我尝试将此“图像显示/隐藏”脚本添加到地图区域代码中的“鼠标悬停”。像这样。
<area id="sol" class="youtube" ...
onmouseover="if(document.images) document.getElementById('img_id12').src= 'assets/images/text/day/12solaire-ani.gif'; PlaySound('solaire'); "
</area
这有效.. 但是 PlaySound/StopSound 不再有效。然而,彩盒和不断变化的图像仍然有效。所以问题是添加了显示/隐藏图像功能并且仍然有 PlaySound/StopSound 功能工作。
编辑和更多信息: 我还使用了两个名为 Colorbox 和 Responsive Image Map 的 Javascript 工具。 * Colorbox 是一个 jQuery 灯箱脚本。来源:http://www.jacklmoore.com/colorbox/ * 响应式图像映射允许图像映射随屏幕大小调整大小。来源:http://mattstow.com/experiment/responsive-image-maps/rwd-image-maps.html
在索引页面上使用它们的代码是这样的。
<script>
$(document).ready(function(e) {
$('img[usemap]').rwdImageMaps();
});
$(document).ready(function(){
//Examples of how to assign the Colorbox event to elements
$(".youtube").colorbox({iframe:true, innerWidth:1024, innerHeight:576});
});
/script>
可在此处找到响应式图像映射脚本和 Colorbox 的完整代码。
响应式图像映射脚本: - 链接 -> http://pastebin.com/699T5kLY
彩盒: - 链接 -> http://pastebin.com/Jj4SQEhu - 链接 -> http://pastebin.com/bErhvPFA
【问题讨论】:
-
你试过这样的事情吗? ` img1.onmouseover = function(){ img2.style.display = "block";播放声音(); }`
-
不,在您问题的第一个
<script>块中,您有img1.onmouseover = function() {...},添加PlaySound();试试看。 -
你能不能再试一下代码,还要在
PlaySound()函数中加入:console.log('I should now here sound:', soundobj);。在您的浏览器 devTools 中,您“应该”在触发.onmouseover事件时看到该消息。 -
刚刚注意到,您的 JS 中也存在语法错误:
var img1 = document.getElementById("sol,") 结尾。 -
是的。我改变了那个语法错误。 =) Ty 看到它!
标签: javascript jquery html css