vue+jquery+lodash如何實現(xiàn)滑動時頂部懸浮固定效果

這篇文章給大家分享的是有關(guān)vue+jquery+lodash如何實現(xiàn)滑動時頂部懸浮固定效果的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)建站是一家專業(yè)提供華容企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設(shè)、H5開發(fā)、小程序制作等業(yè)務(wù)。10年已為華容眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進行中。

這個效果是一個項目中抽出來的一個demo效果。

vue+jquery+lodash如何實現(xià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, //定義一個截流函數(shù)的變量
  };
 },
 methods: {
  //滾動的函數(shù)
  handleScroll() {
   let h = $(this.$refs.header).outerHeight(); //header的高度
   let wh = $(window).scrollTop(); //滾動的距離的,為什么這里使用的jq,因為不用考慮的什么的兼容問題
   let navH = $(this.$refs.nav).outerHeight(); //nav的高度

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

 mounted() {
  //寫在掉接口的里面的
  this.$nextTick(() => {
   //這里使用監(jiān)聽的scroll的事件,為什么要使用的節(jié)流函數(shù),如果不使用的,頁面一直在滾動計算的,這樣在
   //使用手機時候,出現(xiàn)非??ǖ模粢欢螘r間計算,大大降低了性能的消耗(具體的好處自己去查資料)
   window.addEventListener('scroll', this.throttleScroll, false);
  });

  this.throttleScroll = throttle(this.handleScroll, 100);
 },
 deactivated() {
  //離開頁面需要remove這個監(jiān)聽器,不然還是卡到爆。
  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>

感謝各位的閱讀!關(guān)于“vue+jquery+lodash如何實現(xiàn)滑動時頂部懸浮固定效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

當前名稱:vue+jquery+lodash如何實現(xiàn)滑動時頂部懸浮固定效果
網(wǎng)頁URL:http://muchs.cn/article32/ghjosc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、營銷型網(wǎng)站建設(shè)、微信公眾號搜索引擎優(yōu)化、域名注冊、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)