使用Less製作帶箭頭提示框,less製作箭頭
這裡使用Less來製作帶箭頭的提示框,跟css比起來,方便多了,可以剩下很多的代碼,別的不扯了,先看看效果吧。
這裡我實現了4個方向的提示框,下面是具體實現代碼:
提示框代碼:
<div class="top_prompt"> <div class="top_triangle"> <span class="line"></span> <span class="back"></span> </div> <div class="prompt_border"> 上提示框<br/> </div> </div> <div class="left_prompt"> <div class="top_triangle"> <span class="line"></span> <span class="back"></span> </div> <div class="prompt_border"> 左提示框<br/> </div> </div> <div class="right_prompt"> <div class="top_triangle"> <span class="line"></span> <span class="back"></span> </div> <div class="prompt_border"> 右提示框<br/> </div> </div> <div class="bottom_prompt"> <div class="top_triangle"> <span class="line"></span> <span class="back"></span> </div> <div class="prompt_border"> 下提示框<br/> </div> </div>
less部分:
這裡將實現提示框的代碼封裝到一個library.less檔案中的,方面多個地方調用,其他less檔案使用該檔案時,直接使用@import "library";就可以引入該檔案了
library.less檔案代碼:
//邊框.border(all,@b_w:1px,@color:#cdcdcd,@style:solid){border:@b_w @style @color;}//上邊框.border(top,@b_w:1px,@color:#cdcdcd,@style:solid){border-top:@b_w @style @color;}//右邊框.border(right,@b_w:1px,@color:#cdcdcd,@style:solid){border-right: @b_w @style @color;}//下邊框.border(bottom,@b_w:1px,@color:#cdcdcd,@style:solid){border-bottom:@b_w @style @color;}//左邊框.border(left,@b_w:1px,@style:solid,@color:#cdcdcd){border-left: @b_w @style @color;}//定位->相對定位.position(r){position: relative;}//定位->絕對位置.position(a){position: absolute;}//定位->固定定位.position(f){position: fixed;}//字型.font(@size:14px,@lh:30px,@style:normal,@family:"微軟雅黑"){font:@style @size/@lh @family;}//內邊距.padd(all,@distance:10px){padding:@distance;}.padd_top_left(@top_distance:5px,@left_distance:10px){ padding:@top_distance @left_distance;}//上內邊距.padd(top,@distance:10px){padding-top: @distance;}//右內邊距.padd(right,@distance:10px){padding-right: 10px;}//下內邊距.padd(bottom,@distance:10px){padding-bottom: @distance;}//左內邊距.padd(left,@distance:10px){padding-left: @distance;}//外邊距.margin(all,@distance:10px){margin:@distance;}.margin_top_left(@top_distance:5px,@left_distance:10px){ margin:@top_distance @left_distance;}//上外邊距.margin(top,@distance:10px){margin-top:@distance;}//右外邊距.margin(right,@distance:10px){margin-right:@distance;}//下外邊距.margin(bottom,@distance:10px){margin-bottom:@distance;}//左外邊距.margin(left,@distance:10px){margin-left:@distance;}/**向上的三角top:匹配模式@b_width:三角的寬度@color:三角的顏色*/.triangle(top,@b_width:10px,@color:#cdcdcd) when(iscolor(@color)){ border-width: @b_width; border-color:transparent transparent @color transparent; border-style:solid dashed dashed dashed;}/**向右的三角right:匹配模式@b_width:三角的寬度@color:三角的顏色*/.triangle(right,@b_width:10px,@color:#cdcdcd) when(iscolor(@color)){ border-width: @b_width; border-color: transparent transparent transparent @color; border-style: dashed solid dashed dashed;}/**向下的三角bottom:匹配模式@b_width:三角的寬度@color:三角的顏色*/.triangle(bottom,@b_width: 10px,@color: #cdcdcd) when(iscolor(@color)){ border-width: @b_width; border-color: @color transparent transparent transparent; border-style: dashed dashed solid dashed;}/**向左的三角bottom:匹配模式@b_width:三角的寬度@color:三角的顏色*/.triangle(left,@b_width: 10px,@color: #cdcdcd) when(iscolor(@color)){ border-width: @b_width; border-color: transparent @color transparent transparent ; border-style: dashed dashed dashed solid;}//這裡是上面的公公樣式,不需要手動調用,less會自動調用.triangle(@_,@b_width:10px,@color:#cdcdcd){ width: 0px; height: 0px; display: inline-block;}//圓角.border-radius(@raiuds:5px){ border-radius:@raiuds; -webkit-border-radius: @raiuds; -moz-border-radius: @raiuds;}/*箭頭向上的提示框top:匹配模式@pb_w:提示框的寬度@color:提示框邊框顏色*/.prompt(top,@pb_w:200px,@color: #cdcdcd){ .top_triangle{top:-20px;left:10px;.line{ .triangle(top,10px,@color);//向上的三角}.back{ .triangle(top,10px,#ffffff);//向上的三角 top:1px;} }}/*箭頭向左的提示框left:匹配模式@pb_w:提示框的寬度@color:提示框邊框顏色*/.prompt(left,@pb_w:200px,@color: #cdcdcd){ .top_triangle{left:-20px;top:10px;.line{ .triangle(left,10px,@color);//向左的三角}.back{ .triangle(left,10px,#ffffff);//向左的三角 left:1px;} }}/*箭頭向右的提示框right:匹配模式@pb_w:提示框的寬度@color:提示框邊框顏色*/.prompt(right,@pb_w:200px,@color: #cdcdcd){ .top_triangle{left:@pb_w+21;top:10px;.line{ .triangle(right,10px,@color);//向右的三角}.back{ .triangle(right,10px,#ffffff);//向右的三角 left:-1px;} }}/*箭頭向下的提示框bottom:匹配模式@pb_w:提示框的寬度@color:提示框邊框顏色*/.prompt(bottom,@pb_w:200px,@color: #cdcdcd){ .top_triangle{bottom: 0px;left:10px;.line{ .triangle(bottom,10px,@color);//向下的三角}.back{ .triangle(bottom,10px,#ffffff);//向下的三角 top:-1px;} }}//提示框公用樣式,不需要手動調用.prompt(@_,@pb_w:200px,@color: #cdcdcd){ .position(r);//相對定位 .margin(top,5px);//上外邊距 .top_triangle{z-index: 1;.position(a);//絕對位置.line{ .position(a);//絕對位置}.back{ .position(a);//絕對位置} } .prompt_border{.position(r);//相對定位.border(all,1px,@color);//邊框.padd_top_left();//內邊距background: #ffffff;width: @pb_w;.border-radius();//圓角.font(); }}
使用另外一個檔案調用:
@import "lib/library";.top_prompt { .prompt(top);}.left_prompt{ .prompt(left);}.right_prompt{ .prompt(right);}.bottom_prompt{ .prompt(bottom);}
編譯less成為css檔案,我使用的是Koala的工具,當然這裡代碼要對less熟悉的人才能看懂,如果對less不熟悉,可以先去瞭解下less