移動端頁面製做2

5:等分,居中等頁面佈局 javascript

等分

在說等分以前,先拋出一個問題,以下面的emmet代碼,footer部分的導航有些頁面是三個,有些頁面是四個,咱們要求的是不管是三個仍是四個甚至於5個,都平分寬度。php

 

footer.footer>ul.nav-links>li*3 footer.footer>ul.nav-links>li*4 

 

float

 

若是採用float技術的話,那估計只有在ul上添加額外的class來設置li的百分比寬度了。css

 

.nav-links li{ float:left; width:25%; } .percent-half li{ width:50%; } .percent-third li{ width:33.333%; } ... 

 

這個太蛋疼了,高上大的移動端怎麼能用這麼老套的東西呢,因此不考慮。html

 

table

 

也許這個技術會被不少人忘記,不過用在移動端確實不錯,關鍵是沒有兼容問題的。主要設置父元素的display: table;table-layout: fixed;width: 100%;,而後設置子元素爲display: table-cell;便可。html5

 

// table 等分 @mixin table-equal($children: li) { display: table; table-layout: fixed; width: 100%; $childrenEle: li div p a span strong; @if index($childrenEle, $children) { #{$children} { display: table-cell; } } @else { .#{$children} { display: table-cell; } } } .nav-links{ @include table-equal; } 

 

這個mixin內部定義了一個$childrenEle元素選擇器變量集合,若是傳入的參數是其中的一個,那麼直接使用元素選擇器,不然就當class選擇器使用,如默認的li解析後就是li{display: table-cell;},而若是傳入children,則解析後就是.children{display: table-cell;}。下面的flex一樣使用了該方法java

 

注:在移動端display: table;一樣也是個有利的神器,比起各類float什麼的,這個技術仍是能夠單刀直入,直指問題核心css3

 

flex

 

flex技術是個好技術,不過最關鍵的仍是其兼容問題,算起來它有三個版本,是有點亂哈哈。不過sandal的css3文件已經封裝好了,因此只管調用,它會自動生成對應的兼容代碼。git

 

// flex 等分 @mixin flex-equal($children: li) { @extend %display-flex; $childrenEle: li div p a span strong; @if index($childrenEle, $children) { #{$children} { @include flex(1); } } @else { .#{$children} { @include flex(1); } } } .nav-links{ @include flex-equal; } 

 

 

 

水平垂直居中

 

以簡單的彈窗爲例:github

 

<div class="overlay"> <section class="modal"> <div class="modal-bd"> <p>青,取之於藍,而青於藍;冰,水爲之,而寒於水。故木受繩則直,金就礪則利,君子博學而日參省乎己,則知明而行無過矣。</p> </div> </section> </div> 

 

也許看到這個結構,不少人都會納悶,由於你們看到更多的應該是:.overlay+section.modal,即蒙版與彈出內容是兄弟元素,而不是嵌套關係。這裏先賣個關子,到modal實例的時候,再分析。web

 

flex

 

樣式寫在父元素上

 

// flex center // display:flex %display-flex,%flex-display { @include display-flex; } @mixin flex-center($direction: both) { @extend %display-flex; @if $direction == both { @include justify-content(center); @include align-items(center); } @else if $direction == x { @include justify-content(center); } @else if $direction == y { @include align-items(center); } } .overlay{ z-index: 980; position: absolute; top: 0; right: 0; bottom: 0; left: 0; background-color: rgba(0,0,0,.8); @include flex-center; // overlay調用 } .modal{ background-color: #fff; border-radius: 5px; margin: 0 10px; overflow: hidden; .modal-bd{ padding: 15px; } } 

 

關於flex的單個元素水平垂直居中,新語法直接父元素爲flex,子元素設置margin爲auto便可,由於移動端還在使用舊語法,因此暫不使用margin這個方法,而是設置父元素的水平及垂直都居中

 

translate

 

樣式寫在要居中的元素上。原理就是先絕對定位,left/top爲50%,而後經過translate偏移-50%回去(translate偏移的百分比爲自身寬高的百分比),比從前的margin-top/left設置負值偏移回去高級點,由於設置margin必須得知道自身元素的寬高,而後設置具體的數字,而translate不用管自身的寬高,直接50%就能夠搞定

 

