最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue+jquery+lodash實(shí)現(xiàn)滑動(dòng)時(shí)頂部懸浮固定效果

 更新時(shí)間:2018年04月28日 11:54:28   作者:zhooson  
這篇文章主要為大家詳細(xì)介紹了vue+jquery+lodash實(shí)現(xiàn)滑動(dòng)時(shí)頂部懸浮固定效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了vue實(shí)現(xiàn)滑動(dòng)時(shí)頂部懸浮固定效果的具體代碼,供大家參考,具體內(nèi)容如下

這個(gè)效果是一個(gè)項(xiàng)目中抽出來(lái)的一個(gè)demo效果。

前期準(zhǔn)備:

1. 引入jQ

<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>

引入lodash.js

npm install lodash -D

fixTop.vue組件的

<template>
 <div class="fixtop2">

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

  <div class="nav" ref="nav" :class="{isFixed:isFixed}">
   <div class="box" v-for="(item,index) in list" :key="index">
    {{item.title}}
   </div>
  </div>

  <ul class="content">
   <li v-for="(item,index) in new Array(20)" :key="index">{{index+1}}</li>
  </ul>

 </div>
</template>
<script>
var throttle = require('lodash/throttle'); //從lodash中引入的throttle節(jié)流函數(shù)
export default {
 name: 'navScroll2',
 data() {
  return {
   list: [
    { title: 'AAAA', id: 1 },
    { title: 'BBBB', id: 2 },
    { title: 'CCCC', id: 3 },
    { title: 'DDDD', id: 4 },
   ],
   isFixed: false, //是否固定的
   throttleScroll: null, //定義一個(gè)截流函數(shù)的變量
  };
 },
 methods: {
  //滾動(dòng)的函數(shù)
  handleScroll() {
   let h = $(this.$refs.header).outerHeight(); //header的高度
   let wh = $(window).scrollTop(); //滾動(dòng)的距離的,為什么這里使用的jq,因?yàn)椴挥每紤]的什么的兼容問(wèn)題
   let navH = $(this.$refs.nav).outerHeight(); //nav的高度

   if (wh > h) {
    this.isFixed = true;
   } else {
    this.isFixed = false;
   }
  },
 },

 mounted() {
  //寫(xiě)在掉接口的里面的
  this.$nextTick(() => {
   //這里使用監(jiān)聽(tīng)的scroll的事件,為什么要使用的節(jié)流函數(shù),如果不使用的,頁(yè)面一直在滾動(dòng)計(jì)算的,這樣在
   //使用手機(jī)時(shí)候,出現(xiàn)非常卡的,隔一段時(shí)間計(jì)算,大大降低了性能的消耗(具體的好處自己去查資料)
   window.addEventListener('scroll', this.throttleScroll, false);
  });

  this.throttleScroll = throttle(this.handleScroll, 100);
 },
 deactivated() {
  //離開(kāi)頁(yè)面需要remove這個(gè)監(jiān)聽(tīng)器,不然還是卡到爆。
  window.removeEventListener('scroll', this.throttleScroll);
 },
};
</script>
<style lang="scss" scoped>
.fixtop2 {
 min-height: 100vh;
}

.header {
 height: 5rem;
 width: 100%;
 background-color: red;
}

.nav {
 display: flex;
 width: 100%;
 background-color: pink;
 &.isFixed {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 9999;
 }
 .box {
  font-size: 0.3rem;
  padding: 0 0.3rem;
  height: 0.9rem;
  line-height: 0.9rem;
  color: #333333;
  flex: 1;
 }
}

.content {
 height: 20rem;
 li {
  width: 100%;
  height: 1rem;
  border-bottom: 1px solid #000;
 }
}
</style>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

平阳县| 罗山县| 林甸县| 双鸭山市| 阳东县| 济宁市| 康平县| 嘉兴市| 常宁市| 玛纳斯县| 奇台县| 海宁市| 肥西县| 内丘县| 德保县| 托里县| 永清县| 门源| 张家港市| 砚山县| 卓资县| 南康市| 宿迁市| 鄂伦春自治旗| 巴林左旗| 乌审旗| 罗山县| 区。| 郁南县| 息烽县| 门源| 平南县| 北票市| 称多县| 瑞昌市| 安泽县| 应城市| 忻州市| 马关县| 巴青县| 海林市|