2017-07-12 10 views
1

2つのボタンのいずれかをクリックすると、表示されるオーバーレイdivが表示されます。 1つのボタンをクリックすると、その要素が特定のZインデックスで表示され、同じ要素が再びクリックされると、その要素は再び非表示になります。しかし、要素が表示され、2番目のdivがクリックされた場合は、オーバーレイのz-indexを変更するだけです.2番目のボタンを再度クリックすると、オーバーレイが消えてしまいます。私はこれをIf ElseステートメントとjQueryで実現しようとしています。ここにコードがあります。オーバレイを表示させることはできますが、意図したとおりにZインデックスを消したり変更したりすることはできません。あなたが値になるようにZインデックスの値を設定しているあなたは、あなたのzインデックスZ-インデックス条件でdivを非表示にするElse-ifステートメント

$(".block").css("z-index", "+1"); 

の値をインクリメントしているあなたのラインで

$(document).ready(function() { 
 

 
    $(".dot").click(function(){ 
 
     $(this).data("clicked", true); 
 
    }); 
 

 
    $(".burgerMenu").click(function() { 
 
    $(this).data("clicked", true); 
 

 
    //Overlay Animation 
 
    if ($(this).data("clicked") && $(".block").is(":visible")) { 
 
     //alert("IT WORKS!"); 
 
     $(".block").css("z-index", "+1"); 
 
    } else if ($(this).data("clicked") && $(".block").is(":hidden")) { 
 
     $(".block").css("z-index", "+1"); 
 
     $(".block").css("display", "block"); 
 
    } else if ($(this).data("clicked") && $(".block").css("z-index") > "-1") { 
 
     $(".block").css("display", "none"); 
 
    } else { 
 
     alert("Ah lads."); 
 
    } 
 
    }); 
 
});
/* FAB SECTION */ 
 
.dot 
 
{ 
 
    position: absolute; 
 
    bottom: 100px; 
 
    left: 200px; 
 
} 
 

 
#fab { 
 
    position: absolute; 
 
    color: white; 
 
    font-size: 18px; 
 
    text-align: center; 
 
    width: 100px; 
 
    height: 100px; 
 
    background-color: #6eadea; 
 
    border-radius: 50%; 
 
    box-shadow: 6px 10px 18px #686868; 
 
} 
 

 
#plusContain { 
 
    position: relative; 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
#plus { 
 
    position: relative; 
 
    margin: auto; 
 
    top: 50%; 
 
    transform: translateY(-50%); 
 
} 
 

 
/* DIALOGUE/MENU OVERLAY */ 
 

 
.block { 
 
    position: fixed; 
 
    width: 100%; 
 
    height: 100%; 
 
    top: 0; 
 
    background-color: red; 
 
    opacity: 0.8; 
 
    display: none; 
 
    z-index: -1; 
 
} 
 

 

 
/* BURGER MENU SECTION */ 
 

 
.burgerMenu { 
 
    position: absolute; 
 
    margin: auto; 
 
    height: 50px; 
 
    width: 50px; 
 
    cursor: pointer; 
 
    bottom: 10px; 
 
} 
 

 
.bar { 
 
    height: 6px; 
 
    width: 100%; 
 
    background-color: black; 
 
} 
 

 
#top { 
 
    position: relative; 
 
    top: 5px; 
 
} 
 

 
#middle { 
 
    position: relative; 
 
    top: 15px; 
 
} 
 

 
#bottom { 
 
    position: relative; 
 
    top: 25px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 

 
<div id="fab" class="dot dotDown"> 
 
    <div id=plusContain class="fab rotate"> 
 
    <div id="plus">+</div> 
 
    </div> 
 
</div> 
 

 
<!--MENU OVERLAY--> 
 
<div id="overlay" class="block"></div> 
 

 
<!--Burger--> 
 
<div id="burger" class="burgerMenu"> 
 
    <div id="top" class="bar barTop"></div> 
 
    <div id="middle" class="bar barMid"></div> 
 
    <div id="bottom" class="bar barBot"></div> 
 
</div>

+2

質問は何ですか?無関係なコードがなぜ私たちを助けてくれるのですか?単に問題を混乱させるだけで、問題がどこに集中するのかが難しくなります。これを[mcve]に変換します。オーバーレイがどこにあるのかは分かりません – charlietfl

+0

謝罪、完了! – Brian

答えて

1

正しくあなたを応答しない場合、私は、あなたのポストに申し訳ありませんコメントすることはできません。

最初に 'ifs'は、その中に$(this).data("clicked")が必要ですか?チェックを始める前に毎回$(this).data("clicked", true);でそれをtrueに設定しているので、ちょっと冗長です。

次に、.blockvisibleであるかどうか、またはhiddenである場合は、3番目のifに達しないことを確認します。

私の問題は、あなたがここで試していることを完全に理解していないことです。あなたは、バーガーがクリックされたときにオーバーレイが残っているように見えるが、オーバーレイの背後にあるドットを残しておきたい。バーガーをもう一度クリックすると、オーバーレイが消え、最初に戻ります。私は、ドットがハンバーガーと同じ行動をしていると推測します。これはあなたが望む行動がある場合

、その後、私はこのような多くの異なるアプローチを取る:

