Code Monkey home page Code Monkey logo

hoc-element-affix's Introduction

@hoc-element/affix

npm NPM

📌 将页面元素固定在可视范围内。

Live demo: https://pdsuwwz.github.io/hoc-element-affix

Environment Support

  • Vue 2.6.0+

Install

npm install @hoc-element/affix

Quick Start

import Vue from 'vue'
import HocElementAffix from '@hoc-element/affix'

Vue.use(HocElementAffix)

Feature

  • 自定义顶部偏移量
  • 固定状态改变时触发的回调
  • 插槽式的固定状态反馈

Using

绑定参数 Attributes

字段 说明 类型 默认值
offsetTop (可选)距离窗口顶部多少时开始固定 Number 0

事件 Events

字段 说明 类型
change (可选)固定状态发生改变时的回调函数 Function

插槽数据 Slot

绑定的数据默认在一个对象里,如需要可直接解构再使用

字段 说明 类型 默认值
affixed (可选)实时的固定状态 Boolean false

Demo

下面是比较全的例子,几乎囊括了 API 的所有用法,源码戳这: Code

<template>
  <div class="box-container">
    <div class="content">
      <div class="long-list">
        <div
          v-for="(item, index) in 50"
          :key="index"
          class="long"
        >
          <template
            v-if="index === 2"
          >
            <hoc-el-affix>
              <template v-slot="{ affixed }">
                <div class="box">
                  <span style="font-size: 25px">{{ affixed ? '🍎' : '🍏' }}</span>
                  吸顶【插槽版】
                </div>
              </template>
            </hoc-el-affix>
          </template>
          <template
            v-else-if="index === 9"
          >
            <hoc-el-affix
              :offset-top="120"
              @change="handleAffixed120"
            >
              <div class="box">
                <span style="font-size: 25px">{{ isAffixed120 ? '🌝' : '🌚' }}</span>
                距离顶部120px时固定【回调版】
              </div>
            </hoc-el-affix>
          </template>
          <template
            v-else
          >
            {{ index === 49 ? '到底了' : `占位符${index + 1}` }}
          </template>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ExampleAffix',
  data () {
    return {
      isAffixed120: false
    }
  },
  methods: {
    handleAffixed120 (affixed) {
      this.isAffixed120 = affixed
    }
  }
}
</script>

<style lang="scss" scoped>
.box-container {
  .content {
    position: relative;
    width: 300px;
    margin: 0 auto;
    text-align: center;
    .box {
      width: 300px;
      height: 50px;
      line-height: 50px;
      border: 1px solid #000;
    }
    .long-list {
      .long {
        height: 50px;
        line-height: 50px;
        font-size: 16px;
      }
    }
  }
}
</style>

hoc-element-affix's People

Contributors

pdsuwwz avatar

Stargazers

naooan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.