使用bootstrap框架避免不了寫CSS,當CSS文件較大時,會發現維護起來很麻煩,一些默認值,如行高、背景色、標注顏色、字號等信息往往反復出現,還有一些大體上一致,只有小部分不同的樣式定義,這就需要css預處理器的幫助,常見的有兩種Sass和Less,前者使用ruby寫的,需要安裝ruby,后者似乎是用js開發,用npm直接安裝就可以了。考慮到安裝過程,我比較喜歡less。
less最好先安裝nodejs,使用其帶的npm來安裝,nodejs從nodejs.org下載,這是一個服務器端的JS框架,可以用來提供Web服務和做后端開發。less入門可以參考http://less.bootcss.com/,這里有較為詳細的描述。這里就不描述了。
安裝之后,我在netbeans里使用less,它會調用系統的lessc來生成的css程序,當保存less文件時,會自動生成css文件。這需要配置一下,在項目上右鍵-》屬性-》css預處理程序,將保存時編譯LESS文件打勾,就可以了。
在開發過程中,發現一個較長的流程操作,需要分步來執行,我感覺微信公眾平台的處理方式比較好,其界面如下:
要實現這個效果,本來打算用圖片來實現的,后來發現用CSS+JS也可以實現,我實現的效果如下
這里實現的也非常簡單,用了框架的屬性來生成了箭頭,實際上是兩個div,生成了兩個類似箭頭的東西,下面可以看到效果
其css樣式如下
1 .arrow-next { 2 height: 0px; 3 width: 0px; 4 border: solid 1.5em; 5 border-color: #fff; 6 border-left-color: #54b003; 7 } 8 .arrow-current { 9 height: 0px; 10 width: 0px; 11 border: 1.5em solid; 12 border-color: #54b003; 13 border-left-color: #fff; 14 }
arrow-next產生一個箭頭,arrow-current產生一個凹陷,原理也很簡單,一個長寬為0的div,border很寬,通過其顏色配置,產生這種效果,剩下的就是通過js將這兩個div的位置移動到相應的步驟前后,注意處理一下第一步,就可以了,所有代碼如下
1 <html> 2 <head> 3 <meta charset="utf-8"> 4 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 5 <meta name="viewport" content="width=device-width, initial-scale=1"> 6 <title>流程導航范例</title> 7 <!-- Bootstrap --> 8 <link href="css/bootstrap.css" rel="stylesheet"> 9 <style type="text/css"> 10 .row.row-nav { 11 border: solid 1px #54b003; 12 } 13 .col-nav { 14 height: 3em; 15 padding: 0px; 16 border: 0px; 17 text-align: center; 18 font-size: 14px; 19 line-height: 3em; 20 } 21 .col-nav.current { 22 background-color: #54b003; 23 } 24 .arrow-next { 25 height: 0px; 26 width: 0px; 27 border: solid 1.5em; 28 border-color: #fff; 29 border-left-color: #54b003; 30 } 31 .arrow-current { 32 height: 0px; 33 width: 0px; 34 border: 1.5em solid; 35 border-color: #54b003; 36 border-left-color: #fff; 37 } 38 </style> 39 </head> 40 <body> 41 <br> 42 <br> 43 <br> 44 <br> 45 <div class="container"> 46 <div class="row row-nav"> 47 <div class="col-md-3 col-nav current" id="nav-1"> 48 第一步 49 </div> 50 <div class="col-md-3 col-nav" id="nav-2"> 51 第二步 52 </div> 53 <div class="col-md-3 col-nav" id="nav-3"> 54 第三步 55 </div> 56 <div class="col-md-3 col-nav" id="nav-4"> 57 第四步 58 </div> 59 </div> 60 61 <div class='row'> 62 <div class="arrow-next" id='arrow-next'></div> 63 <div class='arrow-current' id='arrow-current'></div> 64 </div> 65 <br> 66 <br> 67 <div class='row'> 68 <div class="col-xs-6 col-sm-4 col-md-3"> 69 <button id='b1' class='btn btn-success'>第一步</button> 70 </div> 71 <div class="col-xs-6 col-sm-4 col-md-3"> 72 <button id='b2' class='btn btn-success'>第二步</button> 73 </div> 74 <div class="col-xs-6 col-sm-4 col-md-3"> 75 <button id='b3' class='btn btn-success'>第三步</button> 76 </div> 77 <div class="col-xs-6 col-sm-4 col-md-3"> 78 <button id='b4' class='btn btn-success'>第四步</button> 79 </div> 80 </div> 81 <br> 82 <br> 83 <div class='row'> 84 <div class='col-md-4 col-md-offset-4'> 85 <button id='clear_all' class='btn btn-success form-control'>清除樣式</button> 86 </div> 87 </div> 88 </div> 89 90 <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 91 <script src="js/jquery-1.11.1.min.js"></script> 92 93 <script> 94 $(function () { 95 96 $('[id^=nav-]').click(function () { 97 var position = $(this).position(); 98 var str = 'left:' + position.left + ",top" + position.top; 99 str = str + ',heigth:' + $(this).height() + ',width:' + $(this).width(); 100 alert(str); 101 }); 102 103 $('button[id^=b]').click(function () { 104 clearClass(); 105 var b = $(this).attr('id'); 106 var index = b.substr(-1, 1); 107 var current_div = 'nav-' + index; 108 var next_div = 'nav-' + (parseInt(index) + 1); 109 $('#' + current_div).addClass('current'); 110 cposition = $('#' + current_div).position(); 111 nposition = $('#' + next_div).position(); 112 113 cwidth = $('#' + current_div).width(); 114 left = parseInt(cposition.left) + parseInt(cwidth); 115 116 if (parseInt(index) === 1) 117 { 118 $('#arrow-next').css({'position': 'absolute', 'left': left, 'top': nposition.top, 'zindex': 10}).show(); 119 $('#arrow-current').hide(); 120 } 121 else if (parseInt(index) === 4) 122 { 123 $('#arrow-next').css({'position': 'absolute', 'left': (left + 1) + 'px', 'top': cposition.top}).show(); 124 $('#arrow-current').css({'position': 'absolute', 'left': cposition.left, 'top': cposition.top}).show(); 125 } else 126 { 127 $('#arrow-next').css({'position': 'absolute', 'left': left, 'top': nposition.top, 'zindex': 10}).show(); 128 $('#arrow-current').css({'position': 'absolute', 'left': cposition.left, 'top': cposition.top}).show(); 129 } 130 131 if ($(window).width() < 992) 132 { 133 $('#arrow-next').hide(); 134 $('#arrow-current').hide(); 135 } 136 }); 137 138 $('#clear_all').click(function () { 139 clearClass(); 140 }); 141 }); 142 143 function clearClass() 144 { 145 $('[id^=nav-]').each(function () { 146 $(this).removeClass('current').removeClass('arrow-current').removeClass('arrow-next'); 147 }); 148 } 149 </script> 150 </body> 151 </html>
這段代碼也挺簡單了,雖然花了我一下午的時間,水平有限,努力學習吧。