// Function to run the jQuery animation function runAnimation() { $("#elementToAnimate").animate({ opacity: 1, top: 0 }, 1000); // 1000 milliseconds = 1 second } // Function to check if the element is in the viewport function isElementInViewport(element) { var windowTop = $(window).scrollTop(); var elementTop = $(element).offset().top; var elementBottom = elementTop + $(element).outerHeight(); return elementBottom > windowTop && elementTop < windowTop + $(window).height(); } // Check if the element is initially in the viewport if (isElementInViewport("#elementToAnimate")) { runAnimation(); } // Check if the element enters the viewport when scrolling $(window).on("scroll", function () { if (isElementInViewport("#elementToAnimate")) { runAnimation(); } });