Coder Social home page Coder Social logo

sticky-div's People

Contributors

dubroe avatar jonabrams avatar keithslater avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

sticky-div's Issues

Nice little plugin!

Hey

Good job, if a first plugin! I found it here: http://stackoverflow.com/questions/2907367/have-a-div-cling-to-top-of-screen-if-scrolled-down-past-it

I am showing a relatively positioned div, on a jquery ajax complete for adding to a shopping basket, to show a message near the "mini basket". As such, I have made a couple of changes to the code. Commented below. Now it works a treat, even on a mobile (as site is responsive)

(function ($) {
    $.fn.sticky_div = function (options) {
        var defaults = {
            top: 0,
            min_screen_width: 0,
            bottom: 0,
            outer_div: null
        };
        var options = $.extend(defaults, options);

        // added !important to override any current positioning or top.
        $("<style type='text/css'> .sticky-div{ position:fixed !important; top:" + options.top + "px !important;} </style>").appendTo("head");

        var selector = this;

        // added a class to the anchor, and checked for it, so that the anchor <div/> is only added the once.
        selector.each(function () {
            if ($(this).siblings(".sticky-anchor").length === 0) {
                $(this).before("<div class='sticky-anchor'></div>");
            }
        });

        // call to refactored method, so that it is not only a window scroll which triggers it (if, for example, my add to basket button is at the bottom of the page, and my anchor is at the top
        $.sticky_div(selector, options);
        $(window).scroll(function () {
            if ($(window).width() >= options.min_screen_width) {
                // called again on scroll.
                $.sticky_div(selector, options);
            }
        });
    };

    // refactored method, which does the magic
    $.sticky_div = function (selector, options) {
        var window_top = $(window).scrollTop();
        var window_height = $(window).height();
        selector.each(function () {
            var div_top = $(this).prev().offset().top;
            var div_height = $(this).outerHeight();
            if ((!options.outer_div || (div_height < options.outer_div.height())) && (window_top > (div_top - options.top)) && (div_height < (window_height - options.bottom))) {
                $(this).addClass('sticky-div');
            } else {
                $(this).removeClass('sticky-div');
            }
        });
    }
})(jQuery);

When I put a value on "bottom:"

When I put a value on bottom like this "bottom: 515," the code doesn't work anymore. I just want to stop the div when it hits the footer div. Am I doin it right?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.