美文网首页技术干货
position: sticky 的vue组件化使用与兼容性写法

position: sticky 的vue组件化使用与兼容性写法

作者: jevons_lee_ | 来源:发表于2019-02-14 15:05 被阅读26次

vue组件化

原理:检查是否兼容position: sticky ,若兼容就使用,若不兼容则在watch监听高度(若高度是变化的)或者在mounted中直接调用(高度不变)

<template>
    <div class="header_sticky">
        <slot></slot>
    </div>
</template>
<script>
export default {
    name: 'stickyHeader',
    computed: {
        randomId: function(){
            return 'randomId_' + Number(Math.random().toString().substr(3,3) + Date.now()).toString(36);
        },
        targetElement_: function() {
            return this.$el
        }
    },
    methods: {
        // css: 用于替换的css样式; (一般用默认的)
        sticky_(css='sticky_') {
            if (CSS.supports('position', 'sticky') || CSS.supports('position', '-webkit-sticky')) {
                console.log('>>>>>>>>> sticky is supported')
            } else {
                let newNodeTop;
                let header = this.targetElement_;
                if(document.getElementById(this.randomId)) {
                    newNodeTop = document.getElementById(this.randomId);
                }else{
                    newNodeTop = document.createElement("div");
                    newNodeTop.id = this.randomId;
                    header.parentNode.insertBefore(newNodeTop, header);
                    header.classList.add(css);
                }

                setTimeout(() => {
                    let height = header.offsetHeight + 1; //高度 + 1 以防有小数点
                    newNodeTop.style.height = height + 'px';
                }, 0)
            }
        },
    }
}
</script>

注意:要用异步获取高度

css

.header_sticky {
    width: 100%;
    position: sticky;
    position: -webkit-sticky;
    top: 0;
    z-index: 100;
    transition: height 1s;
    -moz-transition: height 1s;
    -webkit-transition: height 1s;
    -o-transition: height 1s;
}

.sticky_ {
    width: 100%;
    position: fixed;
    position: -webkit-fixed;
    top: 0;
    z-index: 100;
}

在watch中监听高度变化

watch: {
            oldToNew(newVal, oldVal) {
                if(newVal.length !== oldVal.length) {
                    this.$refs.sticky_.sticky_()
                }
            }
        }

在mounted中获取高度变化

this.$refs.sticky_.sticky_()

<使用>

html

<sticky-header ref="sticky_">
    <!-- contents -->
</sticky-header>

相关文章

  • position: sticky 的vue组件化使用与兼容性写法

    vue组件化 原理:检查是否兼容position: sticky ,若兼容就使用,若不兼容则在watch监听高度(...

  • weex 踩坑

    1. 用weex实现页面跳转? 1.1 使用vue-router position: fixed/sticky 的...

  • position:sticky和display:grid

    position:sticky 首先介绍一下position:sticky。positin:sticky是一个新的...

  • IOS的那些定位

    position: sticky; position属性中最有意思的就是sticky了,设置了sticky的元素,...

  • position:sticky

    杀了个回马枪,还是说说position:sticky吧 1. position:sticky简介 单词sticky...

  • position:sticky 的使用

    如果你想想做这样一个滚动跟随。那你肯定第一时间想到的一定是position:fixed;这个css 属性,然后加上...

  • position:sticky 的使用

    如果你想想做这样一个滚动跟随。那你肯定第一时间想到的一定是 position:fixed; 这个css 属性,然后...

  • 网页布局之粘性布局

    position: -webkit-sticky; position: sticky; top:0; 只需要在CS...

  • Vue 记事

    嵌套路由简易写法 Vue setInterval 使用 Vue 组件中添加监听 添加监听: handleGloba...

  • 2019-01-23

    CSS3学习 Tips: 兼容性查询:caniuse.com兼容写法要写在标准写法之前position:absol...

网友评论

    本文标题:position: sticky 的vue组件化使用与兼容性写法

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