title |
---|
scrollIntoView |
Scroll an element into view.
.scrollIntoView()
.scrollIntoView(options)
{% fa fa-check-circle green %} Correct Usage
cy.get('footer').scrollIntoView() // Scrolls 'footer' into view
{% fa fa-exclamation-triangle red %} Incorrect Usage
cy.scrollIntoView('footer') // Errors, cannot be chained off 'cy'
cy.window().scrollIntoView() // Errors, 'window' does not yield DOM element
{% fa fa-angle-right %} options (Object)
Pass in an options object to change the default behavior of .scrollIntoView()
.
Option | Default | Description |
---|---|---|
duration |
0 |
Scrolls over the duration (in ms) |
easing |
swing |
Will scroll with the easing animation |
log |
true |
{% usage_options log %} |
offset |
{top: 0, left: 0} |
Amount to scroll after the element has been scrolled into view |
timeout |
{% url defaultCommandTimeout configuration#Timeouts %} |
{% usage_options timeout .scrollIntoView %} |
{% yields same_subject .scrollIntoView %}
cy.get('button#checkout').scrollIntoView()
.should('be.visible')
cy.get('.next-page').scrollIntoView({ easing: 'linear' })
cy.get('footer').scrollIntoView({ duration: 2000 })
Cypress does not reflect the accurate scroll positions of any elements within snapshots. If you want to see the actual scrolling behavior in action, we recommend using {% url .pause()
pause %} to walk through each command or {% url 'watching the video of the test run' screenshots-and-videos#Videos %}.
{% requirements dom .scrollIntoView %}
{% assertions wait .scrollIntoView %}
{% timeouts assertions .scrollIntoView %}
cy.get('#scroll-horizontal button').scrollIntoView()
.should('be.visible')
The commands above will display in the Command Log as:
{% imgTag /img/api/scrollintoview/command-log-for-scrollintoview.png "command log scrollintoview" %}
When clicking on the scrollintoview
command within the command log, the console outputs the following:
{% imgTag /img/api/scrollintoview/console-log-for-scrollintoview.png "console.log scrollintoview" %}
- {% url
cy.scrollTo()
scrollto %}