私は、ヘッダーをメニューにフェードするonmouseoverイベントを持つ要素(私のページヘッダーを含む)を持っています。また、その逆を行うonmouseoutイベントがあります。メニュー自体は単にリンクと画像の束です。マウスが入ると、期待どおりに消えます。しかし、内のマウスを<div>
内に移動すると、onmouseoutイベントは、私が残している要素によってトリガされ、続けて入力している要素のonmouseoverイベントがトリガされます。 stopPropagationとcancelBubbleを使用してこれを停止しようとしましたが、役に立たないです。stopPropagation()およびcancelBubbleにもかかわらず、子プロセスが引き続きトリガーしているイベント
のJavascript(menu.jsに格納されている):
//These two functions are for rendering the menu.
//Fade is a function that fades an element out or in by
//changing the opacity of the element.
function renderHeader() {
fade('menu');
setTimeout("document.getElementById('menu').innerHTML=\
\"<img src='menu_1.png' width=20% /><img src='menu_2.png' width=20% />\
<img src='menu_3.png' width=20% /><img src='menu_4.png' width=20% />\
<img src='menu_5.png' width=20% />\"",TimeToFade+33);
setTimeout("fade('menu')",TimeToFade+66);
}
function renderMenu() {
fade('menu');
setTimeout("document.getElementById('menu').innerHTML=\
\"<a href='home.asp'><img src='menu_a.png' width=20% /></a>\
<a href='members.asp'><img src='menu_b.png' width=20% /></a>\
<a href='locations.asp'><img src='menu_c.png' width=20% /></a>\
<a href='about.asp'><img src='menu_d.png' width=20% /></a>\
<a href='services.asp'><img src='menu_e.png' width=20% /></a>\"",TimeToFade+33);
setTimeout("fade('menu')",TimeToFade+66);
}
//These two functions are responsible for stopping the propagation.
function load() {
element = document.getElementById("menu");
element.addEventListener("mouseover", stopEvent, false);
element.addEventListener("mouseout", stopEvent, false);
}
function stopEvent(ev) {
if (!ev) var ev = window.event;
ev.cancelBubble = true;
if (ev.stopPropagation) ev.stopPropagation();
}
HTML(要素が最初にヘッダを示す):
<head>
...
<script type="text/javascript" src="Scripts/menu.js"></script>
</head>
<body onload="load();">
<div id='menu' style="opacity:1;filter:alpha(opacity=100);height:150px"
onmouseover="renderMenu()" onmouseout="renderHeader()">
<img src='menu_1.png' width=20% /><img src='menu_2.png' width=20% />
<img src='menu_3.png' width=20% /><img src='menu_4.png' width=20% />
<img src='menu_5.png' width=20% /></div>
実際には、実際の機能でタイムアウトを設定することを検討してください。 ( "setTimeout()"の最初の引数は、関数であり、文字列でなければなりません。つまり、 – Pointy
ありがとうございます。私はそれが可能であるかどうかはわかりませんでした。いずれにせよ、私はこの時点でjQueryに切り替えたので、もうsetTimeoutは使用していません。 – err1100