2012-04-01 2 views
0

私は質問があります。 JQueryの助けを借りずに自分のデザインのフェーディングメニューシステムを試しています。私の問題はもちろん、ポインタがすぐにメニューを生成する要素(私の場合はUL)に入ることができない完璧な世界では、フェードインまたはフェードアウトするメニューを得ることができます。しかし、カーソルがすぐにULに入り、すぐに出ると、当然私はちらつきの影響を受けます。私は自分自身のデザインの素晴らしいオーバーアーシングイベントハンドラの必要性を想像しています。私はおそらく私はそれをスクラップしてやり直すつもりですので、私はそれが必要になるとは思わないが、私は自分のコードを掲載します。誰もがリンク/アイデア/これで始める良い方向を持っていますか?JQueryを使わないフェードメニュー

javascriptのコード:

window.onload = InitPage; 

function InitPage(){ 

function fadeEffect(child, opacity, direction, e){ 
    if(direction && opacity != 1){ 
     opacity = opacity < 1?Math.round((opacity + 0.05)*100)/100:1; 
     child.style.opacity = opacity; 
     setTimeout(function(){fadeEffect(child, opacity, direction,e)},50); 
    }else if(!direction && opacity != 0){ 
     opacity = opacity > 0?Math.round((opacity - 0.05)*100)/100:0; 
     child.style.opacity = opacity; 
     child.style.display = opacity == 0?"none":"block"; 
     setTimeout(function(){fadeEffect(child, opacity, direction,e)},50); 
    } 
} 

function hoverMenu(e, oTarget){ 
    var isChildOf = function(pNode, cNode){ 
     if(pNode === cNode){ 
      return true; 
     } 

     while (cNode && cNode !== pNode){ 
      cNode = cNode.parentNode; 
     } 

     return cNode === pNode; 
    } 

    var hasChildMenu = function(pNode, cNode){ 
     while(cNode && cNode !== pNode){ 
      if(cNode.className == "ul_menu" || cNode.className == "li_menu"){ 
       cNode.style.display = "block"; 
       //cNode.style.opacity = 1; 
       fadeEffect(cNode,0,true,e); 
       //cNode.timer = setInterval(function(arg1,arg2,arg3){return function(){fadeEffect(arg1,arg2,arg3)}}(cNode,opacity,true),50); 
      } 
      cNode = cNode.previousSibling; 
     } 
     if(e.type == "mouseout"){ 
      e.cancelBubble(); 
     } 
    } 

    var target = e.target; 

    if(!oTarget){ 
     oTarget = target; 
    } 

    var relTarg = e.fromElement; 

    if(isChildOf(oTarget, relTarg) == false){ 
     //alert("mouse enters"); 
     hasChildMenu(oTarget, oTarget.lastChild); 
    } 
} 

function unhoverMenu(e, oTarget){ 

    var isChildOf = function(pNode, cNode){ 
     //check to see if element is a child 
     if(pNode === cNode){ 
      return true; 
     } 

     while (cNode && cNode !== pNode){ 
      cNode = cNode.parentNode; 
     } 

     return cNode === pNode; 
    } 

    var hasChildMenu = function(pNode, cNode){ 
     while(cNode && cNode !== pNode){ 
      if(cNode.className == "ul_menu" || cNode.className == "li_menu"){ 
       //cNode.style.opacity = 0; 
       //cNode.style.display = "none"; 
       fadeEffect(cNode,1,false,e); 
      } 
      cNode = cNode.previousSibling; 
     } 
     if(e.type == "mouseover"){ 
      e.cancelBubble(); 
     } 
    } 

    var target = e.target; 

    if(!oTarget){ 
     oTarget = target; 
    } 

    var relTarg = e.toElement; 

    if(isChildOf(oTarget, relTarg) == false){ 
     hasChildMenu(oTarget, oTarget.lastChild); 
    } 

    function MenuEventHandler (e, oTarget){ 
    } 
} 

var ul_menu = document.getElementById("ul_grabbed"); 
ul_menu.addEventListener("mouseover", function(e1){return function(e){hoverMenu(e, e1)}}(ul_menu),false); 
ul_menu.addEventListener("mouseout", function(e1){return function(e){unhoverMenu(e, e1)}}(ul_menu),false); 
//document.addEventListener("mouseover", hoverMenu,false); 
//document.addEventListener("mouseout",unhoverMenu,false); 
} 

