【问题标题】:Bootstrap Button Color引导按钮颜色
【发布时间】:2021-05-15 15:36:07
【问题描述】:

我创建了一个自定义引导导航按钮。

当用户将鼠标悬停在按钮上时,按钮颜色会从绿色变为蓝色,并且会下拉一个子菜单。

然后用户可以单击子菜单项导航到所需的页面。

当用户将鼠标从主按钮移到子菜单上时,主按钮颜色变回绿色。

当用户将鼠标移动到子菜单时,我们希望主按钮保持蓝色。

这是当前使用的代码:

    <!doctype html>
<html>
<head>
    
<meta charset="utf-8">
<title>Login Button Mouseover</title>
    
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
    
<style>
        .btn {
            border-radius: 0rem; 
        }

        .btn-secondary {
            background-color: #55aa39;
        }
    
        .btn-secondary:not(:disabled):not(.disabled).active, 
        .btn-secondary:not(:disabled):not(.disabled):active, 
        .show>.btn-secondary.dropdown-toggle {
            color: #fff;
            background-color: #55aa39; /* GREEN */
            border-color: #4e555b;
            font-weight: bold;
        }
    
        /* set the background color on hover, focus, active */
        .btn-secondary:hover, .btn-secondary:focus, .btn-secondary:active {
            background-color: #223f8e !important; 
        }
    
            .dropdown-item a {
            margin-left: 10px;
            margin-right: 10px;
            width: 90%;
            margin: auto;
        }
    
        .dropdown-menu{
        margin-top: 0;
        display: hide;  /* toggle between hide or block to hide or show for dev */
        min-width: 12rem;
    }
    
    .dropdown:hover .dropdown-menu{
        display: block;
    }
    
            .dropdown-item.active, .dropdown-item:active {
            color: #fff;
            background-color: #007bff;

        }
    
            .dropdown-item:focus, .dropdown-item:hover {
            color: #fff;
            background-color: #55aa39;
            border-style: solid;
            border-color: #000000;
            border-width: 1px;
        }
    

    /* STYLE THE CARET - DOWN POINTING TRIANGLE */
        .dropdown-toggle::after {
            display: inline-block; /* Default */
            width: 0; /* Default */
            height: 0; /* Default */
            margin-left: .3em; /* Default */
            vertical-align: middle; /* Default */
            content: ""; /* Default */
            border-top: .5em solid; /* caret size */
            border-right: .5em solid transparent; /* caret size */
            border-left: .5em solid transparent; /* caret size */
        }
    
    .mycustom {
            margin-left: 5px; 
            margin-right: 7px; 
            width: 95%; 
            text-align: center;
            margin: auto;
            min-height: 35px;
        }
    
</style>
<script>
$(document).ready(function(){
    $(".dropdown").hover(function(){
        var dropdownMenu = $(this).children(".dropdown-menu");
        if(dropdownMenu.is(":visible")){
            dropdownMenu.parent().toggleClass("open");
        }
    });
});     
</script>
    
</head>

<body>
    <p>&nbsp;</p>
    
    
    <div class="dropdown show" style="margin: auto; text-align: center; width: 100px;">
      <a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        Login
      </a>

          <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
            <a class="dropdown-item mycustom" href="#">User Access</a>
            <a class="dropdown-item mycustom" href="#">Business Access</a>
            <a class="dropdown-item mycustom" href="#">Employee</a>
          </div>
    </div>
    
    
</body>
</html>

感谢您的帮助!

【问题讨论】:

  • 嗨@Wordpress-Pros.com,如果确实解决了您的问题,请标记答案已接受。

标签: css twitter-bootstrap


【解决方案1】:

谢谢桑迪普!你的答案有效。你太棒了!