$(document).ready(function() { 
 

 
    function modifyOverlay(new_z_index) { 
 

 
     /** If the overlay is hidden, we just show it with the z-index we 
 
      * get in new_z_index (new_z_index == 1 means that dot was clicked, 
 
      * new_z_index == 3 means that burger was clicked) 
 
      */ 
 
     if ($(".block").is(":hidden")) { 
 
     
 
      $(".block").css("display", "block"); 
 
      $(".block").css("z-index", new_z_index); // 1 or 3 
 

 
     } else if ($(".block").css("z-index") != new_z_index) { 
 
     /** Because of the first if, we know that the overlay 
 
      * is visible (which means it has z-index 1 or 3). 
 
      * The only time the current z-index of the overlay will 
 
      * be different from the new_z_index (this is what we are 
 
      * checking on this if) is when you click the burger 
 
      * (new_z_index == 3) while the dot is also upfront 
 
      * (current overlay z-index == 1) 
 
      */ 
 
     
 
      $(".block").css("z-index", new_z_index); // So we just set the z-index 
 
                // to new_z_index (which will 
 
                // be 3) hidding the dot 
 
     } else { 
 
      /** If we get to this else, we know that the overlay is visible 
 
      * (because of the first if) and we know that the current 
 
      * z-index is equal to the new_z_index (because of the second 
 
      * if). 
 
      * So the possible cases are: 
 
      * overlay's z-index is 3, and we pressed the burger (3) 
 
      * or overlay's z-index is 1, and we pressed the dot (1), 
 
      * both cases mean we hide the overlay 
 
      */ 
 
      $(".block").css("display", "none"); 
 
      $(".block").css("z-index", "0"); 
 
     } 
 
    } 
 

 
    $(".dot").click(function(){ 
 
     //Overlay Animation 
 
     modifyOverlay(1); 
 
    }); 
 

 
    $(".burgerMenu").click(function() { 
 
     //Overlay Animation 
 
     modifyOverlay(3); 
 
    }); 
 
});
/* FAB SECTION */ 
 
.dot 
 
{ 
 
    position: absolute; 
 
    bottom: 100px; 
 
    left: 200px; 
 
} 
 

 
#fab { 
 
    position: absolute; 
 
    color: white; 
 
    font-size: 18px; 
 
    text-align: center; 
 
    width: 100px; 
 
    height: 100px; 
 
    background-color: #6eadea; 
 
    border-radius: 50%; 
 
    box-shadow: 6px 10px 18px #686868; 
 
    z-index: 2; 
 
} 
 

 
#plusContain { 
 
    position: relative; 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
#plus { 
 
    position: relative; 
 
    margin: auto; 
 
    top: 50%; 
 
    transform: translateY(-50%); 
 
} 
 

 
/* DIALOGUE/MENU OVERLAY */ 
 

 
.block { 
 
    position: fixed; 
 
    width: 100%; 
 
    height: 100%; 
 
    top: 0; 
 
    background-color: red; 
 
    opacity: 0.8; 
 
    display: none; 
 
    z-index: 0; 
 
} 
 

 

 
/* BURGER MENU SECTION */ 
 

 
.burgerMenu { 
 
    z-index: 4; 
 
    position: absolute; 
 
    margin: auto; 
 
    height: 50px; 
 
    width: 50px; 
 
    cursor: pointer; 
 
    bottom: 10px; 
 
} 
 

 
.bar { 
 
    height: 6px; 
 
    width: 100%; 
 
    background-color: black; 
 
} 
 

 
#top { 
 
    position: relative; 
 
    top: 5px; 
 
} 
 

 
#middle { 
 
    position: relative; 
 
    top: 15px; 
 
} 
 

 
#bottom { 
 
    position: relative; 
 
    top: 25px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 

 
<div id="fab" class="dot dotDown"> 
 
    <div id=plusContain class="fab rotate"> 
 
    <div id="plus">+</div> 
 
    </div> 
 
</div> 
 

 
<!--MENU OVERLAY--> 
 
<div id="overlay" class="block"></div> 
 

 
<!--Burger--> 
 
<div id="burger" class="burgerMenu"> 
 
    <div id="top" class="bar barTop"></div> 
 
    <div id="middle" class="bar barMid"></div> 
 
    <div id="bottom" class="bar barBot"></div> 
 
</div>
がスニペットに

変更:

  • .blockが持っていますZインデックスは0である。
  • .burgerMenuは、az -1のインデックス
  • 追加クラス.upfront
  • JavaScriptコードが修正されました。

EDIT:スニペットに

変更:

  • .burgerMenu 4.
  • #fabのz屈折率を有しているが削除2.
  • のz屈折率を有していますクラス.upfront
  • javascriptコードが修正されました。説明のためにコメントが追加されました。
+0

それは実際にはかなり近いです、と申し訳ありません私は非常にはっきりと説明することができなかった場合。ドットがクリックされた場合、オーバーレイがドットの後ろに表示され、バーガーが表示され、ドットの後にバーガーがクリックされた場合、オーバーレイがドットの上に移動するように調整されます。 – Brian

+0

こんにちは@ブライアン、私は私の答えを編集しました:) –

+0

それは完璧な男を働く!どうもありがとうございます。 – Brian

1

プラスの1の代わりに

$(".block").css("z-index", "+=1"); 

同様に、値をデクリメントする

$(".block").css("z-index", "-=1"); 
+0

ありがとう、私はあなたの答えからも学んだ! – Brian

+0

心配はいりません。ハッピーコーディング:) – Joel

関連する問題