|
| 1 | +# Visible |
| 2 | + |
| 3 | +> The `v-b-visible` directive allows you to react when an element becomes visible in the viewport. |
| 4 | +
|
| 5 | +The `v-b-visible` directive was added in version `2.1.0`. |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +- `v-b-visible` will call your callback method with a boolean value indicating if the element is |
| 10 | + visible (intersecting) with the viewport. |
| 11 | +- The directive can be placed on almost any element or component. |
| 12 | +- Changes in visibility cqn also be detected (such as `display: none`), as long as the element is |
| 13 | + within (or partially within) the viewport, or within the optional offset. |
| 14 | +- Several BootstrapVue components use `v-b-visible`, such as `<b-img-lazy>`. |
| 15 | +- The `v-b-visible` directive requires browser support of |
| 16 | + [`IntersectionObserver`](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API). |
| 17 | + For older browsers that do not support `IntersectionObserver`, you will need to use a |
| 18 | + [polyfill](/docs/#js). |
| 19 | + |
| 20 | +## Directive syntax and usage |
| 21 | + |
| 22 | +```html |
| 23 | +<div v-b-visible.[mod].[...]="callback">content</div> |
| 24 | +``` |
| 25 | + |
| 26 | +Where `callback` is required: |
| 27 | + |
| 28 | +- A function reference that will be called whenever visibility changes. The callback is passed a |
| 29 | + single boolean argument. `true` indicates that the element is intersecting (partially or entirely |
| 30 | + visible) in the viewport, or `false` if the element is not visible/intersecting with the viewport. |
| 31 | + The callback will be called each time the element's visibility changes (except when hte `once` |
| 32 | + modifier is used. See below for details) |
| 33 | + |
| 34 | +Where `[mod]` can be (all optional): |
| 35 | + |
| 36 | +- A positive number representing the offset (margin) in pixels _away_ from the edge of the viewport |
| 37 | + to determine when the element is considered in (or just about to be in) the viewport. The value |
| 38 | + adds a margin around the view port. The default value is `0`. |
| 39 | +- The keyword `once`. When this modifier is present, the callback will be called once (with the |
| 40 | + argument of `true` indicating the element is intersecting/visible) when the element is |
| 41 | + intersecting with the viewport. Note the callback may be called prior to this with an argument of |
| 42 | + `false` signifying the element is not intersecting/visible. |
| 43 | + |
| 44 | +### Usage examples |
| 45 | + |
| 46 | +Basic: |
| 47 | + |
| 48 | +```html |
| 49 | +<template> |
| 50 | + <div v-b-visible="visibleHandler"> ... </div> |
| 51 | +</template> |
| 52 | +<script> |
| 53 | +export default { |
| 54 | + methods: { |
| 55 | + visibleHandler(isVisible) { |
| 56 | + if (isVisible) { |
| 57 | + // Do something |
| 58 | + } else { |
| 59 | + // Do something else |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | +</script> |
| 65 | +``` |
| 66 | + |
| 67 | +With viewport offset modifier of 350px (if the element is outside of the physical viewport by at |
| 68 | +least 350px, then it will be considered "visible"): |
| 69 | + |
| 70 | +```html |
| 71 | +<template> |
| 72 | + <div v-b-visible.350="visibleHandler"> ... </div> |
| 73 | +</template> |
| 74 | +<script> |
| 75 | +export default { |
| 76 | + methods: { |
| 77 | + visibleHandler(isVisible) { |
| 78 | + if (isVisible) { |
| 79 | + // Do something |
| 80 | + } else { |
| 81 | + // Do something else |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | +</script> |
| 87 | +``` |
| 88 | + |
| 89 | +With `once` modifier: |
| 90 | + |
| 91 | +```html |
| 92 | +<template> |
| 93 | + <div v-b-visible.350="visibleHandler"> ... </div> |
| 94 | +</template> |
| 95 | +<script> |
| 96 | +export default { |
| 97 | + methods: { |
| 98 | + visibleHandler(isVisible) { |
| 99 | + if (isVisible) { |
| 100 | + // This will only ever happen once, when the |
| 101 | + // element has become visible for the first time |
| 102 | + } else { |
| 103 | + // This may happen zero or more times before |
| 104 | + // the element becomes visible, but will never |
| 105 | + // happen after the element has become visible |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | +</script> |
| 111 | +``` |
| 112 | + |
| 113 | +With `once` and offset modifiers: |
| 114 | + |
| 115 | +```html |
| 116 | +<template> |
| 117 | + <div v-b-visible.350.once="visibleHandler"> ... </div> |
| 118 | +</template> |
| 119 | +<script> |
| 120 | +export default { |
| 121 | + methods: { |
| 122 | + visibleHandler(isVisible) { |
| 123 | + if (isVisible) { |
| 124 | + // This will only ever happen once, when the |
| 125 | + // element has become visible for the first time |
| 126 | + } else { |
| 127 | + // This may happen zero or more times before |
| 128 | + // the element becomes visible, but will never |
| 129 | + // happen after the element has become visible |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | +</script> |
| 135 | +``` |
0 commit comments