【讨论】:

    【解决方案2】:

    这是我更新的代码:

    <!doctype html>
    <html>
    <head>
        
    <meta charset="utf-8">
    <title>Login Button Mouseover</title>
    
        
        
    <!-- BEGIN CUSTOM BUTTON CODE -->
        
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
        
        <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
    
        <style>
    
              .btn-secondary:not(:disabled):not(.disabled).active, 
              .btn-secondary:not(:disabled):not(.disabled):active, 
              .show>.btn-secondary.dropdown-toggle {
                  background-color: #55aa39; /* GREEN */
                  font-weight: bold;
                  border-radius: 0rem; 
              }
    
    /* set the main button background color on hover, focus, active */
              .btn-secondary:hover, .btn-secondary:focus, .btn-secondary:active {
                  background-color: #223f8e !important; 
              }
    
              .dropdown-menu {
                  margin-top: 0;
                  display: hide;  /* toggle between hide or block to hide or show for dev */
                  min-width: 12rem;
              }
    
              .dropdown:hover .dropdown-menu{
                  display: block;
              }
    
              .dropdown-item.active, .dropdown-item:active {
                  background-color: #007bff;
              }
    
              .dropdown-item:focus, .dropdown-item:hover {
                  color: #fff;
                  background-color: #55aa39;
                  border-style: solid;
                  border-color: #000;
                  border-width: 1px;
              }
    
    /* keep the button blue when hovered and when mousing over drop menu */
              .dropdown.open > a.dropdown-toggle {
                  background-color: #223f8e;
              }
    
    /* STYLE THE CARET - DOWN POINTING TRIANGLE */
            .dropdown-toggle::after {
                vertical-align: middle; /* Default */
                border-top: .5em solid; /* caret size */
                border-right: .5em solid transparent; /* caret size */
                border-left: .5em solid transparent; /* caret size */
            }
    
            .mycustom {
                  margin-left: 5px; 
                  margin-right: 7px; 
                  width: 95%;
                  text-align: center;
                  margin: auto;
                  min-height: 35px;
            }
    
        </style>
    
        <script>
            $(document).ready(function(){
                $(".dropdown").hover(function(){
                    var dropdownMenu = $(this).children(".dropdown-menu");
                    if(dropdownMenu.is(":visible")){
                        dropdownMenu.parent().toggleClass("open");
                    }
                });
            });     
        </script>
        
    <!-- END CUSTOM BUTTON CODE -->
        
        
    </head>
    
    <body>
        <p>&nbsp;</p>
        
    
        <div class="dropdown show" style="margin: auto; text-align: center; width: 100px;">
          <a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            Login
          </a>
    
              <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
                <a class="dropdown-item mycustom" href="#">User Access</a>
                <a class="dropdown-item mycustom" href="#">Business Access</a>
                <a class="dropdown-item mycustom" href="#">Employee</a>
              </div>
        </div>
    
    </body>
    </html>
    

    【讨论】:

      【解决方案3】:

      我们希望当鼠标悬停在主登录按钮上时主按钮背景变为蓝色。

      当用户将鼠标悬停在任何下拉菜单项上时,我们希望主登录按钮背景保持蓝色。

      如果用户将鼠标从主登录按钮或下拉菜单上移开,我们希望主背景颜色恢复为绿色。

      如果用户随后再次将鼠标悬停在主登录按钮上,当用户将鼠标悬停在任何下拉菜单项上时,我们希望主按钮背景变为蓝色,并且主按钮背景保持蓝色。

      问题是当用户将鼠标移开,移到页面的不同部分,然后返回到按钮,并将鼠标悬停在该按钮上时,主按钮背景颜色保持绿色不变。

      感谢您对这个 Sandeep 的帮助!

      【讨论】:

      • 如果有帮助,请点赞并接受答案。
      【解决方案4】:

      您只需要在样式元素中添加更多代码

      .dropdown.open > a.dropdown-toggle 
      {
                  background-color: #223f8e;
      }
      

      我正在发布您的查询的完整答案。我做了一些绝对没有必要的更改,但以这种方式编写代码很好。

      我所做的重要更改

      1. 删除了类open,而只使用show
      2. 如果下拉菜单不可见,则应删除脚本作为显示类的更改。
      3. 阻止点击事件,因为它需要 popper.js
      4. css 的一些变化。

      这是完整的代码

      <!doctype html>
      <html>
      <head>
      
          <meta charset="utf-8">
          <title>Login Button Mouseover</title>
          <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
          <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
          <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
          <style>
      
              :root {
                  --green: #55aa39;
                  --blue: #223f8e;
              }
      
              #dropdownMenuLink {
                  background-color: var(--green); /* GREEN */
                  font-weight: bold;
                  border-radius: 0rem;
              }
      
                  #dropdownMenuLink:hover, .dropdown.show > #dropdownMenuLink {
                      background-color: var(--blue) !important;
                  }
      
                  #dropdownMenuLink::after {
                      vertical-align: middle;
                      border-top: .5em solid;
                      border-right: .5em solid transparent;
                      border-left: .5em solid transparent;
                  }
      
              .mycustom {
                  margin-left: 5px;
                  margin-right: 7px;
                  width: 95%;
                  text-align: center;
                  margin: auto;
                  min-height: 35px;
              }
              .dropdown-menu {
                  margin-top: 0;
                  min-width: 12rem;
              }
              .dropdown:hover .dropdown-menu {
                  display: block;
              }
      
              .dropdown-item.active {
                  background-color: #007bff;
              }
      
              .dropdown-item:focus, .dropdown-item:hover {
                  color: #fff;
                  background-color: #55aa39;
                  border-style: solid;
                  border-color: #000;
                  border-width: 1px;
              }
          </style>
      
          <script>
              $(document).ready(function () {
                  $(".dropdown").hover(function () {
                      var dropdownMenu = $(this).children(".dropdown-menu");
                      if (dropdownMenu.is(":visible")) {
                          dropdownMenu.parent().addClass("show");
                      }
                      else {
                          dropdownMenu.parent().removeClass("show");
                      }
                  });
      
                  $("#dropdownMenuLink").click(function (e) { e.stopPropagation(); })
              });
          </script>
      </head>
      
      <body>
          <p>&nbsp;</p>
          <div class="dropdown" style="margin: auto; text-align: center; width: 100px;">
              <a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  Login
              </a>
              <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
                  <a class="dropdown-item mycustom" href="#">User Access</a>
                  <a class="dropdown-item mycustom" href="#">Business Access</a>
                  <a class="dropdown-item mycustom" href="#">Employee</a>
              </div>
          </div>
      </body>
      </html>
      

      【讨论】:

      • 嗨桑迪普。这几乎可以工作!现在,如果我将鼠标悬停在主按钮上,背景会变为蓝色,如果我将鼠标悬停在下拉项目上,则背景会保持蓝色。如果我随后将鼠标移开,然后再次将鼠标悬停在主按钮上,则主按钮背景颜色将恢复为绿色。有什么想法吗?
      • 嗨@Wordpress-Pros.com 你能分享一下你到底想要什么吗?
      • 谢谢桑迪普!正是我需要的。非常感谢您的帮助。
      • 嗨@Wordpress-Pros.com,请将我的答案标记为已接受并投票。
      • 嗨桑迪普。我很乐意将您的回答标记为接受并投赞成票,但我找不到其中任何一个方法!我没有看到任何接受或支持的选项。
      猜你喜欢
      • 1970-01-01
      • 2018-03-23
      • 1970-01-01
      • 2013-06-27
      • 1970-01-01
      • 2017-12-07
      • 2015-10-18
      • 2019-10-19
      • 1970-01-01
      相关资源
      最近更新 更多