Home > Web Front-end > JS Tutorial > jQuery Keep Element in View When Scrolling

jQuery Keep Element in View When Scrolling

Lisa Kudrow
Release: 2025-02-27 01:05:09
Original
676 people have browsed it

This jQuery code snippet keeps an element in view as the page scrolls. A demo shows this effect on a right sidebar ad. The code is presented both as a standalone script and as a reusable jQuery plugin. Finally, a FAQ section addresses common jQuery scrolling questions.

jQuery Keep Element in View When Scrolling

The Code (Standalone):

//keep element in view
(function($) {
    $(document).ready(function() {
        var elementPosTop = $('#jq4u-sidebar-ads').position().top;
        $(window).scroll(function() {
            var wintop = $(window).scrollTop(), docheight = $(document).height(), winheight = $(window).height();
            if (wintop > elementPosTop) {
                $('#jq4u-sidebar-ads').css({ "position": "fixed", "top": "10px" });
            } else {
                $('#jq4u-sidebar-ads').css({ "position": "inherit" });
            }
        });
    });
})(jQuery);
Copy after login

The Code (jQuery Plugin):

/**
 * jQuery keep element in view plugin.
 *
 * @author      Sam Deering
 * @copyright   Copyright (c) 2012 jQuery4u
 * @license     http://jquery4u.com/license/
 * @link        http://jquery4u.com
 * @since       Version 1.0
 * @notes       Plugin only works on scroll down elements.
 */

(function($) {
    $.fn.keepElementInView = function() {
        var elemPosTop = this.position().top;
        $(window).scroll(function() {
            var wintop = $(window).scrollTop(), docheight = $(document).height(), winheight = $(window).height();
            if (wintop > elemPosTop) {
                this.css({ "position": "fixed", "top": "10px" });
            } else {
                this.css({ "position": "inherit" });
            }
        });
        return this;
    };

    $(document).ready(function() {
        $('#jq4u-sidebar-ads').keepElementInView();
    });
})(jQuery);
Copy after login

Frequently Asked Questions (FAQs):

This section provides concise answers to common questions about jQuery element view scrolling, including techniques for animated and instant scrolling, horizontal scrolling, viewport checks, and button-triggered scrolling. The code examples are slightly reformatted for improved readability.

Q1: Animate scroll to element?

$('html, body').animate({ scrollTop: $("#elementID").offset().top }, 2000);
Copy after login

Q2: Instant scroll to element?

$('html, body').scrollTop($("#elementID").offset().top);
Copy after login

Q3: Horizontal scroll to element?

$('html, body').animate({ scrollLeft: $("#elementID").offset().left }, 2000);
Copy after login

Q4: Check if element is in viewport?

function isInViewport(element) {
  let elementTop = $(element).offset().top;
  let elementBottom = elementTop + $(element).outerHeight();
  let viewportTop = $(window).scrollTop();
  let viewportBottom = viewportTop + $(window).height();
  return elementBottom > viewportTop && elementTop < viewportBottom;
}
Copy after login

Q5: Scroll to element on button click?

$("#buttonID").click(function() {
  $('html, body').animate({ scrollTop: $("#elementID").offset().top }, 2000);
});
Copy after login

The above is the detailed content of jQuery Keep Element in View When Scrolling. For more information, please follow other related articles on the PHP Chinese website!

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