如何修改select標簽的默認下拉箭頭樣式?


  對於一般的項目而言,select標簽在瀏覽器中表現出來的默認樣式也不算丑,但是一次項目中,項目經理卻要求對select標簽本身進行樣式修改,美化其下拉小箭頭的樣式。我思考和嘗試了許多方法,最終得到一種能夠兼容chrome、360、火狐、搜狗、IE10+等瀏覽器的最佳方案。廢話不多說,實現原理如下:

  html結構:

1 <div class="box">
2         <select id="choice">
3             <option value="000">請選擇</option>
4             <option value="371">河南</option>
5             <option value="372">河北</option>
6         </select>
7         <img src="arrow-3.png" alt="" id="arrow2">
8 </div>
9 <label for="userName">姓名</label><input type="text" placeholder="請輸入姓名" id="userName">

  css樣式:

 1   .box{
 2         width: 200px;
 3         height: 30px;
 4         border: 1px solid #0f0;
 5         position: relative;
 6         margin-bottom: 100px;
 7     }
 8     #choice{
 9         position: absolute;
10         top: 0;
11         left: 0;
12         z-index: 4;
13         width: 200px;
14         height: 30px;
15         border: 0;
16         /*outline: none;*/
17         
18         appearance: none;  
19         -moz-appearance: none;  
20         -webkit-appearance: none; 
21         background-color: transparent;
22     }
23     select::-ms-expand { 
24         display: none; 
25     }
26     img{
27         width: 30px;
28         height: 20px;
29         position: absolute;
30         top: 5px;
31         right: 0;
32         z-index: 2;
33         transition:all 0.2s;
34     }

  js代碼:

 1 <script src="jquery.js"></script>
 2     <script>
 3 
 4         $("#choice").focus(function(){
 5             $("#arrow2").css({
 6                 transform:"rotate(180deg)"
 7             });
 8         });
 9         $("#choice").blur(function(){
10             $("#arrow2").css({
11                 transform:"rotate(0deg)"
12             });   
13         });
14 
15         $("#choice").on("change",function(){
16             $("#choice").blur();
17             $("#arrow2").css({
18                 transform:"rotate(0deg)"
19             });
20         });
21    
22     </script>

  好了,本方法還存在一些不完美,歡迎各位小伙伴跟帖補充,我會及時完善博客,助人助己。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM