css - 第二次单击按钮后失去焦点

标签 css sass

我有以下样式:

.btn-dropdown {
  height: 30px;
  background: transparent;
  border: solid 1.4px #ffffff;
  font-size: 10px;
  color: white;
  min-width: 0;
  max-width: fit-content;
  padding-left: 15px;
  padding-bottom: 6px;

  &:hover {
    background: white;
    color: $blue;
  }

  &:active,
  &:focus {
    background: white;
    color: $blue;
    box-shadow: none;
    outline:none;  
    &::after {
      background: transparent;
    }
  }
}

html

   <button type="button" class="btn btn-dropdown dropdown-toggle">
                   abcd          
   <i class="far fa-chevron-down"></i>
    </button>

我想实现以下行为:

1) 第二次点击按钮后,它应该失去焦点。

纯 SCSS/CSS 有可能吗?我用谷歌搜索了一下,发现了一些不令人满意的解决方案,例如指针事件:无 ..

提前致谢

最佳答案

我有一个 CSS 和 JavaScript(没有 jQuery)的解决方案:

function isHidden(el) {
  var style = window.getComputedStyle(el);
  return (style.display === 'none')
}

const list = document.getElementById('drop');
const btn = document.getElementById('btn');

btn.addEventListener("click", function() {
  if (isHidden(list) == true) {
    btn.classList.add("active");
    list.style.display = 'flex';
  } else {
    btn.classList.remove("active");
    list.style.display = 'none'
  }
});
body {
  background: #121212;
}

.list-group {
  display: none;
  flex-direction: column;
  padding-left: 0;
  margin-bottom: 0;
}

.list-group.active {
  display: flex;
}

.list-group-item {
  position: relative;
  display: block;
  padding: 0.75rem 1.25rem;
  margin-bottom: -1px;
  background-color: #fff;
  border: 1px solid rgba(0, 0, 0, 0.125);
}

.btn-dropdown {
  height: 30px;
  background: transparent;
  border: solid 1.4px #ffffff;
  font-size: 10px;
  color: white;
  min-width: 0;
  max-width: fit-content;
  padding-left: 15px;
  padding-bottom: 6px;
}

.btn-dropdown:hover {
  background: white;
  color: #069;
}

.btn-dropdown.active {
  background: white;
  color: #069;
  box-shadow: none;
  outline: none;
}

.btn-dropdown.active::after {
  background: transparent;
}
<button type="button" id="btn" class="btn btn-dropdown dropdown-toggle">Button          
       <i class="fa fa-chevron-down"></i>
</button>

<ul class="list-group" id="drop">
  <li class="list-group-item">Cras justo odio</li>
  <li class="list-group-item">Dapibus ac facilisis in</li>
  <li class="list-group-item">Morbi leo risus</li>
  <li class="list-group-item">Porta ac consectetur ac</li>
  <li class="list-group-item">Vestibulum at eros</li>
</ul>

由于悬停状态,当您悬停时按钮仍然具有白色背景:

.btn-dropdown:hover {
  background: white;
  color: #069;
}

如果您“取消悬停”,它就会变得透明。

SASS版本可见 this jsFiddle。

关于css - 第二次单击按钮后失去焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59029381/

相关文章:

css - 在 body 元素上使用 ID 属性选择器(不是 #id)来命名空间 CSS 文件是否容易出错?

sass - 使用@while 循环创建动态类名

javascript - 在 GWT 中如何知道应用于给定元素的所有样式(通过 id 或类名)

css - 第一个和最后一个子边界半径不起作用

html - css)我想将页脚放在该部分下,但它在旁边,在文章旁边

jquery - 如何替换 Canvas 图像?

sass - 用Grunt将变量传递给SASS

html - 如何在 IE10 中用 css 覆盖不可选择的 ="on"?

css - 使用 PIE 行为圆 Angular 时的 IE 7 滚动条问题

html - 我怎样才能让所有容器的高度最高?