npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-exposure

v1.4.0

Published

基于InterfaceObserver API,当绑定元素出现在视窗内的时候执行回调

Downloads

14

Readme

vue-exposure

npm npm bundle size npm NPM

中文简体

Support Vue 3.x Document

Based on the InterfaceObserver API, the vue directive is used to bind the element and execute callback functions when the element appears in the viewport.

Quick Start

Install

npm install vue-exposure --save

Since InterfaceObserver API compatibility is still not well supported in some lower versions of browsers, vue-exposure released two packages: normal and with polyfill.

import normal package

import Exposure from 'vue-exposure'

import polyfill package

import Exposure from 'vue-exposure/dist/exposure-polyfill'

Use Plugin

By default, vue-exposure executes the callback function only when all areas of the element are displayed in the viewport.

Vue.use(Exposure)

Use in component

vue-exposure is based on the vue directive wrapper, making it easier to develop, components such as the following.

<template>
  <div class="exposure-test">
    <div class="top" v-exposure="handlerTop"></div>
    <div class="middle" v-exposure="handlerMiddle"></div>
    <div class="bottom" v-exposure="handlerBottom"></div>
  </div>
</template>

<script>
export default {
  name: 'ExposureText',
  data() {
    return {
      handlerBottom: {
        enter() {
          console.log('bottom enter')
        },
        leave() {
          console.log('bottom leave')
        }
      }
    }
  }
  methods: {
    handlerMiddle() {
      alert('middle')
    },
    handlerTop() {
      alert('top')
    },
  },
}
</script>

<style scoped>
.top {
  background-color: red;
  margin-bottom: 1000px;
}
.middle {
  background-color: yellowgreen;
}
.bottom {
  background-color: blue;
  margin-top: 1000px;
}
</style>

Scroll through the interface, triggering the callback function when the element appears in the viewport.

handler

The handler is also the value of the instruction, similar to the value of the computed property, and is of two types: function or object.

function

The function type is the more common way of writing, and the function handler will only be triggered once if the element is exposed and the threshold is met.

object

Handlers of object type need to have one of the enter and leave attributes, and the values of the enter and leave attributes are function types.

  • enter: enter handler is triggered once if the element enters exposure and threshold is met
  • leave: leave handler is triggered once after the enter handler is triggered and the element leaves the visible area completely.

threshold

By default, exposure callbacks are not executed until the entire bound element is wrapped. If you have a need when a certain percentage of the element is exposed, the threshold can be set in the following two ways.

Global threshold

vue-exposure supports global threshold setting.

Vue.use(Exposure, {
  threshold: 0.2,
})

As shown in the above code, the callback function is executed when the exposure ratio of the element reaches 0.2.

Element threshold

If you want an element to have a different exposure ratio than other elements, you can set the threshold for the element separately.

<template>
  <div class="exposure-test">
    <div class="top" v-exposure:[0.1]="handlerTop"></div>
    <div class="middle" v-exposure:[0.5]="handlerMiddle"></div>
    <div class="bottom" v-exposure:[threshold]="handlerBottom"></div>
  </div>
</template>

<script>
export default {
  name: 'ExposureText',
  data() {
    return {
      threshold: 0.8,
    }
  },
}
</script>

The directive is parameterized using Vue's dynamic directive parameters, which must be passed as a value between [0,1] so that the exposure is scaled to the passed value when listening to the exposure.

Note: Element threshold > Global threshold.

$resetExposure

The execution of the exposure callback is single-case, which means that when exposed once and the callback is executed, the callback function is not executed when exposed again.

In the case of a KeepAlive scenario in a Vue component, the exposure callback is not re-executed when the KeepAlive component is switched. In this case, you need to use the $resetExposure API to reset the element state if you want to re-execute it.

deactivated() {
  this.$resetExposure()
}

When this.$resetExposure() is called without any arguments, it resets the execution state of all listener elements in the current instance. If you need to reset the execution state of just one element, you need to pass in the current element.

deactivated() {
  this.$resetExposure(this.$refs.el)
}

Vue 2 + composition-api

If the project is built with Vue 2 + composition-api, useResetExposure to reset the exposure in order to follow the composition-api coding specification.

import { useResetExposure } from 'vue-exposure'
import { defineComponent, onDeactivated } from '@vue/composition-api'
export default defineComponent({
  setup() {
    onDeactivated(() => {
      useResetExposure()
    })
  }
})

Caution

vue-exposure listens on elements in strict mode, and does not listen on elements whose visibility is hidden or whose width is 0 or whose height is 0.