HTMLコード:

<html> 
<head> 
    <script type="text/javascript" src="bubble.js"></script> 
    <link rel="stylesheet" href="bubble_style.css" type="text/css" media="screen" /> 
</head> 
<body> 
    <div id="background"> 
     <div id="menu_section"> 
      <ul id="ul_grabbed" class="ul_menu">Menu 1 
       <li class="li_menu">item 1</li> 
       <li class="li_menu">item 2</li> 
       <li class="li_menu">item 3</li> 
       <li class="li_menu">item 4</li> 
      </ul> 
      <ul class="ul_menu"> 
       Menu 2 
      </ul> 
     </div> 
    </div> 
</body> 
</html> 

CSSコード:実際のプロジェクト、jQueryのせずにこれを行う(またはいくつかの他のライブラリ)で

*{margin:0; padding:0} 
body{margin:0; padding:0;background-color:black} 

div#background{ 
margin: 0 auto 0 auto; 
display:block; 
width:800px; 
height:100%; 
vertical-align:middle; 
background-color: green;} 

div#menu_section{ 
margin:auto; 
width:800px; 
height:40px; 
background-color:purple;} 

div#monkey{ 
width:400px; 
height:400px; 
background-color:red;} 

ul.ul_menu{ 
list-style:none; 
display:block; 
height:40px; 
width:400px; 
float:left; 
text-align:center; 
color:white; 
} 

li.li_menu{ 
height:20px; 
display:none; 
position:relative; 
text-align:center; 
color:white; 
background-color:black; 
opacity:0;} 
+0

このような例は、[JSFiddle](http://jsfiddle.net/)を作成する必要があり、私たちは見ることができます。 – josh3736

+0

ステップ1はjQueryを見ることです。内部的な畳み込みによって完全に混乱することはありません。ステップ2は最初の原則に戻り、良い技術的アドバイスを得るフォーラムで尋ねる(comp.lang.javascriptはそれに適しています)。ステップ3はそれを維持することです。あなたは[MyLibrary](http://www.cinsoft.net/mylib.html)を見るよりも悪くなる可能性があります。 – RobG

答えて

0

私はこれに対する解決策を考えています。以下のコードは、私が勉強しようとしていたことを示し、おそらく他の人が同じことを見るのを助けることができます。

HTML/CSS

<html> 
<head> 
    <title>Test</title> 
    <script type="text/javascript" src="test_menu.js"></script> 
    <style type="text/css" media="screen"> 
     #menu_header{ 
      background-color:#617A2A; 
      color:#C5DE8E; 
      height:40px; 
      width:120px; 
      text-align:center; 
      display:block;} 

     #menu{ 
      opacity:0; 
      display:none; 
      position:absolute; 
      background-color:#AEC971; 
      width:120px;}   

     #menu div{ 
      text-align:center;} 

     #menu a, #menu a:link, #menu a:visited{ 
      color:#FFF; 
      display:block; 
      text-decoration:none;} 

     #menu a:hover{ 
      background-color:#414A2C;} 

     #info_area{ 
      border: 1px black solid; 
      position:float-left; 
      margin-top:20px; 
      width:512px; 
      height:512px; 
      overflow:auto;} 
    </style> 
</head> 
<body> 
    <div id="menu_header"> 
     Something 
    </div> 
    <div id="menu"> 
     <div><a href="http://www.google.com">Google</a></div> 
     <div><a href="http://www.yahoo.com">Yahoo</a></div> 
    </div> 
    <div id="info_area"> 
    </div> 
</body> 
</html> 

Javascriptを:

window.onload = InitPage; 

