前言:上一篇項目總結介紹了頁面骨架的開發、header組件的開發,這一篇主要梳理:商品組件開發、商品詳情頁實現。項目github地址:https://github.com/66Web/ljq_eleme,歡迎Star。javascript
goods |
1、商品組件開發 |
<router-view :seller="seller"></router-view>
兩欄佈局-flex佈局手機屏幕自適應css
<div class="goods">
<div class="menu-wrapper"></div>
<div class="foods-wrapper"></div>
</div>
.goods
display: flex
position: absolute
top: 174px
bottom: 46px
width: 100%
overflow: hidden
.menu-wrapper
flex: 0 0 80px
width: 80px
background: #f3f5f7
.foods-wrapper
flex: 1
左側佈局-菜單列表html
.menu-item
display: table
height: 54px
width: 56px
line-height: 14px
.text
display: table-cell
width: 56px
vertical-align: middle
font-size: 12px
右側佈局-食品列表vue
列表滾動-better-scroll 第三方JS庫java
cnpm install better-scroll --save
import BScroll from 'better-scroll';
<div class="menu-wrapper" ref="menuWrapper">
<div class="foods-wrapper" ref="foodsWrapper">
better-scroll初始化:git
methods: { _initScroll(){ this.meunScroll=new BScroll(this.$refs.menuWrapper,{}); this.foodsScroll=new BScroll(this.$refs.foodsWrapper,{}); } }
成功回調函數中調用_initScroll方法:github
this.$nextTick(()=>{ this._initScroll(); })
this.$nextTick()這個方法做用是當數據被修改後使用這個方法會回調獲取異步更新後的dom再render出來 web
若是不在下面的this.$nextTick()方法裏回調這個方法,數據改變後再來計算滾動軸就會出錯npm
左右聯動數組
data () { return { goods:[], listHeight: [] } }
爲了獲取高度,給food-list定義一個class--food-list-hook,不用來編寫css,專門用來獲取DOM元素,沒有實際的效果,只是用來被js選擇的
<li v-for="item in goods" :key="item.id" class="food-list food-list-hook">
定義foodList拿到每一個li,每一個li是包括包括標題在內的每一類food的高度,不是單獨的一種good,將_calculateHeight放在nextTick中初始化_initScroll的後面,保證其能正確計算到高度
_calculateHeight() { //food-list-hook類的添加知識爲了能拿到food列表,例如,拿到的是多個相似整個粥品的區塊
let foodList = this.$refs.foodsWrapper.getElementsByClassName('food-list-hook'); let height = 0; this.listHeight.push(height); //listHeight是一個遞增的區間數組,是每一個專區高度的累加
for (let i = 0; i < foodList.length; i++) { let item = foodList[i]; height += item.clientHeight; this.listHeight.push(height); } }
在data中定義一個scrollY對象,用來跟蹤滾動的高度 scrollY:0;在初始化betterScroll時,爲右側添加probeType--能夠檢測到右側實時滾動的位置,監聽scroll,將其實時滾動的位置暴露出來
data () { return { goods:[], listHeight: [], scrollY: 0 } }
_initScroll() { this.meunScroll=new BScroll(this.$refs.menuWrapper,{ click: true //使better-scroll可點擊,默認派發一個點擊事件
}); this.foodsScroll=new BScroll(this.$refs.foodsWrapper,{ click: true, probeType: 3 //BScroll滾動時,能實時告訴咱們滾動的位置,相似探針的效果
}); //foodsScroll監聽事件,在scroll滾動時能見位置實時暴露出來
this.foodsScroll.on('scroll', (pos) => { this.scrollY = Math.abs(Math.round(pos.y));//自己是個負值,取正值
}) }
拿到滾動的高度和內容區的固定高度以後, 查看scrollY落在哪一個區間,並返回那個區間的索引(!height2是測試最後一個區間的)其中,>= 向下的是一個閉區間,這樣第一個就會高亮了
computed: { currentIndex() { //currentIndex對應菜單欄的下標
for (let i = 0; i < this.listHeight.length; i++) { //不要忘了加this引用
let height1 = this.listHeight[i]; let height2 = this.listHeight[i + 1]; //得到了一個區間的上下範圍,判斷scrollY落到這個區間,!height2是判斷最後一個區間
//避免i溢出,>= 向下的是一個閉區間,這樣第一個就會高亮了
if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) { return i; //映射到第5行menu的變化
} } return 0; }
拿到index以後,回到左側的menu區,當咱們遍歷menu的時候,若是$index等於咱們計算獲得的currentIndex時,就爲當前的li添加一個current樣式
<!-- 若是index等於currentIndex,就爲這個li添加一個current類,改變左側導航欄的背景顏色-->
<li v-for="(item,index) in goods" :key="item.id"
class="menu-item" :class="{'current': currentIndex === index}" @click = "selectMenu($index, $event)">
&.current
position: relative
z-index: 10
margin-top: -1px
background: #ffffff
font-weight: 700
.text
border-none()
selectMenu (index, event) { if (!event._constructed) { //瀏覽器直接return掉,去掉自帶click事件的點擊
return; } let foodList = this.$refs.foodsWrapper.getElementsByClassName('food-list-hook'); let ref = foodList[index]; //取到index對應的DOM
this.foodsScroll.scrollToElement(ref, 300);//滾動到DOM所在位置
//console.log(index);
}
購物車組件
<div class="content">
<div class="content-left"></div>
<div class="content-right"></div>
</div>
.content display: flex background: #141d27 .content-left flex: 1 /*讓全部彈性盒模型對象的子元素都有相同的長度,忽略它們內部的內容*/ .content-right flex: 0 0 105px /*flex三個參數依次表示:等分、內容縮放狀況、站位空間*/
width: 105p
display:inline-block有一個默認間隙的問題 —— 解決:父級font-size:0
cartcontrol組件
Vue import Vue from 'vue';
使用Vue.set接口:
Vue.set(this.food, 'count', 1);
<transition name="move">...</transition>
.cart-decrease display: inline-block padding: 6px transform: translate3d(0, 0, 0) transform: rotate(0) &.move-enter-active, &.move-leave-active transition: all 0.4s linear transform: translate3d(0, 0, 0) transform: rotate(0) &.move-enter, &.move-leave-active opacity: 0 transform: translate3d(24px, 0, 0)/*開啓硬件加速,讓動畫更流暢*/ transform: rotate(180deg) .inner display: inline-block line-height: 24px font-size: 24px color: rgb(0, 160, 220)
購物車拋物線小球動畫
data() { return { balls: [{ //每個成員都用來維護當前小球的狀態,初始狀態都是隱藏的
show: false }, { show: false }, { show: false }, { show: false }, { show: false }], //添加一個變量,用來存貯已經下落的小球
dropBalls: [], fold: true //購物車詳情列表默認摺疊
}; }
<div class="ball-container">
<div v-for="(ball, index) in balls" :key="index">
<div class="ball-container">
<div v-for="(ball, index) in balls" :key="index">
<transition name="drop" >
<div v-show="ball.show" class="ball">
<div class="inner inner-hook"></div>
</div>
</transition>
</div>
</div>
</div>
</div>
<div v-for="(ball, index) in balls" :key="index" v-show="ball.show"></div>
.ball-container
position: fixed
left: 32px
bottom: 22px
z-index: 200
.inner
width: 15px
height: 15px
border-radius: 50%
background-color: #00A0DC
transition: all 1s linear
&.drop-enter-active
transition: all 1s cubic-bezier(0.49, -0.29, 0.75, 0.41)
CSS3 三次貝塞爾曲線(cubic-bezier):
貝塞爾曲線經過控制曲線上的四個點(起始點、終止點以及兩個相互分離的中間點)來創造、編輯圖形, |
使用js動畫鉤子,vue在實現動畫的時候提供了幾個javascript鉤子,可配合css動畫一塊兒使用,也可單獨使用
methods: { dropMove(el) { // console.log(el) for(let i=0; i<this.balls.length; i++) { let ball = this.balls[i]; if(!ball.show) { ball.show = true; ball.el = el; this.dropBalls.push(ball); return; } } }, beforeEnter(el, done) { let count = this.balls.length; while (count--) { let ball = this.balls[count]; if(ball.show) { let rect = ball.el.getBoundingClientRect();//返回元素的大小及其相對於視口的位置 let x = rect.left - 32 //ball-container left:32 let y = -(window.innerHeight - rect.top -22); el.style.display = ''; el.style.transform = `translate3d(0,${y}px,0)`;//外層元素縱向移動 el.style.webkitTransform = `translate3d(0,${y}px,0)`; let inner = el.getElementsByClassName('inner-hook')[0];//內層元素橫向移動 inner.style.webkitTransform = `translate3d(${x}px, 0, 0)`; inner.style.transform = `translate3d(${x}px, 0, 0)`; // console.log(el); } } }, dropEnter(el, done) { /*手動取到offsetHeight, 觸發瀏覽器重繪*/ let rf = el.offsetHeight; this.$nextTick(() => { //樣式重置回來 el.style.webkitTransform = 'translate3d(0, 0, 0)'// 設置小球掉落後最終的位置 el.style.transform = 'translate3d(0, 0, 0)' let inner = el.getElementsByClassName('inner-hook')[0] inner.style.webkitTransform = 'translate3d(0, 0, 0)' inner.style.transform = 'translate3d(0, 0, 0)' el.addEventListener('transitionend', done) // Vue爲了知道過渡的完成,必須設置相應的事件監聽器。它能夠是transitionend或 animationend }) // console.log(el); }, afterEnter(el) { let ball = this.dropBalls.shift(); if(ball) { ball.show = false; el.style.display = 'none'; } // console.log(el); }
每一個鉤子都有一個參數el: 當前執行transition動畫的DOM對象 當咱們點擊觸發一個過渡的時候,咱們在beforeEnter裏先拿到當前元素的偏移位置, 關於tansition實踐詳解博客:【重點突破】—— Vue2.0 transition 動畫Demo實踐填坑 |
2、food商品詳情頁實現 |
food |
商品詳情頁實現-food.vue組件
.food
position: fixed
left: 0
top: 0
bottom: 48px
z-index: 30
width: 100%
background: #ffffff
transform: translate3d(0, 0, 0)
&.move-enter-active, &.move-leave-active
transition: all 0.2s linear
transform: translate3d(0, 0, 0)
&.move-enter, &.move-leave-active
opacity: 0
transform: translate3d(100%, 0, 0)
.image-header
position: relative
width: 100%
height: 0
padding-top: 100% // 百分比是相對於盒子的寬度來計算的,看起來就像是一個正方形
img
position: absolute
top: 0
left: 0
width: 100%
height: 100%
&.buy-enter-active, &.buy-leave-active
transition: all 0.2s
opacity: 1
&.buy-enter, &.buy-leave-active
opacity: 0
@click.stop.prevent="decreaseCart($event)"
@click.stop.prevent="addCart($event)"
@click.stop.prevent="addFirst($event)"
split組件實現
<template> <div class="split"></div> </template> <script type="text/ecmascript-6"> export default {}; </script> <style lang="stylus" rel="stylesheet/stylus"> .split width 100% height 16px border-top: 1px solid rgba(1, 17, 27, 0.1); border-bottom: 1px solid rgba(1, 17, 27, 0.1); background: #f3f5f7 </style>
商品評價 - ratingselect 組件
<v-ratingselect :select-type="selectType" :only-content="onlyContent" :desc="desc" :ratings="food.ratings" @increment="incrementTotal">
</v-ratingselect>
const POSITIVE = 0; const NEGATIVE = 1; const ALL = 2; export default { //須要一些評價數據才能完成評價組件
props: { ratings: { type: Array, default() { return []; } }, selectType: { //所有,滿意,不滿意
type: Number, default: ALL //默認狀況時ALL,值等於2
}, onlyContent: { //只看有內容的評價仍是全部的評價
type: Boolean, default: false //設置爲能夠看到全部的評價
}, desc: { //描述
type: Object, default() { //默認desc是這三種,在商品詳情頁的時候傳入推薦或者吐槽
return { all: '所有', positive: '滿意', negative: '不滿意' }; } } },
在food.vue(商品詳情頁)中引入ratingSelect組件的時候,將desc改爲"所有","推薦"和"吐槽",接下來寫DOM佈局:
<template> <div class="ratingselect"> <div class="rating-type" border-1px> <span>{{desc.all}}</span> <span>{{desc.positive}}</span> <span>{{desc.negative}}</span> </div> <div @click="toggleContent($event)" class="switch" :class="{'on':oContent}"> <span class="icon-check_circle"></span> <span class="text">只看有內容的評價</span> </div> </div> </template>
在food.vue(商品詳情頁)的data中掛載對上述對象的跟蹤,並對其進行初始化
const POSITIVE = 0; const NEGATIVE = 1; const ALL = 2; data () { return { showFlag: false, selectType: ALL, onlyContent: false, //先設置組件一開始顯示有內容的評價
desc: { //desc作了改變
all: '所有', positive: '推薦', negative: '吐槽' } }; }
需求:在切換不一樣商品的時候能有相同的初始化狀態 —— 定義show()做爲goods組件中調用food組件的函數,即點開商品詳情的顯示函數,將初始化設置傳入到show()中
show() { //能夠被父組件調用到,方法前加下劃線通常是私有方法
this.showFlag = true; //初始化部分,ratingselect組件是被被不一樣的商品使用的,因此咱們但願在點開不一樣的商品時,能有同樣的初始化狀態
this.selectType = ALL; this.onlyContent = false; //展現界面時用到BScroll
this.$nextTick(() => { if (!this.scroll) { this.scroll = new BScroll(this.$refs.food, { click: true // 能夠被點擊
}); } else { this.scroll.refresh(); } }); }
.ratingselect
.rating-type
padding 18px 0
margin 0 18px //保證橫線的長度
border-1px(rgba(7,17,27,0.1))
font-size 0
.block //沒有寫文字的時候是沒有被撐開的
display inline-block
padding 8px 12px
margin-right 8px
border-radius 1px
line-height 16px
font-size 12px
color rgb(77,85,93)
&.active // block的active要設置一下
color #ffffff
.count
margin-left 2px
font-size 8px
&.positive
background rgba(0,160,220,.2)
&.active
background rgb(0,160,220)
&.negative
background rgba(77,85,93,0.2)
&.active
background rgb(77,85,93)
.switch
padding 12px 18px
line-height 24px
border-bottom 1px solid rgba(7,17,27,0.1)
color rgb(147,153,159)
font-size 0
&.on
.icon-check_circle
color #00c850
.icon-check_circle
display inline-block
vertical-align top
margin-right 4px
font-size 24px
.text
display inline-block
vertical-align top
font-size 12px
:class="{'active':selectType===2}"
display: inline-block
vertical-align: top
由於rating下有一條border,因此在rating下不能夠設置四周的padding值,若是設置了border就撐不開整個屏幕了
.rating //由於要在rating title下方畫一條橫線,因此不能用padding-left,改用title的margin代替
padding-top: 18px
.title
line-height 14px
margin-left 18px
font-size 14px
color rgb(7,17,27)
data() { return { sType : this.selectType, oContent : this.onlyContent } }
<template> <div class="ratingselect"> <div class="rating-type" border-1px> <span class="block positive" @click="select(2,$event)" :class="{'active':sType === 2}">{{desc.all}}<span class="count">{{ratings.length}}</span> </span> <span class="block positive" @click="select(0,$event)" :class="{'active':sType === 0}">{{desc.positive}}<span class="count">{{positives.length}}</span></span> <span class="block negative" @click="select(1,$event)" :class="{'active':sType === 1}">{{desc.negative}}<span class="count">{{negatives.length}}</span></span> </div> <div @click="toggleContent($event)" class="switch" :class="{'on':oContent}"> <span class="icon-check_circle"></span> <span class="text">只看有內容的評價</span> </div> </div> </template>
編寫rating-type和swicth切換有內容評價部分的綁定函數:select(type, event) —— 在點擊的時候就把類型123傳進去,傳入event是由於外層是一個betterScroll,要進行點擊事件的判斷,將sType的值更新以後經過emit將函數派發出去;
methods: { select (type, event) { //點擊的時候外層是有一個BScroll的,因此要傳遞event阻止默認點擊 if (!event._constructed) { //瀏覽器直接return掉,去掉自帶click事件的點擊 return; } //將this.selectType設置成傳入的參數,而不是food傳過來的初始化的值,以後樣式就能夠隨着點擊改變了 this.sType = type; /派發事件通知父組件food.vue selectType的改變,將type值傳出去
console.log('ratingselect.vue ' + type); this.$emit('increment', 'selectType', this.sType); }, toggleContent (event) { if (!event._constructed) { //瀏覽器直接return掉,去掉自帶click事件的點擊 return; } this.oContent = !this.oContent; console.log('ratingselect.vue ' + this.oContent); this.$emit('increment', 'onlyContent', this.oContent); } }
統計不一樣評價的數量(過濾評價類型),添加計算屬性 -- positives和negitives數組,長度即爲評價數量
<div class="rating-type" border-1px>
<span class="block positive" @click="select(2,$event)" :class="{'active':sType === 2}">{{desc.all}}<span class="count">{{ratings.length}}</span> </span>
<span class="block positive" @click="select(0,$event)" :class="{'active':sType === 0}">{{desc.positive}}<span class="count">{{positives.length}}</span></span>
<span class="block negative" @click="select(1,$event)" :class="{'active':sType === 1}">{{desc.negative}}<span class="count">{{negatives.length}}</span></span>
</div>
computed: { positives() { //對應全部正向評價的數組
return this.ratings.filter((rating) => { return rating.rateType === POSITIVE; }); }, negatives() { return this.ratings.filter((rating) => { return rating.rateType === NEGATIVE; }); } }
評價列表
<li v-show="needShow(rating.rateType, rating.text)" //v-show特殊用法:綁定函數返回值
v-for="rating in food.ratings"
:key="rating.id"
class="rating-item border-1px">
needShow(type, text) { // console.log('this.selectType: ' + this.selectType + ' type: ' + type + ' out ' + text); if (this.onlyContent && !text) { return false; } if (this.selectType === ALL) { return true; } else { //console.log('this.selectType: ' + this.selectType + 'type: ' + type + ' in ' + text); return type === this.selectType; } }
incrementTotal(type, data) { // 對子組件更改的數值進行監聽
this[type] = data; this.$nextTick(() => { // 當咱們改變數據的時候,DOM的更新是異步的
this.scroll.refresh(); }); }
<div class="time">{{rating.rateTime | formatDate}}</div> import {formatDate} from 'common/js/date.js';
export function formatDate(date, fmt) { if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)); } let o = { 'M+': date.getMonth() + 1, 'd+': date.getDate(), 'h+': date.getHours(), 'm+': date.getMinutes(), 's+': date.getSeconds() }; for (let k in o) { if (new RegExp(`(${k})`).test(fmt)) { let str = o[k] + ''; fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str)); } } return fmt; } function padLeftZero(str) { return ('00' + str).substr(str.length); }
filters: { formatDate(time) { let date = new Date(time); return formatDate(date, 'yyyy-MM-dd hh:mm'); } }
注:項目來自慕課網