Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

jquery event element in viewport

$(document).ready(function () {
    function checkImages() {
        $('img:not(.checked)').each(function () {
            if ($(this).isOnScreen()) {
                $(this).addClass('checked');
                console.log('user saw this image', this.src);
                // do some stuff
            }
        });
    }
    $.fn.isOnScreen = function () {
        const win = $(window);
        const viewport = {
            top: win.scrollTop(),
            left: win.scrollLeft()
        };
        viewport.right = viewport.left + win.width();
        viewport.bottom = viewport.top + win.height();
        const bounds = this.offset();
        bounds.right = bounds.left + this.outerWidth();
        bounds.bottom = bounds.top + this.outerHeight();
        return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));
    };
    $(window).scroll(function () {
        checkImages();
    });
    checkImages();
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: mongo db get child result with array of parent ids 
Javascript :: json query rails c 
Javascript :: javascript get elemet last of array 
Javascript :: laravel sending email to outlook link not working 
Javascript :: clickable image full screen javascript 
Javascript :: in nav link if I click on the same active link, page has to refresh in react js 
Javascript :: queryselector undefined not working in react js 
Javascript :: Saving dependencies in your node package.json syntax 
Javascript :: phaser set mass 
Javascript :: jquery show loader 
Javascript :: string to date with ist javascript 
Javascript :: barcode javascript library 
Javascript :: react router how to prevent navlink from two classes 
Javascript :: Javascript Area When Base and Height is Known 
Javascript :: how do i block or restrict special characters from input fields with jquery 
Javascript :: Accessing Function Declared Outside Constructor But Inside Class 
Javascript :: CUSTOM_ELEMENTS_SCHEMA 
Javascript :: jquery properties 
Javascript :: sweet alert for react 
Javascript :: use function in mongodb query example 
Javascript :: javascript enter key 
Javascript :: backbone view initialize 
Javascript :: how to change array elements position in array in javascript 
Javascript :: Backbone Collection Example 
Javascript :: copy array 
Javascript :: convert 12 hour to 24 hour javascript 
Javascript :: connect react to backend 
Javascript :: jquery search string for substring 
Javascript :: socket io server 
Javascript :: createReadStream axios 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =