最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

怎样使用vue+jquery+lodash实现滑动时顶部悬浮

来源:动视网 责编:小采 时间:2020-11-27 19:42:36
文档

怎样使用vue+jquery+lodash实现滑动时顶部悬浮

怎样使用vue+jquery+lodash实现滑动时顶部悬浮:这次给大家带来怎样使用vue+jquery+lodash实现滑动时顶部悬浮,使用vue+jquery+lodash实现滑动时顶部悬浮的注意事项有哪些,下面就是实战案例,一起来看一下。这个效果是一个项目中抽出来的一个demo效果。前期准备:1. 引入jQ<script sr
推荐度:
导读怎样使用vue+jquery+lodash实现滑动时顶部悬浮:这次给大家带来怎样使用vue+jquery+lodash实现滑动时顶部悬浮,使用vue+jquery+lodash实现滑动时顶部悬浮的注意事项有哪些,下面就是实战案例,一起来看一下。这个效果是一个项目中抽出来的一个demo效果。前期准备:1. 引入jQ<script sr
 这次给大家带来怎样使用vue+jquery+lodash实现滑动时顶部悬浮,使用vue+jquery+lodash实现滑动时顶部悬浮的注意事项有哪些,下面就是实战案例,一起来看一下。

这个效果是一个项目中抽出来的一个demo效果。

前期准备:

1. 引入jQ

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

引入lodash.js

npm install lodash -D

fixTop.vue组件的

<template>
 <p class="fixtop2">
 <header class="header" ref="header"></header>
 <p class="nav" ref="nav" :class="{isFixed:isFixed}">
 <p class="box" v-for="(item,index) in list" :key="index">
 {{item.title}}
 </p>
 </p>
 <ul class="content">
 <li v-for="(item,index) in new Array(20)" :key="index">{{index+1}}</li>
 </ul>
 </p>
</template>
<script>
var throttle = require('lodash/throttle'); //从lodash中引入的throttle节流函数
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, //定义一个截流函数的变量
 };
 },
 methods: {
 //滚动的函数
 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(() => {
 //这里使用监听的scroll的事件,为什么要使用的节流函数,如果不使用的,页面一直在滚动计算的,这样在
 //使用手机时候,出现非常卡的,隔一段时间计算,大大降低了性能的消耗(具体的好处自己去查资料)
 window.addEventListener('scroll', this.throttleScroll, false);
 });
 this.throttleScroll = throttle(this.handleScroll, 100);
 },
 deactivated() {
 //离开页面需要remove这个,不然还是卡到爆。
 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>

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读

如何操作jQuery实现电子时钟效果

怎样操作Angular5路由传参

文档

怎样使用vue+jquery+lodash实现滑动时顶部悬浮

怎样使用vue+jquery+lodash实现滑动时顶部悬浮:这次给大家带来怎样使用vue+jquery+lodash实现滑动时顶部悬浮,使用vue+jquery+lodash实现滑动时顶部悬浮的注意事项有哪些,下面就是实战案例,一起来看一下。这个效果是一个项目中抽出来的一个demo效果。前期准备:1. 引入jQ<script sr
推荐度:
标签: VUE 实现 使用jquery
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top