VUE進(jìn)階筆記(5) - ( better-scroll ) + ( 父組件調(diào)用子組件的方法函數(shù) ) + ( click.stop阻止冒泡瑞佩,click.prevent阻止默認(rèn)事件 ) + ( 1px像素邊框 ) + ( 過渡聚磺,動畫 ) + ( 顯示省略號。炬丸。瘫寝。) + ( 背景模糊 filter ) + ( display:table多行垂直居中布局 ) +(陰影box-shadow,filter: drop-shadow )

(1) better-scroll插件

官網(wǎng):https://ustbhuangyi.github.io/better-scroll/#/examples
詳細(xì):https://zhuanlan.zhihu.com/p/27407024

  • 安裝:
cnpm install better-scroll --save
  • 使用
js部分


<script type="text/ecmascript-6">
  import BScroll from 'better-scroll';                   // 引入better-scroll
    created() {                              // 生命周期鉤子函數(shù)稠炬,用在實(shí)例被創(chuàng)建之后
      this.$http.get('/api/goods')       // 異步請求數(shù)據(jù)
        .then((response) => {
          response = response.body;
          if (response.errno === ERR_OK) {
            this.goods = response.data;
            this.$nextTick(() => {       // this.$nextTick保證在更新dom之后焕阿,再執(zhí)行initscroll()函數(shù)
              this.initScroll();
            });
          }
        });
    },
    methods: {
        initScroll() {                                // 定義一個(初始化scroll)的函數(shù)
            this.menuScroll = new BScroll(this.$refs.menuWrapper, { click: true });   
                 // 拿到dom,實(shí)例化BScroll首启,click:true則是允許點(diǎn)擊事件
            this.contentScroll = new BScroll(this.$refs.goodsWrapper, { click: true});  
        }
    }
  };
</script>
----------------------------------------------------------------------------
vue的DOM部分


<template>

  <div class="goods">

    <div class="menu-wrapper" ref="menuWrapper">               // ref屬性:綁定dom
      <ul>
        <li v-for=" item in goods " class="li">
        </li>
      </ul>
    </div>

    <div class="goods-wrapper" ref="goodsWrapper">             // ref屬性:綁定dom
      <ul>
        <li v-for=" item in goods" class="foods-Wrapper">
          <h1 class="foods-section"> {{ item.name }} </h1>
          <ul>
            <li v-for=" foods in item.foods " class=" foods-content ">
            </li>
          </ul>
        </li>
      </ul>
    </div>

  </div>
</template>


  • 使用進(jìn)階
    當(dāng)better-scroll處在時刻變化的場景中的時候( 比如:在顯示隱藏一個modal的時候暮屡,每次狀態(tài)的變化都需要new BScroll? 不需要毅桃,better-scroll中有 refresh() 接口 )
//初始化函數(shù)


initBetterScroll() {      
            if (!this.modalScroll) {
              this.modalScroll = new BScroll(this.$refs.modalDom, { click: true });
            } else {
                this.modalScroll.refresh();      // 不存在就new褒纲,已經(jīng)存在就refresh()
            }
        }

---------------------------------------------------------------

//調(diào)用


  showDetail() {
            if (!this.selectTotalCount) {    // 當(dāng)數(shù)量為0的時候,不顯示
                return;
            } else {
              this.showModal = !this.showModal;      // 切換顯示狀態(tài)
            } if (this.showModal) {      // 顯示的時候钥飞,調(diào)用better-scroll
              this.$nextTick(() => {     //dom更新后外厂,執(zhí)行
                this.initBetterScroll();
              });
          }
        }

(2) 父組件調(diào)用子組件的方法

  • (1) ref屬性
    ref 綁定在一般元素上,ref指 ( DOM元素 )
    ref 綁定在組件上代承,ref指 ( 組件實(shí)例 )

  • (2) 例子說明

子組件的函數(shù)


methods: {
        show() {
            this.foodsDetail = !this.foodsDetail;
        }
    }

------------------------------------------------------------------

父組件調(diào)用子組件的函數(shù)

dom

<li v-for=" foods in item.foods " class=" foods-content " v-on:click="selectFoodsFunction(foods)">

 // 點(diǎn)擊汁蝶,執(zhí)行 selectFoodsFunction函數(shù),并把當(dāng)前的 foods 作為參數(shù)傳入



<Foods v-bind:foods="selectedFoodsObject" ref="foodsChildrenComponent"></Foods>

 // ref綁定子組件實(shí)例調(diào)用子組件方法 论悴, 將selectedFoodsObject對象 傳給子組件



 selectFoodsFunction(foods) {
        this.selectedFoodsObject = foods;   // 將foods對象保存在data的selectedFoodsObject 中
        this.$refs.foodsChildrenComponent.show();    //調(diào)用子組件的show()函數(shù)
      }

