单按钮与下拉菜单效果 - 帮助优化

时间:2016-06-15 17:07:12

标签: javascript java html css wordpress

我正在客户网站上工作,他们希望在网站的主横幅上有一个下拉按钮。

我能够对它进行编码并使其正常工作,但它看起来有点不稳定。按钮有效,但不是每次点击它都不顺利。有人可以看看这个并让我知道他们是否有其他与此代码冲突的东西,或者我可以做些什么来微调它并让它更好地工作?

我使用以下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/,是横幅右上角的蓝色“收听”按钮。

非常感谢任何帮助。

非常感谢!

2 个答案:

答案 0 :(得分:0)

Ok你可以做的是删除按钮内的span并在按钮类中粘贴span样式,如下所示

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>

答案 1 :(得分:0)

您为按钮设置了'myFunction' 功能,但这并不意味着它将由 span 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');
                }
           }
        }
    });
});