Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Make filtering div appear to float to the right of the results div
- $('#filters').custFloat({width_guide_id: 'filters-width-guide', height_guide_el: '#results'});
- // Custom float (note: relies on underscore.js for throttling and debounce)
- (function ($, _) {
- $.fn.custFloat = function (options) {
- var opts = _.extend({
- width_guide_id: 'float-width-guide', // Should be a valid id string
- height_guide_el: 'body', // Should be a jquery selector
- pin_boundary_adjust_offset: 0,
- outer_height_adjust_offset: 100,
- scroll_throttle_ms: 15,
- debounce_timer_ms: 25
- }, options);
- return this.each(function () {
- var $this = $(this),
- $parent = $this.offsetParent(),
- $window = $(window),
- $width_guide_el = $('<div id=' + opts.width_guide_id + '></div>').insertBefore($this),
- top = Math.floor($this.offset().top - parseFloat($this.css('marginTop').replace(/auto/, 0))),
- floating = false,
- pinned = false,
- scroll = function() {
- var y = $window.scrollTop(),
- height = $this.outerHeight(true) + opts.outer_height_adjust_offset;
- parent_height = $parent.height(),
- available_height = $(opts.height_guide_el).height(),
- parent_top_offset = $parent.offset().top,
- pin_boundary = Math.ceil(parent_top_offset + parent_height - height - opts.pin_boundary_adjust_offset);
- if (height < available_height) {
- if ((y >= top) && (y < pin_boundary) && (floating === false) && (pinned === false)) {
- floatIt();
- } else if ((y >= pin_boundary) && (pinned === false)) {
- pinIt();
- } else if ((y < pin_boundary) && (pinned)) {
- floatIt();
- } else if ((y < top) && (floating)) {
- restoreIt();
- } else {
- // Noop
- }
- } else {
- restoreIt();
- }
- },
- resize = function() {
- var width = $width_guide_el.width();
- $this.css({width: width});
- };
- function floatIt() {
- var width = $width_guide_el.width();
- floating = true;
- pinned = false;
- $this.css({
- position: 'fixed',
- top: 0,
- pin_boundary: '',
- width: width
- });
- }
- function pinIt() {
- var width = $width_guide_el.width(),
- height = $this.outerHeight(true) + opts.outer_height_adjust_offset;
- pinned = true;
- floating = false;
- $this.css({
- position: 'absolute',
- top: $parent.height() - height - opts.pin_boundary_adjust_offset,
- pin_boundary: '',
- width: width
- });
- }
- function restoreIt() {
- floating = false;
- pinned = false;
- $this.css({
- position: '',
- top: '',
- pin_boundary: '',
- width: ''
- });
- }
- $window.scroll(_.throttle(scroll, opts.scroll_throttle_ms));
- $window.resize(_.debounce(resize, opts.debounce_timer_ms));
- });
- };
- })(jQuery, _);
Advertisement
Add Comment
Please, Sign In to add comment