商品列表布局

左側(cè)寬度固定绪杏,右邊自適應(yīng)
flex: 0 0 80px
第一個(gè)是等分,第二個(gè)是縮放雕憔,第三個(gè)是占位空間

<div class="goods">
  <div class="menu-wrapper"></div>
  <div class="foods-wrapper"></div>
</div>
--------------------css------------------------
.goods
  display: flex
  position: absolute
  top: 174px
  bottom: 46px
  width: 100%
  .menu-wrapper
    flex: 0 0 80px
    width: 80px
    background: #ccc
   .foods-wrapper
      flex: 1
--------------------js獲取數(shù)據(jù)------------------
const ERR_OK = 0
export default {
  props: {
    seller: {
      type: Object
    }
  },
  data() {
    return {
      goods: []
    }
  },
  created: {
    this.$http.get('/api/goods').then((res) => {
      res = res.body
      if(res.errno === ERR_OK){
        this.goods = res.data
    }
  })
  }
}

左側(cè)布局

可能有多行垂直居中萧吠,適合用display:table

<ul>
<li class="menu-item" v-for="item in goods" >
  <span class="text border-1px">
    <span v-show="item.type>0" class="icon" :class="classMap[item.type]"></span>
    {{ item. name}}
  </span>
</li>
</ul>
-------------js----------------------------------------------
export default {
  created:() {
    this.classMap = ['decrease','discount','special','invoice','guarantee'];
  }
}
-------------css----------------------------------------------
bg-image($url)
    background-image: url($url + "@2x.png")
    @media (-webkit-min-device-pixel-ratio: 3),(min-device-pixel-ratio: 3)
        background-image: url($url + "@3x.png")
            .menu-item
                display: table
                width: 56px
                height: 54px
                padding: 0 12px
                line-height: 14px
                .icon
                    display: inline-block
                    vertical-align: top
                    width: 12px
                    height: 12px
                    margin-right: 2px
                    background-size: 12px 12px
                    background-repeat: no-repeat
                    &.decrease
                        bg-image('decrease_3')
                    &.discount
                        bg-image('discount_3')
                    &.guarantee
                        bg-image('guarantee_3')
                    &.invoice
                        bg-image('invoice_3')
                    &.special
                        bg-image('special_3')
                .text
                    display: table-cell
                    width: 56px
                    vertical-align: middle
                    border-1px(rgba(7,17,27,0.2)) 
                    font-size: 12px

右側(cè)布局

       <div class="foods-wrapper">
           <ul>
               <li v-for="item in goods" class="food-list">
                   <h1 class="title">{{ item.name }}</h1>
                   <ul>
                       <li v-for="food in item.foods" class="food-item">
                           <div class="icon">
                               <img :src="food.icon">
                           </div>
                           <div class="content">
                               <h2 class="name">{{ food.name}}</h2>
                               <p class="desc">{{food.description}}</p>
                               <div class="extra">
                                   <span>月售{{ food.sellCount }}份</span>
                                   <span>好評(píng)率{{ food.rating }}%</span>
                               </div>
                               <div class="price">
                                   <span>¥{{ food.price }}</span>
                                   <span v-show="food.oldPrice">¥{{ food.oldPrice }}</span>
                               </div>
                           </div>
                       </li>
                   </ul>
               </li>
           </ul>
       </div>
--------------------------css-------------------------------

   .foods-wrapper
           flex: 1
           .title
               padding-left: 14px
               height: 26px
               line-height: 26px
               border-left: 2px solid #d9dde1
               font-size: 12px
               color: rgb(147,153,159)
               background: #f3f5f7
           .food-item
               display: flex
               margin: 18px
               padding-bottom: 18px
               border-1px(rgba(7,17,27,0.1))
               &.last-child
                   border-none()
                   margin-bottom: 0
               .icon
                   flex: 0 0 57px
                   margin-right: 10px
               .content
                   flex: 1
                   .name
                       margin: 2px 0 8px 0
                       height: 14px
                       line-height: 14px
                       font-size: 14px
                       color: rgb(7,17,27)
                   .desc, .extra
                       line-height: 10px
                       font-size: 10px
                       color: rgb(147,153,159)
                   .desc
                       margin-bottom: 8px
                   .extra
                       &.count
                           margin-right: 12px
                   .price
                       font-weight: 700
                       line-height: 24px
                       .now
                           margin-right: 8px
                           font-size: 14px
                           color: rgb(240,20,20)
                       .old
                           text-decoration: line-through
                           font-size: 10px
                           color: rgb(147,153,159)

