美文网首页
Vue 置顶组件FixedTopWrap 支持自定义内容置顶

Vue 置顶组件FixedTopWrap 支持自定义内容置顶

作者: Miwi锐霸 | 来源:发表于2018-11-30 14:16 被阅读0次

背景

最近项目在实现置顶功能时,由于原使用的双标签替换实现存在状态不一致的问题,所以考虑仅用单标签实现,避免状态问题。同时由于多处使用,所以简单实现了这一组件。使用上只要像<div>一样包裹需要被置顶的内容即可。

效果

image

使用示例

<template>
  <div>
    <fixed-top-wrap>
        <div>置顶内容</div>
    </fixed-top-wrap>
     ...
  </div>
</template>

<script>
import FixedTopWrap from '@/components/FixedTopWrap';

export default {
  data() {
    return {};
  },
  components: {
    FixedTopWrap,
  }
};
</script>

<style scoped>
</style>

源码

<template lang="html">
  <!--置顶条目父布局-->
  <div id="fix-scroll-watch" ref="fixScrollWatch" class="fixScrollWatch" :style="fixStyle"> <!--用于监听滚动位置-->
    <div ref="topFixBarFixed" :class="topFixBarFixed ? 'topFixBarFixed' : ''" class="fix-index"> <!--用于固定位置-->
      <slot/> <!--实际内容-->
    </div>
  </div>
</template>

<script>
/**
  * 〖Author〗 MiWi.LIN ^^^^〖E-mail〗 80383585@qq.com
  * ======================================================== Copyright(c) 2018/11/23 ==
  * 〖Version〗 1.0 <BR/>
  * 〖Date〗 2018/11/23_下午4:31 <BR/>
  * 〖Desc〗 自动固顶 <BR/>
  * 〖Modify By〗 <BR/>
  */
export default {
  data() {
    return {
      topFixBarFixed: false,
      isMounted: false,
    };
  },
  computed: {
    fixStyle() {
      if (this.isMounted) {
        const h = this.$refs.topFixBarFixed.offsetHeight;
        return { height: `${h}px` };
      }
      return {};
    },
  },
  methods: {
    handleScroll() {
      const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
      // const fix = document.querySelector('#fix-scroll-watch');
      const fix = this.$refs.fixScrollWatch;
      const offsetTop = fix ? fix.offsetTop : 0;
      this.topFixBarFixed = scrollTop > offsetTop;
    },
  },
  mounted() {
    this.isMounted = true;
    window.addEventListener('scroll', this.handleScroll);
  },
  destroyed() {
    window.removeEventListener('scroll', this.handleScroll);
  },
};
</script>

<style scoped lang="postcss">
  .fixScrollWatch {
    & .fix-index {
        z-index: 99;
        background-color: white;
      }
    & .topFixBarFixed {
      width: 100%;
      position: fixed;
      top: 0;
    }
  }
</style>

没啥新东西,主要还是监听window scroll事件和slot使用。
写此文时,搜索置顶组件,很意外看到vue滑动页面菜单栏置顶,了解到position: sticky这一style也能轻易实现,但有兼容问题,有兴趣的可以了解下position sticky的兼容

预告

小程序版无限滚动日期Tab组件:
GitHub

image

相关文章

  • Vue 置顶组件FixedTopWrap 支持自定义内容置顶

    背景 最近项目在实现置顶功能时,由于原使用的双标签替换实现存在状态不一致的问题,所以考虑仅用单标签实现,避免状态问...

  • 开启微博认知之路 006

    微博置顶内容撰写 1-九图置顶 2-头条文章置顶 3-视频置顶 微博置顶注意事项 1-微信2-二维码3-不要太强的...

  • 不常见bug记录

    1.vue 组件名称 和引入的组件名称重复 会导致循环渲染 超出内存 界面死掉 2.微信小程序 业务域名配置顶级域...

  • 简书,你是否可以增加置顶功能!同感共顶丫!

    你是否可以增加置顶功能? 你应该增加文章置顶功能! 以下为我给出的若干理由: 同类内容平台通常都有置顶功能头条设置...

  • Elementui el-input 实现自定义 v-model

    Vue 本身支持自定义组件实现 v-model ,但 el-input 作为 Elementui 的自定义组件也已...

  • 置顶?不置顶

    有些人主页上,上面有一篇或几篇文章,前面红字写着“不置顶”,但看样子分明是置顶的样子。 怎么回事?简书在玩儿最大的...

  • 小程序开发 wepy/mpvue/uni-app/taro

    框架简介: wepy: 接近于 Vue.js,类Vue开发,支持组件 Props 传值,自定义事件、组件分布式复用...

  • UITextView内容置顶显示

    UITextView内容置顶 最近使用UITextView加载大段文字内容的时候,发现从上一个控制器跳转过来,文字...

  • vue组件使用

    组件 vue支持自定义组件,用户可以根据自己的需求封装一个公共的组件,实现组件的复用。 一个单独的vue文件就是一...

  • vue 滚动导航置顶

    1、首先通过data定义三个变量,一个代表滚动条滚动的距离,一个代表div距顶部的距离; 2、通过监听scroll...

网友评论

      本文标题:Vue 置顶组件FixedTopWrap 支持自定义内容置顶

      本文链接:https://www.haomeiwen.com/subject/vhkhcqtx.html