function InitPage(){ 

var menu = document.getElementById("menu"); 
var menu_header = document.getElementById("menu_header"); 
var opacity_global = menu.style.opacity == ""?0:menu.style.opacity; 
var timer = 0; 

function fadeEffect(opacity, direction){ 
    if(direction && opacity != 1){ 
     opacity_global = opacity < 1?Math.round((opacity + 0.05)*100)/100:1; 
     menu.style.opacity = opacity_global; 
     if(opacity_global == 1){ 
      window.clearInterval(timer); 
     } 
     //setTimeout(function(){fadeEffect(child, opacity, direction);},50); 
    }else if(!direction && opacity != 0){ 
     opacity_global = opacity > 0?Math.round((opacity - 0.05)*100)/100:0; 
     menu.style.opacity = opacity_global; 
     menu.style.display = opacity_global == 0?"none":"block"; 
     if(opacity_global == 0){ 
      window.clearInterval(timer); 
     } 
     //setTimeout(function(){fadeEffect(child, opacity, direction);},50); 
    } 
} 

function eventHandler(e, menu, menu_header){ 
    var target = e.target; 
    var menu_headerTarget = menu_header; 
    var menuTarget = menu; 
    var fromTarget = e.fromElement; 
    var toTarget = e.toElement; 
    var info = document.getElementById("info_area"); 

    var isChild = function(pNode, cNode){ 
     while(cNode && pNode !== cNode){ 
      cNode = cNode.parentNode; 
     } 
     return pNode === cNode; 
    } 

    //alert("e.type: " + e.type + "\ntarget: " + target + "\ntoElement: " + toTarget + "\nfromElement: " + fromTarget); 

    if((isChild(target, fromTarget) == false) && (e.type == "mouseover") && (isChild(menuTarget,target)==false) && (isChild(menuTarget, fromTarget)==false)){ 
     window.clearInterval(timer); 
     info.innerHTML += "<br />mouseover!"; 
     menuTarget.style.display = "block"; 
     timer = window.setInterval(function(){return fadeEffect(opacity_global, true);},25); 
     //menuTarget.timer = setInterval(function(arg){arg.style.opacity += 0.05}(menuTarget),50); 
    }else if((fromTarget === menu_headerTarget) && (isChild(menuTarget,toTarget) == false) && e.type == "mouseout"){ 
     window.clearInterval(timer); 
     info.innerHTML += "<br />mouseout 1"; 
     //menuTarget.style.display = "none"; 
     timer = window.setInterval(function(){return fadeEffect(opacity_global, false);},25); 
     //setTimeout(function(menuTarget){return function(){menuTarget.style.display = "none"}}(menuTarget),1000); 
    } 
    else if((isChild(menuTarget, target) == true) && (isChild(menuTarget, toTarget) == false) && (toTarget !== menu_headerTarget) && e.type == "mouseout"){ 
     window.clearInterval(timer); 
     info.innerHTML += "<br />mouseout 2"; 
     //menuTarget.style.display = "none"; 
     //setTimeout(function(menuTarget){return function(){menuTarget.style.display = "none"}}(menuTarget),1000); 
     timer = window.setInterval(function(){fadeEffect(opacity_global, false);},25); 
    }else{ 
     info.innerHTML += "<br />Stopped an event!! " + "type: " + e.type + " target: " + target; 
     e.stopPropagation(); 
    } 
} 

menu_header.addEventListener("mouseover",function(e){eventHandler(e, menu, menu_header);},false); 
menu_header.addEventListener("mouseout",function(e){eventHandler(e, menu, menu_header);},false); 
menu.addEventListener("mouseout",function(e){eventHandler(e, menu, menu_header);},false); 
menu.addEventListener("mouseover",function(e){eventHandler(e, menu, menu_header);},false); 
} 
0

うあなたが刑罰の大食主であるかどうか疑問に思ってください。クロスブラウザアニメーションは、既に解決されている比較的困難な問題です。これは学習経験があるため

しかし、ここでいくつかのポインタがあります:起動する

+1

Gluttonか、それがうまくいくかを知りたがっているだけです。私たちはここで少なくとも少しはマゾヒストです。これらの "マシン"で、何が正しいかどうかについてのわかりやすい考えを持って自分自身を育てています。 –

+1

それは公正です、@ JaredFarrishです。これが学習の練習であれば、[jQueryのアニメーションの仕組みを調べる](https://github.com/jquery/jquery/blob/master/src/effects.js)を開始するのがよいでしょう。 – josh3736

+0

これは正直なところ、学習実験です。私は誰もがそのような問題を避けたと思っています:) – Mike

関連する問題