怎么把1像素的邊去掉及老?
在mixin.styl中寫(xiě):

border-none()
    &:after
        display: none

better-scroll應(yīng)用

  • 安裝
    cnpm install better-scroll --save
  • 使用
    import BScroll from 'better-scroll'
    vue取dom
    <div class="menu-wrapper" ref="menuWrapper"></div>
      created() {
            this.classMap = ['decrease','discount','special','invoice','guarantee'];
            this.$http.get('/api/goods').then((res) => {
                res = res.body
                if(res.errno === ERR_OK){
                    this.goods = res.data
                    this.$nextTick(() => {
//this.$nextTick() => 在下次 DOM 更新循環(huán)結(jié)束之后執(zhí)行延遲回調(diào)。在修改數(shù)據(jù)之后立即使用這個(gè)方法范抓,獲取更新后的 DOM骄恶。
//數(shù)據(jù)發(fā)生變化后,不能直接更新在dom上匕垫,需要在回調(diào)函數(shù)中刷新DOM,即異步加載DOM
                        this._initScroll()
                    })
                    
                }
            })
        },
        methods: {
            _initScroll() {
                //this.$refs:取得DOM對(duì)象
                this.menuScroll = new BScroll(this.$refs.menuWrapper, {})
                this.foodsScroll = new BScroll(this.$refs.foodsWrapper, {})

            }
        }

如果報(bào)錯(cuò)
Error in nextTick: "TypeError: Cannot read property 'children' of undefined"
那么就是因?yàn)?br> <div class="menu-wrapper" ref="menuWrapper"></div>
menuWrapper要用駝峰寫(xiě),不要用-

實(shí)現(xiàn)左右聯(lián)動(dòng)

右邊縱坐標(biāo)落到哪個(gè) 區(qū)間僧鲁,計(jì)算每個(gè)區(qū)間的高度
然后返回右邊的索引 與 左邊的索引相等時(shí)栅表,給左側(cè)Li高亮

在`$nextTick`拿到數(shù)據(jù)钾麸,dom更新后
data() {
  return {
    listHeight: [],
    scrollY: 0
 }
},
computed: {
  currentIndex() {
    for(let i = 0; i < listHeight.length; i++){
      let height1 = listHeight[i];
      let height2 = listHeight[i + 1]
      if(!height2 || this.scrollY >= height1 && this.scrollY < height2){
        return i
      }
     }
    return 0
}
},
created: {
this.$nextTick(function(){
  this._initScroll();
  this._calculateHeight();
})
},
methods: {
  _initScroll() {
     this.menuScroll = new BScroll(this.$refs.menuWrapper, {
      click: true //取消默認(rèn)阻止事件    
     });
     this.foodsScroll = new BScroll(this.$refs.foodsWrapper, {
      click: true,
      probeType: 3 //監(jiān)聽(tīng)事件的觸發(fā)時(shí)間,1為即時(shí)觸發(fā)踱葛,3為延遲觸發(fā)
    });
     this.foodsScroll.on('scroll', () => {
        this.scrollY = Math.abs(Math.round(pos.y))
     })
},
  _calculateHeight(){
    var foodList = this.$refs.foodsWrapper.getElementsByClassName('food-list-hook')
    let height = 0
    for(var i = 0; i < foodList.length; i++){
      let item = foodList[i]//獲取每個(gè)li的高度
      height += item.clientHeight;
      this.listHeight.push(height);
}
}
}

把右邊當(dāng)前的索引關(guān)聯(lián)到左側(cè)導(dǎo)航的索引
<li v-for="(item,index) in goods" class="menu-item" :class="{'current':currentIndex === index}"></li>
  • 點(diǎn)擊左側(cè)菜單 實(shí)現(xiàn)右側(cè)聯(lián)動(dòng)
