Sha256: 1bba2acc098b32ce9164ac3c0e047b24770d2c8857de6d3ec9c8c16a718ad162

Contents?: true

Size: 1.96 KB

Versions: 7

Compression:

Stored size: 1.96 KB

Contents

;(function ($) {
    var defaults = {
        approach:   0,
        overtravel: 0
    };

    /*
        Trigger appropriate events when the selected element is scrolled in or
        out of view.

        An element is considered to be in view if the length of document
        scrolled is greater than or equal to the offset of the element relative
        to the top of the document and within the vertical span of the element.
        (its height).

        Vertical span of the element can be modified by adding required
        'approach' and 'overtravel' length.

        Events:

            enter.scrollspy:

                When the selected element scrolls into the view.

            leave.scrollspy:

                When the selected element scrolls out of the view.

        Options:

            approach:

                Extend vertical span of the element from top.  It can be
                positive or negative.

            overtravel:

                Extend vertical span of the element from bottom.  It can be
                positive or negative.

            The terminology: approach and overtravel, are borrowed from
            Machining :).

        Usage:

            $('.resultn').scrollspy();

    */
    $.fn.scrollspy = function (options) {
        var elements = this;
        var options  = $.extend({}, defaults, options);

        $(window).scroll(function() {
            var scrolled = $(this).scrollTop();

            elements.each(function() {
                var element = $(this);

                //compute threshold
                var start = element.offset().top - options.approach;
                var end   = start + element.height() + options.overtravel;

                if (scrolled >= start && scrolled <= end){
                    element.trigger('enter.scrollspy');
                }
                else {
                    element.trigger('leave.scrollspy');
                }
            });
        });

        return this;
    }
})(jQuery);

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sequenceserver-1.0.0.pre.2 public/js/jquery.scrollspy.js
sequenceserver-1.0.0.pre.1 public/js/jquery.scrollspy.js
sequenceserver-0.8.4 public/js/jquery.scrollspy.js
sequenceserver-0.8.3 public/js/jquery.scrollspy.js
sequenceserver-0.8.2 public/js/jquery.scrollspy.js
sequenceserver-0.8.1 public/js/jquery.scrollspy.js
sequenceserver-0.8.0 public/js/jquery.scrollspy.js