// translate 50% @mixin translate-center($direction: both) { position: absolute; @if $direction == both { top: 50%; left: 50%; @include translate(-50%, -50%); } @else if $direction == x { left: 50%; @include translate(-50%, 0); } @else if $direction == y { top: 50%; @include translate(0, -50%); } } .overlay{ z-index: 980; position: absolute; top: 0; right: 0; bottom: 0; left: 0; background-color: rgba(0,0,0,.8); } .modal{ @include translate-center; // modal調用 background-color: #fff; border-radius: 5px; width:300px; overflow: hidden; .modal-bd{ padding: 15px; } } 

 

上面的flex和translate兩個mixin均可以實現單獨的水平居中或垂直居中,傳入相應的x或y便可。不管是flex仍是translate水平垂直居中都有兩個很好的優點即無需藉助額外的空標籤,也無需知道子元素的具體寬高,這比從前的一些方法強多了

 

 

 

左右兩端對齊

 

對於左右兩端對齊,之前使用最多的可能就是float,position了,如今一樣能夠採用flex來搞定

 

// justify @mixin justify($extend: true) { @if $extend { @extend %justify; } @else { @extend %display-flex; @include justify-content(space-between); } } %justify { @include justify(false); } .justify{ @include justify; } 

 

 

 

總結

 

若是你開始作移動端,那麼flex和transform這兩大屬性有必要熟練運用,運用好了能解決不少問題。通常來講flex能夠用來實現一些佈局,不再用動不動就float了;而transform中的rotate及translate則能夠實現一些旋轉及位移移動,旋轉能夠搞定圖標的一些變化,而位移移動則能夠實現居中,位移動畫等。

具體頁面代碼以下:


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

