zoukankan      html  css  js  c++  java
  • 吸顶效果—position:sticky的vue组件化和兼容性解决

    封装

    <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
            }
        },
      mounted() {
       //获取高度变化
        this.$refs.sticky_.sticky() 
      }, 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) } },
        watch: {
          oldToNew(newVal, oldVal) {
            if(newVal.length !== oldVal.length) {
              this.$refs.sticky_.sticky()
            }
          }
        }, } } </script>
    <style scoped>
    .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;
    }

    </style>
    
    
    
    

    使用


      
    <sticky-header ref="sticky_">
        <!-- contents -->
    </sticky-header>
     
  • 相关阅读:
    【HIDS】关于HIDS的一些看法
    图片在容器内水平垂直居中显示
    C++ 实现Cholesky分解
    Minikube 安装和简单使用
    关于.net的一些记录
    C#将窗体Form嵌入主窗体Panel中的一些问题
    Pod持久化
    Maven打包包含jar包
    Adam
    [漏洞]DNS Server Spoofed Request Amplification DDoS
  • 原文地址:https://www.cnblogs.com/zjxiang008/p/12118776.html
Copyright © 2011-2022 走看看