【问题标题】:How to make semi-transparent select2?如何制作半透明的select2?
【发布时间】:2015-01-20 10:44:15
【问题描述】:

我使用以下 CSS 使按钮看起来是半透明的 -

.navbar-toggle {
    position:relative;
    float:left;
    display:block;
    padding: 9px 10px;
    background: rgba(0, 0, 0, 0.2);
    background-image: none;
    border: 0 none;
    top:0px;
    -webkit-border-radius: 2px;
       -moz-border-radius: 2px;
            border-radius: 2px;
    outline: 0;
    opacity: 1;
    -webkit-transition: all .45s;
       -moz-transition: all .45s;
            transition: all .45s;
            float: left;
}

但是当我将它应用到select2 组件时,它并没有使它看起来透明。看起来 select2 应用了自己的主题并覆盖了我所做的。 Select2 也有supports themes,但是有详细的文档is missing。

我应该如何更改 select2 样式以使其看起来与按钮相同(请参阅jsfiddle demo)

【问题讨论】:

  • select2 添加自己的 html,因此您可能会设置错误的 html 样式。使用浏览器工具查看生成了哪些元素
  • jsfiddle.net/crxepevy/2 这是您要找的东西吗?您可以添加容器和所有这些,但这很难。只需编辑 select2 类。我只是使用它的结构并在此之上应用自己的规则。我们都在等待select2的主题#
  • @Persijn,是的,这几乎就是我需要的。我还想要下拉列表灰色/半透明。顺便说一句,当前样式的 select2 字体是白色的 - 即你看不到你输入的内容。

标签: css twitter-bootstrap twitter-bootstrap-3 jquery-select2


【解决方案1】:

我试过这个:http://jsfiddle.net/crxepevy/1/ 它工作正常吗?

JS

var data = [{
    id: 0,
    text: 'enhancement'
}, {
    id: 1,
    text: 'bug'
}, {
    id: 2,
    text: 'duplicate'
}, {
    id: 3,
    text: 'invalid'
}, {
    id: 4,
    text: 'wontfix'
}];


$(".js-example-data-array").select2({
    data: data,
    theme: "themes-dark"
});

CSS

<body>
<nav class="mainmenu">
    <div class="container">
        <div class="navbar-right">
            <select class="js-example-data-array themes-dark"></select>
            <button type="button" class="navbar-toggle"><font color="white"><i class="fa fa-search fa-lg fa-fw"></i></font>
            </button>
            <button type="button" class="navbar-toggle"><font color="white"><i class="fa fa-play fa-lg fa-fw"></i></font>
            </button>
            <button type="button" class="navbar-toggle"><font color="white"><i class="fa fa-vk fa-lg fa-fw"></i></font>
            </button>
        </div>
    </div>
</nav>

CSS

html {
background:#505D6E url(http://habrastorage.org/files/a01/ca3/b36/a01ca3b360454f539b4275a3ad274c87.jpg) no-repeat center center fixed;
min-height:100%;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover; }

body {
    background:transparent;
    min-height:100%;
    -webkit-font-smoothing: antialiased;
    -webkit-overflow-scrolling: touch;
    line-height: 1.5em;
    font-size:14px;
    font-family: Helvetica, Arial, sans-serif;
    color:#fff;
    color:rgba(255,255,255,.8);
    font-weight: normal;
}

.mainmenu { z-index: 999999; width:100%;  padding: 30px 0 0 0;}
@media (min-width: 768px) {
    .mainmenu { position: fixed; }
}

.navbar-toggle, .themes-dark, .select2-container--themes-dark {
    position:relative;
    display:block;
    padding: 9px 10px;
    background: rgba(0, 0, 0, 0.2);
    background-image: none;
    border: 0 none;
    top:0px;
    -webkit-border-radius: 2px;
       -moz-border-radius: 2px;
            border-radius: 2px;
    outline: 0;
    opacity: 1;
    -webkit-transition: all .45s;
       -moz-transition: all .45s;
            transition: all .45s;
            float: left;
}
select {
    height: 39px !important;
    width: 100px;
    margin: 8px 20px !important;
}

span.select2.select2-container.select2-container--themes-dark.select2-container--below, .select2-container--themes-dark {
margin-right: 20px !important;
margin-top: 8px;
height: 39px;
}

【讨论】:

  • 谢谢!倒塌的样子很好看。但奇怪的是 select2 不起作用 - 只显示一个元素。刚刚注意到我最初的小提琴也有同样的问题。
  • 添加这个css:.select2-container--open .select2-dropdown--below { background: transparent; border: none; } span.select2-container.select2-container--themes-dark.select2-container--open { height: 220px; }link
【解决方案2】:

Fiddle

将 select2 放入容器中。

<div class="nav-sel">SELECT2</div>

有了这个,你可以让它看起来像一个带有 css 的 bootstrap 2 nav 元素:

css

.nav-sel {
   margin-top: 9px;
   margin-right: 15px;
   float: left;
}

现在选择 2: 添加了大量透明背景和半透明背景。

css

.select2-container {
    background: transparent;
}

.select2-container .select2-selection {
    border: none;
    height: 40px;
    background: rgba(0, 0, 0, 0.2);
}

.select2-container .select2-selection span:first-child {
    color: white;
}

.select2-results ul li {
    color: white;
}

.select2-dropdown {
    background: rgba(0, 0, 0, 0.2);
}
.select2-search input:first-child {
    background: transparent;
}

【讨论】:

  • 非常感谢!太好了。我已经更改了选择的颜色等,但无法弄清楚如何更改文本的位置 - enhancement 和向下箭头(显示为默认元素)应该垂直居中对齐。
  • 我似乎无法正确使用小提琴,但可以通过设置正确设置箭头:top: 20px 和 line-height: 40px 应该是解决此问题的方法,但小提琴看起来与我时不同成功了。
  • 应该修改哪种样式来添加这些设置?
  • jsfiddle.net/crxepevy/6 这是新的小提琴。它修改到我认为它是一个黑客的程度。我不知道为什么箭头显示在左侧。
猜你喜欢
  • 2011-06-15
  • 1970-01-01
  • 2016-02-02
  • 1970-01-01
  • 1970-01-01
  • 2013-09-06
  • 1970-01-01
  • 2011-08-31
相关资源
最近更新 更多