Home > Web Front-end > JS Tutorial > How Can I Verify If an Element Is Visible After Page Scrolling with JavaScript?

How Can I Verify If an Element Is Visible After Page Scrolling with JavaScript?

Patricia Arquette
Release: 2024-12-28 01:11:10
Original
535 people have browsed it

How Can I Verify If an Element Is Visible After Page Scrolling with JavaScript?

Verifying Element Visibility After Scrolling

When loading elements dynamically through AJAX, ensuring their visibility can be challenging, especially if they only become visible after scrolling the page. This article explores methods to determine whether an element is within the visible portion of the page after scrolling.

Solution: JavaScript Visibility Check

To verify element visibility, JavaScript code can be employed. Here's a function that accomplishes this:

function isScrolledIntoView(elem) {
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}
Copy after login

This function takes an element as input and returns a Boolean indicating whether it is visible.

Enhanced Utility Function

For a more versatile approach, consider the following utility function:

function Utils() {

}

Utils.prototype = {
    constructor: Utils,
    isElementInView: function (element, fullyInView) {
        var pageTop = $(window).scrollTop();
        var pageBottom = pageTop + $(window).height();
        var elementTop = $(element).offset().top;
        var elementBottom = elementTop + $(element).height();

        if (fullyInView === true) {
            return ((pageTop < elementTop) && (pageBottom > elementBottom));
        } else {
            return ((elementTop <= pageBottom) && (elementBottom >= pageTop));
        }
    }
};
Copy after login

This function accepts two parameters: an element and an optional flag indicating whether the element should be fully or partially visible.

Usage

To use the function, simply create an instance of the Utils class and call the isElementInView method with the desired element and optional flag:

var Utils = new Utils();
var isElementInView = Utils.isElementInView($('#flyout-left-container'), false);

if (isElementInView) {
    console.log('in view');
} else {
    console.log('out of view');
}
Copy after login

The above is the detailed content of How Can I Verify If an Element Is Visible After Page Scrolling with JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template