2016-06-15 1 views
0

私はクライアントサイトで作業しており、サイトのメインバナーにドロップダウンボタンを表示したいと考えています。ドロップダウンメニューの効果を持つ単一のボタン - ヘルプRefine

私はそれをコード化して動作させることができましたが、少し気分が悪いようです。ボタンは機能しますが、クリックするたびに必ずしもスムーズには動作しません。誰かがこれを見て、彼らがこのコードと矛盾している何か、あるいは私がそれを細かく調整してよりうまく動作させるために何ができるのかを教えてもらえますか?

私は、次のCSSを使用しています:

/* Dropdown Button */ 
.dropbtn { 
    background-color: rgba(102, 146, 255, 0.87); 
    color: white; 
    padding: 9px 15px 14px 15px; 
    font-size: 16px; 
    border: none; 
    cursor: pointer; 
    border-radius: 6px; 
} 

/* Dropdown button on hover & focus */ 
.dropbtn:hover, .dropbtn:focus { 
    background-color: #6692FF; 
} 

/* The container <div> - needed to position the dropdown content */ 
.dropdown { 
    position: relative; 
    display: inline-block; 
} 

/* Dropdown Content (Hidden by Default) */ 
.dropdown-content { 
    display: none; 
    position: absolute; 
    background-color: #f9f9f9; 
    min-width: 160px; 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
} 

/* Links inside the dropdown */ 
.dropdown-content a { 
    color: black; 
    padding: 9px 32px; 
    text-decoration: none; 
    display: block !important; 
    border-bottom: 1px solid #4A5056; 
} 

/* Change color of dropdown links on hover */ 
.dropdown-content a:hover {background-color: #f9f9f9} 

/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */ 
.show {display:block;} 

次のHTML:

<div class="dropdown"> 
    <button onclick="myFunction()" class="dropbtn"><span style="  vertical-align: bottom;font-size: 24px; ">LISTEN</span> <i class="fa fa-volume-up fa-4x"></i></button> 
    <div id="myDropdown" class="dropdown-content"> 
<a href="http://lonebeacondevelopment.com/financialexchange/listen-live/" class="popup" data-height="300" data-width="300" data-scrollbars="0" alt="LISTEN LIVE"> 
LISTEN LIVE</a> 
    <a href="http://lonebeacondevelopment.com/financialexchange/on-demand/">ON DEMAND</a> 
    </div> 
</div> 

そして次JS:

/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */ 
function myFunction() { 
    document.getElementById("myDropdown").classList.toggle("show"); 
} 

// Close the dropdown menu if the user clicks outside of it 
window.onclick = function(event) { 
    if (!event.target.matches('.dropbtn')) { 

    var dropdowns = document.getElementsByClassName("dropdown-content"); 
    var i; 
    for (i = 0; i < dropdowns.length; i++) { 
     var openDropdown = dropdowns[i]; 
     if (openDropdown.classList.contains('show')) { 
     openDropdown.classList.remove('show'); 
     } 
    } 
    } 
} 

リンクはhttp://lonebeacondevelopment.com/financialexchange/は青が聞くですバナーの右上隅にあるボタンをクリックします。

何か助けていただければ幸いです。

ありがとうございます!

+0

私はそれについて何かを気づきました。最初の見た目では、それは時々だけ動作するように見えますが、実際にはうまくいきます。 「ラッピング」領域(外側の領域)のみが動作しています。テキストまたはボリュームの画像を押すと、その画像は表示されません。これは単なる発言です。 –

+0

あなたは何を言っているのか分かります。私はそれを見て、他の誰かがチャイムに入ることを願っています。 – kgmack

+0

jQueryを受け入れますか?それははるかに簡単です。私はjQueryのコードを書いています。 –

答えて

0

あなたが行うことができます[OK]をあなたが設定されたこの

CSS

/* Dropdown Button */ 
.dropbtn { 
    background-color: rgba(102, 146, 255, 0.87); 
    color: white; 
    padding: 9px 15px 14px 15px; 
    font-size: 16px; 
    border: none; 
    cursor: pointer; 
    border-radius: 6px; 

vertical-align: bottom;font-size: 24px; /*HERE The CODE of SPAN*/ 

} 

/* Dropdown button on hover & focus */ 
.dropbtn:hover, .dropbtn:focus { 
    background-color: #6692FF; 
} 

/* The container <div> - needed to position the dropdown content */ 
.dropdown { 
    position: relative; 
    display: inline-block; 
} 

/* Dropdown Content (Hidden by Default) */ 
.dropdown-content { 
    display: none; 
    position: absolute; 
    background-color: #f9f9f9; 
    min-width: 160px; 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
} 

/* Links inside the dropdown */ 
.dropdown-content a { 
    color: black; 
    padding: 9px 32px; 
    text-decoration: none; 
    display: block !important; 
    border-bottom: 1px solid #4A5056; 
} 

/* Change color of dropdown links on hover */ 
.dropdown-content a:hover {background-color: #f9f9f9} 

/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */ 
.show {display:block;} 

HTML

<div class="dropdown"> 
    <button onclick="myFunction()" class="dropbtn">LISTEN <i class="fa fa-volume-up fa-4x"></i></button> 
    <div id="myDropdown" class="dropdown-content"> 
<a href="http://lonebeacondevelopment.com/financialexchange/listen-live/" class="popup" data-height="300" data-width="300" data-scrollbars="0" alt="LISTEN LIVE"> 
LISTEN LIVE</a> 
    <a href="http://lonebeacondevelopment.com/financialexchange/on-demand/">ON DEMAND</a> 
    </div> 
</div> 
+0

それはすごくうまくいった!ありがとうございました!また、選択したボタンの周囲に表示されるアウトハイライトハイライトを削除するにはどうすればよいですか? – kgmack

+0

@kgmack "outline:none;"を.dropbtn:hover、.dropbtn:focus { –

0

       のようなボタンクラスのボタンとペーストスパンスタイリング内のスパンを削除しています関数 'myFunction' ボタンの場合はのみですがボタン内にスパンまたはi要素がトリガーされるわけではありません。 (jQueryを使って)これを試してそれを解決するために


HTML

<div class="dropdown"> 
     <div class="dropbtn"> <!--CHANGED THIS--> 
      <span style="vertical-align: bottom;font-size: 24px; ">LISTEN</span> 
      <i class="fa fa-volume-up fa-4x"></i> 
     </div> 
     <div id="myDropdown" class="dropdown-content"> 
      <a href="http://lonebeacondevelopment.com/financialexchange/listen-live/" class="popup" data-height="300" data-width="300" data-scrollbars="0" alt="LISTEN LIVE">LISTEN LIVE</a> 
      <a href="http://lonebeacondevelopment.com/financialexchange/on-demand/">ON DEMAND</a> 
     </div> 
</div> 


JS/JQ

$(document).ready(function() { 
    $(".dropbtn").click(function(){ 
      document.getElementById("myDropdown").classList.toggle("show"); 
    }); 


    $("body").click(function(e){ 
     if(e.target.className !== "form_wrapper"){ 
      var dropdowns = document.getElementsByClassName("dropdown-content"); 
      var i; 
      for (i = 0; i < dropdowns.length; i++) { 
       var openDropdown = dropdowns[i]; 
       if (openDropdown.classList.contains('show')) { 
        openDropdown.classList.remove('show'); 
       } 
      } 
     } 
    }); 
}); 
関連する問題