js 检测元素是否在页面中_检测何时元素在页面上变得可见或隐藏

js 检测元素是否在页面中

观察观察 (vue-observe-visibility)

Vue 2.x directive using Intersection Observer API that detects if the element is visible (in the viewport and not hidden by CSS).

使用Intersection Observer API的Vue 2.x指令检测该元素是否可见(在视口中并且未被CSS隐藏)。

安装 (Installation)

npm install --save vue-observe-visibility
This also works on components:

```html
<MyComponent v-observe-visibility="visibilityChanged" />

The function will be called whenever the visiblity of the element changes with the argument being a boolean (true means the element is visible on the page, false means that it is not).

每当元素的可见性发生变化且参数为布尔值时,就会调用该函数( true表示元素在页面上可见, false表示不可见)。

The second argument is the corresponding IntersectionObserverEntry object.

第二个参数是对应的IntersectionObserverEntry对象。

visibilityChanged (isVisible, entry) {
  this.isVisible = isVisible
  console.log(entry)
}

传递自定义参数 (Passing custom arguments)

You can add custom argument by using an intermediate function:

您可以使用中间函数来添加自定义参数:

<div v-observe-visibility="(isVisible, entry) => visibilityChanged(isVisible, entry, customArgument)">

Here visibilityChanged will be call with a third custom argument customArgument.

这里visibilityChanged将与第三个自定义的参数调用customArgument

(Example)

<div id="app">
  <button @click="show = !show">Toggle</button>
  <label>
    <input type="checkbox" v-model="isVisible" disabled/> Is visible?
  </label>
  <div ref="test" v-show="show" v-observe-visibility="visibilityChanged">Hello world!</div>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    show: true,
    isVisible: true,
  },
  methods: {
    visibilityChanged (isVisible, entry) {
      this.isVisible = isVisible
      console.log(entry)
    },
  },
})
</script>

翻译自: https://vuejsexamples.com/detect-when-an-element-is-becoming-visible-or-hidden-on-the-page/

js 检测元素是否在页面中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值