(3) click.stop阻止冒泡掖棉,click.prevent阻止默認(rèn)事件



<div class="cart-add icon-add_circle" v-on:click.stop.prevent="addCart($event)"></div>

(4) @media 媒體查詢

<meta 
name="viewport" 
content="width=device-width, 
initial-scale=1.0, 
maximum-scale=1.0, 
minimum-scale=1.0, 
user-scalable= no"
/>


@media screen and (max-width: 300px) {
    body {
        background-color:lightblue;
    }
}

// 如果文檔寬度小于 300 像素則修改背景顏色(background-color):
// ( min-width: 300px) 瀏覽器寬度大于300px時

(5) 1px像素邊框

html


  <div class="suo">
      定義偽類,縮放class
    </div>

---------------------------------------------------------------------------------------------

stylus


.suo
      position: relative
      &:after
        display: block
        position: absolute
        left: 0
        bottom: 0
        width: 100%
        border-bottom: 1px solid red
        content: ' '
@media (-webkit-min-device-pixel-ratio: 1.5),(min-device-pixel-ratio: 1.5)
      .suo:after
          -webkit-transform: scaleY(0.7)
          transform: scaleY(0.7)
@media (-webkit-min-device-pixel-ratio: 2),(min-device-pixel-ratio: 2)
      .suo:after
          -webkit-transform: scaleY(0.5)       // ratio: 比例
          transform: scaleY(0.5)

(6) 省略號膀估。幔亥。。

(1) white-space:nowrap ( 規(guī)定段落中的文本不進(jìn)行換行 )
(2) overflow: hidden ( 內(nèi)容會被修剪察纯,并且其余內(nèi)容是不可見的 )
(3) text-overflow: ellipsis ( 顯示省略符號來代表被修剪的文本 )


 .right
    white-space: nowrap
    overflow: hidden
    text-overflow: ellipsis    //ellipsis是省略的意思

(7) filter: blur(10px) 模糊效果

(filter詳解)http://www.cnblogs.com/moqiutao/p/4843437.html

 filter: blur(10px);
 overflow: hidden;      //一般會讓超出的模糊部分隱藏

(8) filter: drop-shadow() 陰影效果


filter: drop-shadow(x偏移, y偏移, 模糊大小, 色值);

(9) box-shadow: 盒陰影效果


box-shadow: x偏移, y偏移, 模糊大小, 色值

兩者區(qū)別:http://www.zhangxinxu.com/wordpress/2016/05/css3-filter-drop-shadow-vs-box-shadow/

(10) VUE中 css3 過渡 和 動畫

動畫:

vue部分

 <transition name="aa">
      <div class="modal" v-show="showModal">
        <div v-on:click="show()" class="close">關(guān)閉</div>
      </div>
 </transition>

-----------------------------------

css部分

  .aa-enter-active {
    animation: bounce-in .4s;
  }
  .aa-leave-active {
    animation: bounce-in .4s reverse;    // reverse反轉(zhuǎn)的意思
  }
  @keyframes bounce-in {
    0% {
      transform: translateX(-100px);
    }
    100% {
      transform: translateX(0);
    }
  }

(11) flex固定寬度

flex: 0 0 80px;
width: 80px;

(12) display:table 多行垂直居中布局

.li
        display: table
        background:red
        border-bottom: 1px solid red
        line-height:14px;
        height:54px;
        width:56px;
        padding: 0 12px
        .table-cell
          display: table-cell
          background:yellow
          vertical-align: middle

(13) 用數(shù)組控制不同的class的顯示


template

 <li v-for=" item in goods " class="li">
      <div v-bind:class="classMap[ item.type ]" class="ii"></div>
      <span class="table-cell">{{ item.name }}</span>
 </li>
------------------------------------------------------------------------------------------
script

    created() {
      this.classMap = ['jian', 'jia', 'cheng', 'chu', 'mo'];

     //這里也可以在data(){}中直接初始化classMap
     //data() {
     // return {
     //  goods: [],
     //  classMap: ['jian', 'jia', 'cheng', 'chu', 'mo']
     //   };
     //    }

      this.$http.get('/api/goods')
        .then((response) => {
          response = response.body;
          if (response.errno === ERR_OK) {
            this.goods = response.data;
          }
        });
    }
--------------------------------------------------------------------------------------------
css

        .jian
          background:blue
        .jia
          background:blueviolet
        .cheng
          background:black