index:當(dāng)前索引值
$event://當(dāng)pc瀏覽器會(huì)觸發(fā)兩次偶惠,解決方法傳入event
<li v-for="(item,index) in goods" class="menu-item" :class="{'current':currentIndex === index}" @click="selectMenu(index,$event)"></li>

methods: {
  selectMenu(index,event) {
    if(!event._constructed){//阻止非vue事件
      return
    }
    var foodList = this.$refs.foodsWrapper.getElementsByClassName('food-list-hook');
    let el = foodList[index]//獲取右側(cè)當(dāng)前位置
    this.foodsScroll.scrollToElement(el, 300)//右側(cè)內(nèi)容指向右側(cè)當(dāng)前位置
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蜕该,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子洲鸠,更是在濱河造成了極大的恐慌,老刑警劉巖馋缅,帶你破解...
    沈念sama閱讀 217,734評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件扒腕,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡萤悴,警方通過(guò)查閱死者的電腦和手機(jī)瘾腰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)覆履,“玉大人蹋盆,你說(shuō)我怎么就攤上這事∠跞” “怎么了栖雾?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,133評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)伟众。 經(jīng)常有香客問(wèn)我析藕,道長(zhǎng),這世上最難降的妖魔是什么凳厢? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,532評(píng)論 1 293
  • 正文 為了忘掉前任账胧,我火速辦了婚禮,結(jié)果婚禮上先紫,老公的妹妹穿的比我還像新娘治泥。我一直安慰自己,他們只是感情好遮精,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,585評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布居夹。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪吮播。 梳的紋絲不亂的頭發(fā)上变屁,一...
    開(kāi)封第一講書(shū)人閱讀 51,462評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音意狠,去河邊找鬼粟关。 笑死,一個(gè)胖子當(dāng)著我的面吹牛环戈,可吹牛的內(nèi)容都是我干的闷板。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼院塞,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼遮晚!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起拦止,我...
    開(kāi)封第一講書(shū)人閱讀 39,153評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤县遣,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后汹族,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體萧求,經(jīng)...
    沈念sama閱讀 45,587評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,792評(píng)論 3 336
  • 正文 我和宋清朗相戀三年顶瞒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了夸政。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,919評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡榴徐,死狀恐怖守问,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情坑资,我是刑警寧澤耗帕,帶...
    沈念sama閱讀 35,635評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站袱贮,受9級(jí)特大地震影響兴垦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜字柠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,237評(píng)論 3 329
  • 文/蒙蒙 一探越、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧窑业,春花似錦钦幔、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,855評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)搀擂。三九已至,卻和暖如春卷玉,著一層夾襖步出監(jiān)牢的瞬間哨颂,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,983評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工相种, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留威恼,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,048評(píng)論 3 370
  • 正文 我出身青樓寝并,卻偏偏與公主長(zhǎng)得像箫措,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子衬潦,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,864評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 常見(jiàn)的頁(yè)面布局 作為一個(gè)像我這樣的切圖仔前端而言斤蔓,拿到設(shè)計(jì)圖的第一步就是要分析設(shè)計(jì)圖大致地劃分區(qū)域,然后選擇一種最...
    自度君閱讀 1,150評(píng)論 0 1
  • 現(xiàn)在镀岛,對(duì)于微信公眾號(hào)弦牡,大家都不陌生。誰(shuí)微信沒(méi)關(guān)注幾個(gè)自己感興趣的公眾號(hào)呢漂羊。隨著微信公眾號(hào)的普及喇伯,不再是13,14年...
    8976d908eb13閱讀 5,699評(píng)論 0 1
  • 由于小時(shí)候我家住在一條大馬路旁邊,又靠近一個(gè)集市拨与,我每天早上都會(huì)看到從灰蒙蒙的晨霧中從四面八方涌來(lái)的人群,人們提著...
    南城半世閱讀 759評(píng)論 3 7
  • 初萌的日光 自微翹起的檐角 緩緩淌下 秋風(fēng)的落葉 泥靜的小路 帶走了依依不舍的嬉戲 母親遠(yuǎn)處呼喊的聲音 院里說(shuō)故事...
    小劉少爺閱讀 197評(píng)論 0 4