<meta name="robots" content="noindex">
<meta content="width=device-width, initial-scale=1, user-scalable=no, minimal-ui" name="viewport">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta content="telphone=no" name="format-detection">
<title>等分</title>
<style id="jsbin-css">
.clearfix:before,.clearfix:after{content:"";display:table;}
.clearfix:after{clear:both;}
html,body{height:100%;}
html{font-family:"Helvetica Neue",Helvetica,STHeiTi,Arial,sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%;font-size:62.5%;}
body{margin:0;font-size:1.4rem;line-height:1.5;color:#333;background-color:#fff;}
article,aside,details,figcaption,figure,footer,header,hgroup,main,nav,section,summary{display:block;}
audio,canvas,progress,video{display:inline-block;vertical-align:baseline;}
audio:not([controls]){display:none;height:0;}
[hidden],template{display:none;}
a{background:transparent;text-decoration:none;-webkit-tap-highlight-color:transparent;color:#08c;}
a:active{outline:0;}
a:active{color:#069;}
abbr[title]{border-bottom:1px dotted;}
b,strong{font-weight:bold;}
dfn{font-style:italic;}
mark{background:#ff0;color:#000;}
small{font-size:80%;}
sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline;}
sup{top:-0.5em;}
sub{bottom:-0.25em;}
img{border:0;vertical-align:middle;}
svg:not(:root){overflow:hidden;}
hr{-moz-box-sizing:content-box;box-sizing:content-box;height:0;}
pre{overflow:auto;white-space:pre;white-space:pre-wrap;word-wrap:break-word;}
code,kbd,pre,samp{font-family:monospace,monospace;font-size:1em;}
button,input,optgroup,select,textarea{color:inherit;font:inherit;margin:0;}
button{overflow:visible;}
button,select{text-transform:none;}
button,html input[type="button"],input[type="reset"],input[type="submit"]{-webkit-appearance:button;cursor:pointer;}
button[disabled],html input[disabled]{cursor:default;}
button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0;}
input{line-height:normal;}
input[type="checkbox"],input[type="radio"]{box-sizing:border-box;padding:0;}
input[type="number"]::-webkit-inner-spin-button,input[type="number"]::-webkit-outer-spin-button{height:auto;}
input[type="search"]{-webkit-appearance:textfield;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box;}
input[type="search"]::-webkit-search-cancel-button,input[type="search"]::-webkit-search-decoration{-webkit-appearance:none;}
fieldset{border:1px solid #c0c0c0;margin:0 2px;padding:0.35em 0.625em 0.75em;}
legend{border:0;padding:0;}
textarea{overflow:auto;resize:vertical;}
optgroup{font-weight:bold;}
table{border-collapse:collapse;border-spacing:0;}
td,th{padding:0;}
html,button,input,select,textarea{font-family:"Helvetica Neue",Helvetica,STHeiTi,Arial,sans-serif;}
h1,h2,h3,h4,h5,h6,p,figure,form,blockquote{margin:0;}
ul,ol,li,dl,dd{margin:0;padding:0;}
ul,ol{list-style:none outside none;}
h1,h2,h3{line-height:2;font-weight:normal;}
h1{font-size:1.8rem;}
h2{font-size:1.6rem;}
h3{font-size:1.4rem;}
input::-moz-placeholder,textarea::-moz-placeholder{color:#ccc;}
input:-ms-input-placeholder,textarea:-ms-input-placeholder{color:#ccc;}
input::-webkit-input-placeholder,textarea::-webkit-input-placeholder{color:#ccc;}
*{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;}

.demo{line-height:44px;margin-bottom:20px;text-align:center;background-color:#0078e7;color:#fff;}
.flex-equal,.flex-center,.justify{display:-webkit-box;display:-ms-flexbox;display:-webkit-flex;display:flex;}

.flex-equal li{-webkit-box-flex:1;-ms-flex:1;-webkit-flex:1;flex:1;}
.table-equal{display:table;table-layout:fixed;width:100%;}
.table-equal li{display:table-cell;}

.demo-center{border:1px solid #ccc;margin:20px;height:200px;}
.demo-center .children{background:#0078e7;color:#fff;width:150px;line-height:5;text-align:center;}
.flex-center{-webkit-box-pack:center;-ms-flex-pack:center;-webkit-justify-content:center;justify-content:center;-webkit-box-align:center;-ms-flex-align:center;-webkit-align-items:center;align-items:center;}
.translate-center{position:relative;}
.translate-center .children{position:absolute;top:50%;left:50%;-webkit-transform:translate(-50%,-50%);transform:translate(-50%,-50%);}
.justify{-webkit-box-pack:justify;-ms-flex-pack:justify;-webkit-justify-content:space-between;justify-content:space-between;padding:0 10px;background:#0078e7;color:#fff;line-height:32px;}

</style>
</head>
<body>
<h2>flex等分</h2>
<ul class="flex-equal demo">
<li>手機</li>
<li>聯繫人</li>
<li>信息</li>
<li>主屏</li>
</ul>

<ul class="flex-equal demo">
<li>手機</li>
<li>聯繫人</li>
<li>信息</li>
</ul>
<h2>table等分</h2>
<ul class="table-equal demo">
<li>手機</li>
<li>聯繫人</li>
<li>信息</li>
<li>主屏</li>
</ul>
<ul class="table-equal demo">
<li>手機</li>
<li>聯繫人</li>
<li>信息</li>
</ul>
<h2>flex居中</h2>
<div class="flex-center demo-center">
<div class="children">子元素水平垂直居中</div>
</div>
<h2>translate居中</h2>
<div class="translate-center demo-center">
<div class="children">子元素水平垂直居中</div>
</div>
<h2>兩端對齊</h2>
<div class="justify"><h2>左邊對齊</h2><span>右邊對齊</span></div>


</body>
</html>

6:切入切出動畫

transition動畫

先定義要運動的元素在視覺範圍以外,以左方向進入爲例,同時定義transition:

.demo{
    @include translate3D(-2000px, 0, 0); -webkit-transition: -webkit-transform 0.3s ease-in-out; transition: transform 0.3s ease-in-out; } 

從進入視覺範圍來講,不論方向從上下仍是左右,最終都歸於0,因此進入的時候添加class translate-in,而離開的時候去掉translate-in便可

.translate-in{ @include translate3D(0, 0, 0); } 

animation動畫

先定義要運動的元素在視覺範圍以外,一樣以左方向爲例:

.demo{
    @include translate3D(-2000px, 0, 0); } 

再定義keyframes:

// 從左向右方向進入動畫 @mixin left-in($startX: -2000px, $endX: 0) { @include keyframes(left-in) { 0% { @include translate3d($startX, 0, 0); } 100% { @include translate3d($endX, 0, 0); } } .left-in { @include animation-name(left-in); @extend %animated; } } // 從右向左方向消失動畫 @mixin left-out($startX: 0, $endX: -2000px) { @include keyframes(left-out) { 0% { @include translate3d($startX, 0, 0); } 100% { @include translate3d($endX, 0, 0); } } .left-out { @include animation-name(left-out); @extend %animated; } } 

調用上面定義的keyframes,元素進入視覺範圍添加class left-in,元素離開視覺範圍則替換left-inleft-out,動畫結束後調用animationend事件,刪除left-out

@include left-in; @include left-out; 

解析後的css爲:

.left-in, .left-out { -webkit-animation-duration: 1s; animation-duration: 1s; -webkit-animation-fill-mode: both; animation-fill-mode: both; } @-webkit-keyframes left-in { 0% { -webkit-transform: translate3d(-2000px, 0, 0); } 100% { -webkit-transform: translate3d(0, 0, 0); } } @keyframes left-in { 0% { transform: translate3d(-2000px, 0, 0); } 100% { transform: translate3d(0, 0, 0); } } .left-in { -webkit-animation-name: left-in; animation-name: left-in; } @-webkit-keyframes left-out { 0% { -webkit-transform: translate3d(0, 0, 0); } 100% { -webkit-transform: translate3d(-2000px, 0, 0); } } @keyframes left-out { 0% { transform: translate3d(0, 0, 0); } 100% { transform: translate3d(-2000px, 0, 0); } } .left-out { -webkit-animation-name: left-out; animation-name: left-out; }

總結

transition動畫與animation動畫的區別在於:

一、transition動畫只能定義開始和結束位置,中間沒法定義;而keyframes則能夠定義n幀做爲中間的過渡幀。

二、對於切入切出動畫來講,transition動畫咱們只需添加刪除一個class便可完成,而animation動畫則須要切換兩個class,再在最後刪除class,比較複雜。

三、若是你的動畫不須要定製中間幀,那直接使用transition動畫便可,切換一個class就能夠了,運動結束時候能夠js調用transitionend函數,而若是須要定製中間幀,那麼仍是animation,固然animation的事件有三個animationstart,animationiteration,animationend

 

7:頁面圖標

背景圖片

首先咱們會選擇sprite形式,把全部的圖標都放在一個大圖中,而後考慮到retina屏,因此咱們的圖標應該設計爲實際大小的2倍,而後設置background-size爲實際大小。如下面的msg icon爲例:

icon msg

圖中的每一個icon大小爲24px,實際應用時,咱們是以12px來使用的:

%icon-msg{ display: inline-block; vertical-align: -2px; background:url(../images/icon-msg.png) no-repeat; background-size:26px 26px; // 整個sprite圖片大小的一半,注意不要採用50%,百分比是按元素大小來計算的,而不是背景圖片大小 } .icon-info{ @extend %icon-msg; background-position: -14px 0; width: 12px; height: 12px; } .icon-alert{ @extend %icon-msg; background-position: 0 -14px; width: 12px; height: 12px; } ... 

固然有時候圖標比較少,咱們爲了減小請求,也能夠直接把圖片轉成base64格式寫在css中,這裏推薦一個在線轉的工具:Encode Data URL

直接繪製

憑藉優秀的css3,咱們能夠應用其中一些屬性繪製一些簡單的圖標,如箭頭等,這裏咱們以繪製checkbox兩種狀態爲例:

icon checkbox

html:

<i class="icon-checkbox active"></i> <i class="icon-checkbox"></i> 

scss:

$primary: #0078e7 !default; .icon-checkbox{ width: 16px; height: 16px; display: inline-block; vertical-align: middle; border: 1px solid #ccc; background-color: #fff; line-height: 1; text-align: center; margin-right: 5px; &.active{ border-color: $primary; &::after{ content: ""; width: 8px; height: 3px; border-bottom: 2px solid $primary; border-left: 2px solid $primary; display: block; margin-top: 3px; margin-left: 2px; @include rotate(-45deg); } } } 

active狀態,經過after生成一個長方形,而後設置其border-bottom和border-left,再經過css3的rotate旋轉45便可,那個勾就是兩條邊框。

@font-face

sandal的字體圖標爲例,若是你以爲這些圖標不適合你,你能夠本身在icomoon中挑選合適的。

icons

sandal中字體圖標使用方法:

一、下載sandal放在d盤目錄下,在你的scss文件中導入sandal的base文件(若是不須要生成樣式,則導入function文件便可)及font-face文件

@import "d:/sandal/base"; @import "d:/sandal/ext/font-face/font-face"; 

二、根據本身須要覆蓋font-face文件夾中的變量,注意變量應該在導入font-face以前,能夠覆蓋的變量以下:

$fontFamily: icomoon !default; $fontFilePath: "../fonts/icomoon" !default; $fontClassPrefix: if !default; // icon-font $fontClassAllSwitch: true !default; $fontClassOutput: () !default; $fontPseudo: true !default; // 是否採用僞元素(before)生成圖標 

下面咱們改變下class的前綴,而後輸出全部的字體class

@import "d:/sandal/base"; $fontClassPrefix: icon-font; @import "d:/sandal/ext/font-face/font-face"; 

三、把font-face目錄下的fonts文件夾拷貝進解析後的css文件夾同目錄下,如css,js,fonts,images同目錄

根據上面的配置,貼出下面的html和解析後的css代碼:

html:

<i class="icon-font-wifi"></i> <i class="icon-font-comment"></i> <i class="icon-font-user"></i> <i class="icon-font-map"></i> ... 

css:

.icon-font-wifi::before, .icon-font-comment::before, .icon-font-user::before, .icon-font-map::before,...{ -webkit-box-sizing: border-box; box-sizing: border-box; } @font-face { font-family: icomoon; font-weight: normal; font-style: normal; src: url("../fonts/icomoon.eot"); src: url("../fonts/icomoon.eot?#iefix") format("eot"), url("../fonts/icomoon.svg#icomoon") format("svg"), url("../fonts/icomoon.woff") format("woff"), url("../fonts/icomoon.ttf") format("truetype"); } .icon-font-wifi::before, .icon-font-comment::before, .icon-font-user::before, .icon-font-map::before,...{ display: inline-block; vertical-align: -2px; font-family: icomoon; font-size: 1.6rem; line-height: 1; speak: none; font-style: normal; font-weight: normal; font-variant: normal; text-transform: none; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } .icon-font-wifi::before { content: "\e62f"; } .icon-font-comment::before { content: "\e601"; } .icon-font-user::before { content: "\e632"; } .icon-font-map::before { content: "\e61b"; } ... 

通常我是直接繪製和字體圖標都用,簡單的直接繪製就好,因此爲了區別二者的class,直接繪製的我使用icon爲前綴,而字體圖標使用if(icon-font縮寫)爲前綴,至於爲何要區別這二者的class呢,由於說不定你就得使用css3的屬性選擇器,好比i[class^="icon-"],i[class^="if-"]方便選擇控制樣式。

關於變量$fontPseudo這裏單獨說明下,由於使用字體圖標有兩種方法,一種是把對應的字符編碼直接寫在html中,而後設置字體便可,另外一種是html爲空白標籤,經過before或after的content來設置其內容,再設置字體。若是$fontPseudo爲false,則解析的css爲:

@font-face { font-family: icomoon; font-weight: normal; font-style: normal; src: url("../fonts/icomoon.eot"); src: url("../fonts/icomoon.eot?#iefix") format("eot"), url("../fonts/icomoon.svg#icomoon") format("svg"), url("../fonts/icomoon.woff") format("woff"), url("../fonts/icomoon.ttf") format("truetype"); } .icon-font-wifi, .icon-font-comment, .icon-font-user, .icon-font-map,...{ display: inline-block; vertical-align: -2px; font-family: icomoon; font-size: 1.6rem; line-height: 1; speak: none; font-style: normal; font-weight: normal; font-variant: normal; text-transform: none; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; }

注:我的只因此採用僞元素及把樣式寫在僞元素裏面,是由於有些時候可能想偷懶,一些圖標不直接採用一個空白標籤去定義,而是直接寫在某個元素的before或after僞元素上,那個時候只須要採用sass的extend對應圖標的僞元素便可。

 

8:頁面內滾動

原生滾動

原生滾動的屬性爲:-webkit-overflow-scrolling:touch;,若是是走高富帥的蘋果路線,是沒問題的,誰讓這是人家親生的呢;若是是安卓的話,我真不知道是支持仍是不支持,說不支持吧好像有點緩動效果,說支持吧好像把這條屬性砍掉也同樣,感興趣的能夠本身測試下(添加或刪除這個屬性對比下)

iscroll模擬

既然原生的安卓上不太靠譜,那就有必要用js來解決了,由於本人不擅長js,因此直接上iscroll,如今iscroll也已經到第五版本了,iscroll的github上有很全的例子,雖然比較簡單,不過入門仍是不錯的。這裏我也搞了兩個demo,在demo以前,有必要先說明下使用iscroll應該注意的一些事項:

html結構方面

iscroll要求至少兩層結構,wrap是一個固定的容器,overflow爲hidden,而scroll爲滾動的內容,若是開啓translate(默認開啓),則使用translate來實現偏移滾動,若是沒有則使用left/top來實現偏移滾動。若是wrap下面有多個直接子元素,即scroll有其餘兄弟元素則只對第一個子元素滾動,其餘的自動忽略。(wrap和scroll的class是隨便的,主要就是得有兩層結構)

<div class="wrap"> <div class="scroll"> ... </div> </div> 

如圖:

iscoll img

css樣式方面

除了要求wrap有寬高及overflow爲hidden,還得對scroll元素設置position爲relative或absolute,這樣才能設置偏移。

js方面

最簡單的就是直接new一個IScroll對象便可,其餘配置可直接參考官網的說明:

window.addEventListener('load',function(){ var myScroll = new IScroll('.wrap');}); document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false); 

獻上兩個簡單的demo:

因爲本人的js水平實在有限,關於這個我也分析不出來什麼前因後果,摘錄幾篇文章以供想學習的參考下吧。下面的資料都是iscroll 4的。(雖然官網已經介紹的足夠好了,但iscroll的坑仍是不少的,多看看總不賴)

參考資料:

 

9:圖片滾動

滾動原理

既然說到滾動,那跟咱們上篇說到的iscroll多少有點關係吧。下面對swipe的滾動原理和iscroll的滾動原理簡單分析下,以相同的結構爲例:

<div class="wrap"> <ul class="scroll"> <li></li> <li></li> ... </ul> </div> 

相同的是都須要一個能夠設置overflow爲hidden的容器wrap。不一樣的是iscroll是對scroll元素進行滾動,經過設置其translate值或left/top值來實現滾動;而swipe是對li進行滾動,每一個li有三種狀態——prev,current和next,prev狀態的時候向左偏移視窗的寬度,current狀態正好在視覺範圍內,next狀態向右偏移視窗的寬度。

實戰

一樣其github已經有了很好的api使用說明,這裏簡單說下其參數配置:

window.mySwipe = new Swipe(document.getElementById('slider'), { startSlide: 2, //起始圖片切換的索引位置,默認爲0 speed: 400, //動畫執行時間,單位毫秒,默認爲300 auto: 3000, //設置自動切換時間,單位毫秒,無默認值 continuous: true, //無限循環的圖片切換效果,默認爲true disableScroll: false, //阻止因爲觸摸而滾動屏幕,默認爲false stopPropagation: false, //阻止事件冒泡,默認爲false callback: function(index, elem) {}, //回調函數,滾動時調用 transitionEnd: function(index, elem) {} //回調函數,滾動的transition動畫結束後調用。 }); 

除此以外,swipe還提供了幾個比較實用的api,以下:

  •  prev()上一頁
  •  next()下一頁
  •  getPos()獲取當前頁的索引
  •  getNumSlides()獲取全部項的個數
  •  slide(index, duration)滑動方法

demo

一、簡單demo

css代碼以下,圖片大小最大爲100%寬度:

.wrap{ overflow: hidden; visibility: hidden; position: relative; width: 100%; height: 150px; } .wrap .inline-float{ overflow: hidden; position: relative; } .wrap .inline-float li { float: left; width: 100%; position: relative; text-align: center; } .wrap .inline-float img { max-width: 100%; }

二、多張圖片一組demo

即每一個li中多放幾張圖片,而後控制圖片的寬度,以防止擠掉

.wrap .inline-float img { max-width: 30%; margin-left: 10px; }

三、全屏圖片

這個關鍵在於把圖片設置背景圖片,而後採用background-size來調整,這裏採用的是100%,即li的寬高大小(demo中的四張圖片均不一樣大小,而後縮放統一呈現出來的大小就是li的大小),能夠根據本身的須要設置爲cover或contain什麼的

.wrap .inline-float li { float: left; width: 100%; height: 150px; position: relative; text-align: center; background-repeat:no-repeat; background-size: 100% 100%; background-color:#f5f5f5; }

注:上面的幾個demo好像是直接調用Swipe函數,而不是new一個對象,js修改成以下(jsbin中一修改連接數字就變了):

window.addEventListener('DOMContentLoaded', function(){ var $slide = document.getElementById('swipeSlide'); var mySwipe = new Swipe($slide,{ auto: 1000 }); }); 

四、添加子彈導航

js部分,設置callback函數,根據當前的位置爲子彈導航添加激活狀態

window.addEventListener('DOMContentLoaded', function(){ var $slide = document.getElementById('swipeSlide'), aBullet = $slide.querySelectorAll('.icon-bullet'), len = aBullet.length; var mySwipe = new Swipe($slide,{ auto: 3000, callback: function(i){ while(len--) { aBullet[len].classList.remove('active'); } aBullet[i].classList.add('active'); } }); });

10:側邊欄導航

設計結構以下:

<header class="header"></header>

<div class="wrap-page"> <section class="page"></section> ... </div>

<footer class="footer"></footer>

<section class="panel"></section>

第一種實現方案:

先將panel經過translate偏移負的自己寬度,離開可視區域,而後經過切換active這個class來實現無偏移。固然除此以外,top和bottom的0實現了100%高度,z-index要保證大於header和footer的層級。

$panelWidth: 120px !default; .panel{ position: absolute; top: 0; bottom: 0; left: 0; z-index: 980; width: $panelWidth; background-color: #333; @include translate3d(-$panelWidth, 0, 0); @extend %transition-transform; } .panel.active{ @include translate3d(0, 0, 0); } 

一樣咱們也能夠經過給body添加刪除class如panel-active來控制panel的位置。

第二種實現方案

在demo1的基礎上根據第二種方案順便處理下了當panel出現時,內容禁止滾動

由於須要實現整塊內容欄(包括header和footer部分)偏移panel的寬度,因此第一反應是應該有個div把整塊內容欄包裹下,以下:

<div class="wrap-container"> <header class="header"></header> <div class="wrap-page"> <section class="page"></section> ... </div> <footer class="footer"></footer> </div> <section class="panel"></section> 

多了一層結構,看起來有點不爽,不過使用起來仍是很爽的。首先panel偏移負的自己寬度,接下來經過控制wrap-container的class來實現內容欄偏移panel的寬度

.panel{
    position: absolute;
    top: 0; bottom: 0; left: 0; z-index: $zIndexOverlay; width: $panelWidth; background-color: #333; @include translate3d(-$panelWidth, 0, 0); } .wrap-container{ @extend %transition-transform; } .wrap-container.panel-active{ @include translate3d($panelWidth, 0, 0); } 

既然這裏須要一個父元素來實現一個偏移,爲何body不能夠呢?因此果斷幹掉wrap-container,恢復最初的結構

.panel{
    position: absolute;
    top: 0; bottom: 0; left: 0; z-index: $zIndexOverlay; width: $panelWidth; background-color: #333; @include translate3d(-$panelWidth, 0, 0); } body.has-panel{ @extend %transition-transform; } body.panel-active{ @include translate3d($panelWidth, 0, 0); } 

總結

通常來講使用比較多的仍是第二種方案,由於第一種直接把左邊的那個點擊圖標遮蓋住了。而panel實際使用的時候仍是挺不太好辦的,由於左邊的第一個icon通常都是放首頁,返回什麼的,固然適用不適用仍是根據各自業務須要走

 

11:彈框

設計結構以下:

<header class="header"></header> <div class="wrap-page"> <section class="page"></section> ... </div> <footer class="footer"></footer> <div class="overlay"> <section class="modal"> <div class="modal-hd"></div> <div class="modal-bd"></div> <div class="modal-ft"></div> </section> </div> 

前面的第n篇文章提過這個overlay遮罩層的問題,如今這裏說明下爲何這麼設計。

通常來講看到的overlay都與modal是兄弟元素,而不是嵌套關係。原本我也是這麼設計的,這就是習慣。後來因爲modal居中的問題,從新審視了下這個問題:

爲何遮罩層的overlay與彈窗內容是兄弟元素?

說實話真想不出什麼理由,非得搞成兄弟元素。後來忽然意識到之前的遮罩層若是不採用半透明圖片的話,就得使用opacity(ie6-8不支持,經過濾鏡模擬),而這個屬性會對整個子元素都起做用,並且還沒辦法經過子元素覆寫這個值。這是我能想到的一條最佳理由,若是還有其餘理由歡迎交流。

對於高上大的移動端來講,都是rgba時代了,因此opacity回家吃飯先。既然這個對子元素的影響已經不是問題,那麼嵌套關係就能夠成立,並且嵌套還有一個很是好的理由,水平垂直居中,flex小指一動便可。而兄弟元素的水平垂直居中就得設置modal的top和left的值爲50%,而後再設置translate的x和y方向都-50%

因此果斷拋棄兄弟元素設計換成嵌套關係。

由於overlay採用了flex佈局來控制子元素居中,因此不難呢過採用display爲none/block來顯示隱藏遮罩層overlay,而是經過z-index的層級來控制,而modal部分經過添加刪除modal-in這個class來控制顯示隱藏

scss代碼以下:

.overlay{
    position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: -1; background-color: rgba(0,0,0,.8); @include flex-center; // flex水平垂直居中 } .overlay.active { z-index: 980; } $modalBarHeight: 40px !default; $modalBdPadding: 15px; .modal{ background-color: #fff; border-radius: 5px; margin: 0 10px; overflow: hidden; opacity: 0; @include transform(translate3d(0,0,0) scale(0.815)); @extend %all-transition; @include transition-property(transform, opacity); &.modal-in{ opacity: 1; @include transform(translate3d(0,0,0) scale(1)); } .modal-hd{ text-align: center; line-height: $modalBarHeight; background-color: $primary; color: #fff; } .modal-bd{ padding: $modalBdPadding; } .modal-ft{ border-top: 1px solid $gray; @extend %display-flex; .btn-modal{ @include flex(1); background-color: #fefefe; text-align: center; line-height: $modalBarHeight; color: $primary; &:first-child{ border-right: 1px solid $gray; } &:last-child{ border-right: none; } &:hover,&:active{ background-color: #d9d9d9; } } } }


<script>
$(function(){
var $overlay = $('#overlay');

function modalHidden($ele) {
$ele.removeClass('modal-in');
$ele.one('transitionend',function(){
$ele.css({"display": "none"});
$overlay.removeClass('active');
});
}

$('.modal-link').tap(function(){
var $that = $(this);
$overlay.addClass('active');
var $whichModal = $('.'+$(this).data('modal'));
$whichModal.animate({"display":"block"},100,function(){
$(this).addClass('modal-in');
});

$('.btn-modal').tap(function(e){ modalHidden($whichModal); e.stopPropagation(); }); $overlay.tap(function(e){ if(e.target.classList.contains('overlay')){ modalHidden($whichModal); } }); }); });</script>

相關文章
相關標籤/搜索