<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: 2.0.0 -",
    "watchOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/onScrollTargetVisibilityChange(idType:threshold:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE30onScrollTargetVisibilityChange6idType9threshold_Qrqd__m_SdySayqd__GctSHRd__lF"
  },
  "title" : "onScrollTargetVisibilityChange(idType:threshold:_:)"
}
-->

# onScrollTargetVisibilityChange(idType:threshold:_:)

Adds an action to be called with information about what views would
be considered visible.

```
nonisolated func onScrollTargetVisibilityChange<ID>(idType: ID.Type, threshold: Double = 0.5, _ action: @escaping ([ID]) -> Void) -> some View where ID : Hashable
```

## Parameters

`idType`

The type of Identity provided by the subviews.

`threshold`

The amount required to be visible within the viewport of the
the scrollview before the `action` is fired.  By default when the view
has crossed more than 50% on-screen, the action will be called.

`action`

The action which will be called when the views within the
scroll view cross the provided threshold. The callback will include
which views are considered visible.

## Discussion

Use this modifier along with the modifier `View/scrollTargetLayout()`
to be informed when the views in the targetted scroll view have crossed
the provided threshold to be considered on/off screen.

```
ScrollView {
    LazyVStack {
        ForEach(models) { model in
            CardView(model: model)
        }
    }
    .scrollTargetLayout()
}
.onScrollTargetVisibilityChange(for: Model.ID.self, threshold: 0.2) { onScreenCards in
    // Disable video playback for cards that are offscreen...
}
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)