(14) 在vue中如果props是一個 (數(shù)組Array)或者一個(對象Object)帕棉,那么default就是一個函數(shù)

cart組件中js部分



export default {
      props: {
        sellerCart: {          // <Cart v-bind:sellerCart="seller"></Cart> 父組件傳過來的對象
            type: Object,
            default() {       // props是對象,所以default是一個函數(shù)
                return {};
            }
        },
        selectFoods: {
            type: Array,    
            default() {    // props是數(shù)組饼记,所以default是一個函數(shù)
                return [
                  {
                      price: 10,     // 在selectFoods傳過來的不存在時香伴,返回defaut中值
                      count: 20
                  }
                ];
            }
        }
      },
      computed: {                               // 計算屬性
          selectTotalPrice() {
              let total = 0;
              this.selectFoods.forEach((foods) => {     // 遍歷selectFoods
                total += foods.price * foods.count;
              });
              return total;
          },
          selectTotalCount() {
              let count = 0;
              this.selectFoods.forEach((foods) => {
                  count += foods.count;
              });
              return count;
          },
           stateRight() {            //結(jié)算的三種狀態(tài)
              if (this.selectTotalPrice === 0) {
                  return `${this.sellerCart.minPrice}元起送`;     // `${...}` es6帶變量的字符串寫法
              } else if (this.selectTotalPrice < this.sellerCart.minPrice) {
                  let diff = this.sellerCart.minPrice - this.selectTotalPrice;
                  return `還差 ${diff}元起送`;
              } else {
                  return '去結(jié)算';
              }
          }
      }

  };
-----------------------------------------------------------------------------------

cart的vue的dom部分



<template>
  <div class="Cart">
    <div class="Cart-container">

      <div class="left">

        <div class="circle">
          <div class="circle-in" v-bind:class="{ 'selectTotalCount': selectTotalCount>0}">
            <i class="fa fa-credit-card" aria-hidden="true"></i>
            <div class="foodsCountNum">
              {{ selectTotalCount }}
            </div>
          </div>
        </div>
        <div class="price" v-bind:class="{ 'priceHighlight': selectTotalPrice>0 }">
          {{ selectTotalPrice }}
        </div>

        <div class="otherPrice">
          另需配送費(fèi){{ sellerCart.deliveryPrice }}元
        </div>

      </div>

      <div class="right" v-bind:class="{ 'go': (selectTotalPrice - sellerCart.minPrice ) > 0 }" >

            //上面要注意,在template(或者h(yuǎn)tml)中表達(dá)式都不能帶this具则,如(this.selectTotalPrice )

            <div class="right-text">{{ stateRight }}</div>

      </div>

    </div>
  </div>
</template>

(15) 手機(jī)即纲,瀏覽器下點(diǎn)擊觸發(fā)兩次解決方案:

js部分

 methods: {
          add(event) {
              if (!event._constructed) {
                 return;
              }
              console.log('add');
           }
         }
--------------------------------------------------------

html部分

 <div class="same" v-on:click.stop.prevent="add($event)">+</div>
 
//這里要加上add($event)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市博肋,隨后出現(xiàn)的幾起案子低斋,更是在濱河造成了極大的恐慌蜂厅,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件膊畴,死亡現(xiàn)場離奇詭異掘猿,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)唇跨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進(jìn)店門稠通,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人轻绞,你說我怎么就攤上這事采记。” “怎么了政勃?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵唧龄,是天一觀的道長。 經(jīng)常有香客問我奸远,道長既棺,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任懒叛,我火速辦了婚禮丸冕,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘薛窥。我一直安慰自己胖烛,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布诅迷。 她就那樣靜靜地躺著佩番,像睡著了一般。 火紅的嫁衣襯著肌膚如雪罢杉。 梳的紋絲不亂的頭發(fā)上趟畏,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天,我揣著相機(jī)與錄音滩租,去河邊找鬼赋秀。 笑死,一個胖子當(dāng)著我的面吹牛律想,可吹牛的內(nèi)容都是我干的猎莲。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼蜘欲,長吁一口氣:“原來是場噩夢啊……” “哼益眉!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起姥份,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤郭脂,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后澈歉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體展鸡,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年埃难,在試婚紗的時候發(fā)現(xiàn)自己被綠了莹弊。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡涡尘,死狀恐怖忍弛,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情考抄,我是刑警寧澤细疚,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站川梅,受9級特大地震影響疯兼,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜贫途,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一吧彪、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧丢早,春花似錦姨裸、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至凫碌,卻和暖如春扑毡,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背盛险。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工瞄摊, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓满败,卻偏偏與公主長得像数初,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子惯驼,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評論 2 355

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