1.上一章--組件的使用(svg及watch的簡單使用)
2.蒼渡大神的項目源碼--項目源碼地址
3.UI框架--MintUI
4.數據接口地址--API地址
5.下一章--餓了麼loading圖css
1.UI圖
vue
2.miste.vue 修改以下node
<template> <div> <mt-header fixed class="fs1-2" title="標題過長會隱藏後面的內容啊哈哈哈哈"> <router-link to="/" slot="left"> <div><icon class="search2" name="search2" ></icon></div> </router-link> <mt-button class="fs0-8" slot="right">登陸|註冊</mt-button> </mt-header> <div class="padtop40"> <mt-swipe class="myswipe" :auto="0"> <mt-swipe-item v-for="items in category" :key="items.id" class="myitem"> <div v-for="item in items"> <div class="imgbox"><img :src="imgBaseUrl+item.image_url"></div> <div class="fs0-8 col9f mgtop5">{{item.title}}</div> </div> </mt-swipe-item> </mt-swipe> <div class="mgtop10 bgfff maindiv"> <div class="ih30 pad10"> <div class="svgbox left"> <icon name="shop" class="shopicon"></icon> </div> <div class="fs0-8 left col9f mgleft10"> 附近商家 </div> </div> <div class="shoplist after"> <div class="shopimgbox"> <img > </div> <div class="rightbox"> <div class="shoptop"> <span class="pinpai fs10 mgl">品牌</span> <span class="shopname">周哈黑鴨</span> <span class="rightspan right fs10 mgr"><span class="">保</span><span class="">準</span><span class="">票</span></span> </div> <div class="xxdiv"> <div class="xxbox fs10 mgl"> <span><icon name="xx" class="xx"></icon></span> <span><icon name="xx" class="xx"></icon></span> <span><icon name="xx" class="xx"></icon></span> <span class="xxspan1"><icon name="xx2" class="xx"></icon><span class="xxspan2 w5"><icon name="xx" class="xx"></icon></span></span> <span><icon name="xx2" class="xx"></icon></span> </div> <span class="colred fs10 mgl mgleft8">4.4</span> <span class="fs10 mgl">月售125單</span> <span class="rightspan fs10 right mgr"> <span class="fn ">蜂鳥專送</span> <span class="zs ">準時達</span> </span> </div> <div class="shopfoot"> <div><span class="fs10 mgl">¥20起送/配送費約¥5</span></div> <div><span class="right fs10 mgr"><span class="">102.3千米/</span><span class="col">15小時21分鐘</span></span></div> </div> </div> </div> <div class="shoplist after"> <div class="shopimgbox"> <img > </div> <div class="rightbox"> <div class="shoptop"> <span class="pinpai fs10 mgl">品牌</span> <span class="shopname">周哈黑鴨</span> <span class="rightspan right fs10 mgr"><span class="">保</span><span class="">準</span><span class="">票</span></span> </div> <div class="xxdiv"> <div class="xxbox fs10 mgl"> <span><icon name="xx" class="xx"></icon></span> <span><icon name="xx" class="xx"></icon></span> <span><icon name="xx" class="xx"></icon></span> <span class="xxspan1"><icon name="xx2" class="xx"></icon><span class="xxspan2 w5"><icon name="xx" class="xx"></icon></span></span> <span><icon name="xx2" class="xx"></icon></span> </div> <span class="colred fs10 mgl mgleft8">4.4</span> <span class="fs10 mgl">月售125單</span> <span class="rightspan fs10 right mgr"> <span class="fn ">蜂鳥專送</span> <span class="zs ">準時達</span> </span> </div> <div class="shopfoot"> <div><span class="fs10 mgl">¥20起送/配送費約¥5</span></div> <div><span class="right fs10 mgr"><span class="">102.3千米/</span><span class="col">15小時21分鐘</span></span></div> </div> </div> </div> <div class="shoplist after"> <div class="shopimgbox"> <img > </div> <div class="rightbox"> <div class="shoptop"> <span class="pinpai fs10 mgl">品牌</span> <span class="shopname">周哈黑鴨</span> <span class="rightspan right fs10 mgr"><span class="">保</span><span class="">準</span><span class="">票</span></span> </div> <div class="xxdiv"> <div class="xxbox fs10 mgl"> <span><icon name="xx" class="xx"></icon></span> <span><icon name="xx" class="xx"></icon></span> <span><icon name="xx" class="xx"></icon></span> <span class="xxspan1"><icon name="xx2" class="xx"></icon><span class="xxspan2 w5"><icon name="xx" class="xx"></icon></span></span> <span><icon name="xx2" class="xx"></icon></span> </div> <span class="colred fs10 mgl mgleft8">4.4</span> <span class="fs10 mgl">月售125單</span> <span class="rightspan fs10 right mgr"> <span class="fn ">蜂鳥專送</span> <span class="zs ">準時達</span> </span> </div> <div class="shopfoot"> <div><span class="fs10 mgl">¥20起送/配送費約¥5</span></div> <div><span class="right fs10 mgr"><span class="">102.3千米/</span><span class="col">15小時21分鐘</span></span></div> </div> </div> </div> </div> </div> <foot></foot> </div> </template> <script> import foot from '../../components/foot/foot' export default { data () { return { category:[], imgBaseUrl:'https://fuss10.elemecdn.com', //圖片域名地址 shoplist:"" } }, components:{ //註冊組件 foot }, mounted:function(){ //生命週期 //分類 this.$http.get('http://cangdu.org:8001/v2/index_entry').then(response => { console.log(response); var mybody=response.body; var num=parseInt(mybody.length/8); var category=[]; var arr=[]; for(var i=0;i<num;i++){ arr=[]; for(var h= 0;h<8;h++){ arr.push(mybody[i*8+h]); } category.push(arr); } arr=[]; if(num*8<mybody.length){ for(var k=num*8;k<mybody.length;k++){ arr.push(mybody[num*8+k]) } category.push(arr); } this.category=category; console.log(this.category); }, response => { console.log(response); }); }, computed:{ //計算屬性 }, methods:{ //函數 } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> .fs10{ font-size: 12px; transform: scale(.8); display:inline-block; } .mgr{ transform-origin:100% 50% 0;/*改變縮放基點*/ } .mgl{ transform-origin:0 50% 0;/*改變縮放基點*/ } .myswipe{ height:210px; box-sizing:border-box; padding-bottom:30px; padding-top:10px; background-color:#ffffff; border-bottom:1px solid #E4E4E4; } .myswipe .mint-swipe-indicators .is-active{ background-color:#26a2ff; } .myitem>div{ width:25%; height:50%; box-sizing:border-box; float:left; text-align:center; } .imgbox{ height:60%; } .imgbox img{ height:100%; } .shopicon{ width:100%; height:100%; margin-bottom:-2px; } .svgbox{ width:15px; height:15px; } .mgleft10{ margin-left:10px; } .shoplist{ padding:10px; overflow: hidden; display:flex; } .shoplist>div{ float:left; } .shopimgbox{ width:60px; height:60px; margin-right:5px; } .shopimgbox>img{ height:100%; width:100%; background-color:red; } .maindiv{ border-top:1px solid #E4E4E4; } .search2{ width:20px; height:20px; } .rightbox{ font-size:0.8rem; flex:1; } .shoptop,.xxdiv{ height:25px; line-height:25px; } .pinpai{ display:inline-block; width:30px; text-align:center; font-weight:bold; height:20px; line-height:20px; background-color:#ffd930; border-radius:3px; } .shopname{ font-weight:bold; font-size:0.8rem; } .rightspan>span{ display:inline-block; height:14px; border:1px solid #A3ACB7; margin-left:3px; padding:1px; color:#A3ACB7; line-height:14px; font-size:12px; } .xx{ width:10px; height:10px; } .xxbox{ display:inline-block; height:25px; box-sizing:border-box; } .xxbox>span{ margin-right:-2px; } .xxspan1{ position:relative; display:inline-block; } .xxspan2{ display:inline-block; position:absolute; left:0px; overflow: hidden; } .w1{ width:10%; } .w2{ width:20%; } .w3{ width:30%; } .w4{ width:40%; } .w5{ width:50%; } .w6{ width:60%; } .w7{ width:70%; } .w8{ width:80%; } .w9{ width:90%; } .fn{ background-color:#3190E8; color:white !important; border-color:#3190E8 !important; } .zs{ color:#3190E8 !important; border-color:#3190E8 !important; } .mgleft8{ margin-left:-8px; } .shopfoot>div{ width:50%; float:left; } </style>
頁面效果以下
git
商品分類是從API請求出來的,其餘的都只是死樣式。這裏有幾個要注意的地方github
商品分類一頁顯示八條,共16條數據,顯示兩頁,可是若是是18條數據呢?那就要前兩頁8條,第三頁兩條數據。
這就是FCC
社區裏大名鼎鼎的猴子吃香蕉
問題
一個數組,有n條數據,每隔m條數據分紅一個新數組,如有剩餘數據,則也加入一個新數組裏,最後將全部的新數組以數組形式輸出。如數組=[1,2,3,4],則n=4,m=2,結果爲[[1,2],[3,4]];數組=[1,2,3,4,5],則n=5,m=2,結果爲[[1,2],[3,4],[5]]segmentfault
解決思路數組
var mybody=response.body; //mybody爲要處理的數組,每隔8個分爲一個新數組 var num=parseInt(mybody.length/8); //num爲mybody裏含有幾個長度爲8的數組 var category=[]; //最後要輸出的數組 var arr=[]; //將mybody分爲若干個arr,最終將全部的arr加入category for(var i=0;i<num;i++){ arr=[]; for(var h= 0;h<8;h++){ arr.push(mybody[i*8+h]); } category.push(arr); } arr=[]; if(num*8<mybody.length){ //若是mybody不是8的倍數,循環剩下的個數 for(var k=num*8;k<mybody.length;k++){ arr.push(mybody[num*8+k]) } category.push(arr); } this.category=category;
評價爲4.0,就顯示四顆彩色的星星,一顆灰色的星星。
可是若是評分爲4.3或者4.8呢?框架
<span class="xxspan1"><icon name="xx2" class="xx"></icon><span class="xxspan2 w5"><icon name="xx" class="xx"></icon></span></span>
xxspan1
爲容器,相對定位svg
.xxspan1{ position:relative; display:inline-block; }
xx2爲灰色的星星,xx爲彩色的星星。xxspan2
爲存放彩色星星的容器,絕對定位,left
爲0,與xxspan1
重疊,覆蓋灰色的星星。函數
.xxspan2{ display:inline-block; position:absolute; left:0px; overflow: hidden; }
w5
來控制xxspan2
的長度
.w1{ width:10%; } .w2{ width:20%; } .w3{ width:30%; } .w4{ width:40%; } .w5{ width:50%; } .w6{ width:60%; } .w7{ width:70%; } .w8{ width:80%; } .w9{ width:90%; }
這樣就能夠經過class來控制彩色星星顯示的寬度。而灰色的星星則做爲彩色星星的背景。(這是下下策,哪位老鐵有更好的方法?)
當將字體設置小於12px時,谷歌會默認把字體設置爲12px。可能有老鐵說把這個默認事件阻止了不就好了?但這對安卓兼容性並很差,因此我們這裏用css的屬性來控制。
.fs10{ font-size: 10px; transform: scale(.8); display:inline-block; }
transform: scale(.8)
是縮放,把原來的元素縮放成原來的0.8,注意,這裏長度寬度也會縮放。能夠改變基點來讓元素從新回到最左端或者最右端
.mgl{ transform-origin:0 50% 0;/*改變縮放基點*/ }
第一個參數爲x軸,第二個參數是Y軸,第三個參數是z軸。
1.API
2.發送請求,在mounted
中寫
//商品列表 this.$http.get('http://cangdu.org:8001/shopping/restaurants?latitude=31.22967&longitude=121.4762').then(response => { console.log(response); this.shoplist=response.body; }, response => { console.log(response); });
這裏傳了兩個參數經緯度,應該是我們選擇的地點的經緯度,我們這裏先用死的數據。別忘了在data裏聲明shoplist
變量
能夠看到,數據已經請求回來了。共20條。
若是你的console
出現了大量的svg圖標的打印,像下面這樣
在node_modules
下的vue-svg-icon
下的Icon.vue
裏面第79行代碼
console.info(`src/svg/${this.name}.svg has been loaded`);
註銷掉便可
如今就是把數據加載到頁面裏,商家列表的代碼修改以下
<div v-for="item in shoplist" class="shoplist after"> <div class="shopimgbox"> <img :src="imgshopUrl+item.image_path"> </div> <div class="rightbox"> <div class="shoptop"> <span class="pinpai fs10 mgl">品牌</span> <span class="shopname">{{item.name}}</span> <span class="rightspan right fs10 mgr"><span v-for="itemsupports in item.supports" class="">{{itemsupports.icon_name}}</span></span> </div> <div class="xxdiv"> <div class="xxbox fs10 mgl"> <span v-for="(itemxx,index) in 5" class="xxspan1"><icon v-if="index+1<=item.rating" name="xx" class="xx"></icon><icon v-if="index+1>item.rating" name="xx2" class="xx"></icon><span v-if="item.rating-(index)>0&&item.rating-(index)<1" :class="xxclass+(item.rating*10-index*10)"><icon name="xx" class="xx"></icon></span></span> </div> <span class="colred fs10 mgl mgleft8">{{item.rating}}</span> <span class="fs10 mgl">月售{{item.recent_order_num}}單</span> <span class="rightspan fs10 right mgr"> <span v-if="item.delivery_mode" class="fn">{{item.delivery_mode.text}}</span> <span class="zs ">準時達</span> </span> </div> <div class="shopfoot"> <div><span class="fs10 mgl">¥{{item.float_minimum_order_amount}}起送/{{item.piecewise_agent_fee.tips}}</span></div> <div><span class="right fs10 mgr"><span class="">{{item.distance}}/</span><span class="col">{{item.order_lead_time}}</span></span></div> </div> </div> </div>
這裏惟一須要注意的地方就是星星的展現。數據只會給咱們返回4.7分,那咱們怎麼展現相應的星星
呢?
<span v-for="(itemxx,index) in 5" class="xxspan1"><icon v-if="index+1<=item.rating" name="xx" class="xx"></icon><icon v-if="index+1>item.rating" name="xx2" class="xx"></icon><span v-if="item.rating-(index)>0&&item.rating-(index)<1" :class="xxclass+(item.rating*10-index*10)"><icon name="xx" class="xx"></icon></span></span>
無論返回多少分,都會是5顆星星,因此咱們直接循環五次。而後咱們再根據當前的星星的索引index
與評分的比較來判斷展現哪種星星。
頁面效果以下
ok!展現成功!
這裏不得不注意,Mint ui 的header組件中間文字並無點擊效果。。因此這裏我們要本身寫一個,
<div class="myhead"> <icon class="search2" name="search2" ></icon> <div class="headmain nowarp fs1-2">標題過程機會隱藏啊啊啊啊啊啊</div> <div class="headright fs0-8">登陸|註冊</div> </div>
css以下
.myhead{ height:40px; box-sizing:border-box; line-height:40px; position:fixed; background-color:#26a2ff; width:100%; color:white; padding:0px 10px; text-align:center; z-index:1; } .myhead>div{ display:inline-block; } .headright{ position:absolute; right:10px; } .headmain{ max-width:55%; margin:0px auto; } .search2{ width:20px; height:20px; position:absolute; top:10px; left:10px; }
1.imgshopUrl
http://cangdu.org:8001/img/
2.xxclass
xxclass:"xxspan2 w", //星星的class