欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

select下拉框option的样式修改 有大用

select下拉框option的样式修改

select原样式:

进行样式修改后的样式:

 

附上修改代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//select外面必须包裹一个div,用来覆盖select原有的样式<div class="option">
        <select name="">
          <option value=" ">筛选实验类型</option>
          <option value="实验1">实验1</option>
          <option value="实验2">实验2</option>
        </select>
      </div>
  
css:
 
 .option{
  /*用div的样式代替select的样式*/
    margin: 100px;
   width: 140px;
    height: 40px;
    /*border-radius: 5px;*/
  /*盒子阴影修饰作用,自己随意*/
 /* box-shadow: 0 0 5px #ccc;*/
    border: 1px solid #cccccc;
    position: relative;
  }
  .option select{
  /*清除select的边框样式*/
  border: none;
  /*清除select聚焦时候的边框颜色*/
  outline: none;
  /*将select的宽高等于div的宽高*/
  width: 100%;
    height: 40px;
    line-height: 40px;
  /*隐藏select的下拉图标*/
  appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
  /*通过padding-left的值让文字居中*/
  padding-left: 20px;
  }
  /*使用伪类给select添加自己想用的图标*/
  .option:after{
      content: "";
      width: 14px;
      height: 8px;
      background: url(../assets/arrow-down.png) no-repeat center;
    /*通过定位将图标放在合适的位置*/
    position: absolute;
      right: 20px;
      top: 41%;
    /*给自定义的图标实现点击下来功能*/
    pointer-events: none;
    }

  

 但是有个问题;option的样式没办法修改;我各种百度搜索,没有搜索出如何修改其样式;

 因为option是html固有元素;因而无论怎么修改在浏览器上都是不起作用的。

想修改option样式,只能通过div ul li模拟select功能;

我项目是用vue做的,所以我基于vue对select用div li进行改造。

用div ul li做的select下来框:

实现的代码如下:

<div class="divInput">
        <div class="input" @click="openValue">
          <input v-model="value" type="text" placeholder="筛选实验类型">
          <img src="../assets/arrow.png" alt="">
        </div>
        <div class="list" v-show="show">
          <ul>
            <li @click="getvalue(index,item)" v-for="(item,index) in tableData" :key="item.index">{{ item.name }}</li>
          </ul>
        </div>
      </div>

js:

export default {
    name: 'javascript',
    data(){
      return{
        tableData:[
          {
            'name':111
          },
          {
            'name':222
          },
          {
            'name':333
          }, {
            'name':444
          }
        ],
        show:false,
        value:''
      }
    },
    methods: {
      openValue(){
        this.show=!this.show;
      },
      getvalue(index,item){
        this.value=item.name;
        this.show=false;
      },
    },

css:

.divInput{
  margin: 200px;
}
  ul li{
    list-style: none;
  }
  .input{
    width: 140px;
    height: 40px;
    line-height: 40px;
    padding-left: 10px;
    border: 1px solid #cccccc;
    position: relative;
  }
  .input input{
    border: none;
    outline: none;
    width: 90%;
  }
  .input img{
    position: absolute;
    right: 34px;
    top: 48%;
  }
  .list{
    width: 150px;
    border: 1px solid #cccccc;
    overflow: hidden;
  }
  .list ul li{
    width: 100%;
    height: 30px;
    cursor: pointer;
    line-height: 30px;
    padding-left: 10px;
  }
  .list ul li:hover{
    background-color: #cccccc;
  }

 

这样就可以完成对select样式的修改了。

嘿嘿,开心!

1
« 上一篇: vue项目打包之后样式错乱问题,如何处理
» 下一篇: ‘Maximum call stack size exceeded’错误的解决方法
posted @ 2019-07-11 18:09  web小白需努力  阅读(33138)  评论(5)  编辑  收藏


  #1楼 2019-12-18 14:49 玉楠
请问,在input失去焦点也就是在输入框写入内容后,下一个 鼠标点击 落在了你整个布局的外面。 那么这个信息提示列表是不是应该关闭? 如果在 input框 加个 blur事件 又会覆盖 li 的 click 事件 ,请问 你这里是怎么解决的?
  #2楼 [楼主2019-12-18 15:03 web小白需努力
@ 玉楠
getvalue(index,item){
this.value=item.name;
this.show=false;
},
当选择完成之后,this.show=false;就会立即关闭信息列表
  #3楼 2020-04-20 20:37 弥猫深巷-

你好,如何进到页面之后是下拉框隐藏状态,,展开是浮在页面上,而不是把我的页面顶下去

  #4楼 2020-04-20 20:41 弥猫深巷-

<div class="divInput">
<div class="input" @click="selectChange()">
<van-cell title="就诊人" value="内容" v-model="value" is-link arrow-direction="down"/>
</div>
<div class="list" v-show="show">
<ul>
<li @click="getvalue(index,item)" v-for="(item,i) in this.patientInfo" :key="i" v-if="item.relationId == 1" v-bind:value="item.patientId" >{{ item.names}}</li>
<li @click="getvalue(index,item)" v-for="(item,i) in this.patientInfo" :key="i" v-if="item.relationId != 1" v-bind:value="item.patientId" >{{ item.names}}</li>
</ul>
</div>
</div>

  #5楼 [楼主2020-04-21 17:32 web小白需努力

@弥猫深巷-
下拉框的css用定位处理,不占位就会浮在页面上面


来自   https://www.cnblogs.com/zr123/p/11171757.html




复制代码
select,option{
    /*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/
    border: none;
    outline: none;
    /*很关键:将默认的select选择框样式清除*/
    appearance:none;
    -moz-appearance:none;
    -webkit-appearance:none;
    /*将背景改为红色*/
    background:#fff;
    /*加padding防止文字覆盖*/
    padding-right: .14rem;
}
/*清除ie的默认选择框样式清除,隐藏下拉箭头*/
select::-ms-expand {display: none;}


来自  https://www.cnblogs.com/zr123/p/11171757.html

普通分类: