Not a programer so dont know the issue with the code

102 Views Asked by At

I am working on a blog using Blogger, now to run a fixed sidebar ad widget to float along the screen I found a code that worked (as I tried various codes) and couldn't use the template built-in variable functions as it caused some other malfunctions. So, now although the code works it has a little problem, as on desktop and mobile you can't scroll down to the bottom of the screen.

I tried some other methods but they didn't work, this one did solve the problem but it won't let you scroll down to the bottom of the screen. I just want a sticky sidebar fixed ad that stays there no matter whether you scroll up or down, and I couldn't debug this code snippet as I am not much of a programmer.

Here's the code:

/*<![CDATA[*/
// Sticky Plugin
// =============
// Author: abc
(function($) {
var defaults = {
        topSpacing: 0,
        bottomSpacing: 0,
        className: 'is-sticky',
        center: false
    },
    $window = $(window),
    $document = $(document),
    sticked = [],
    windowHeight = $window.height(),
    scroller = function() {
        var scrollTop = $window.scrollTop(),
            documentHeight = $document.height(),
            dwh = documentHeight - windowHeight,
            extra = (scrollTop > dwh) ? dwh - scrollTop : 0;
        for (var i = 0; i < sticked.length; i++) {
            var s = sticked[i],
                elementTop = s.stickyWrapper.offset().top,
                etse = elementTop - s.topSpacing - extra;
            if (scrollTop <= etse) {
                if (s.currentTop !== null) {
                    s.stickyElement.css('position', '').css('top', '').removeClass(s.className);
                    s.currentTop = null;
                }
            }
            else {
                var newTop = documentHeight - s.elementHeight - s.topSpacing - s.bottomSpacing -     scrollTop - extra;
                if (newTop < 0) {
                    newTop = newTop + s.topSpacing;
                } else {
                    newTop = s.topSpacing;
                }
                if (s.currentTop != newTop) {
                    s.stickyElement.css('position', 'fixed').css('top', newTop).addClass(s.className);
                    s.currentTop = newTop;
                }
            }
        }
    },
    resizer = function() {
        windowHeight = $window.height();
    };
// should be more efficient than using $window.scroll(scroller) and $window.resize(resizer):
if (window.addEventListener) {
    window.addEventListener('scroll', scroller, false);
    window.addEventListener('resize', resizer, false);
} else if (window.attachEvent) {
    window.attachEvent('onscroll', scroller);
    window.attachEvent('onresize', resizer);
}
$.fn.sticky = function(options) {
    var o = $.extend(defaults, options);
    return this.each(function() {
        var stickyElement = $(this);
        if (o.center)
            var centerElement = "margin-left:auto;margin-right:auto;";
        stickyId = stickyElement.attr('id');
        stickyElement
            .wrapAll('<div id="' + stickyId + 'StickyWrapper" style="' + centerElement + '"></div>')
            .css('width', stickyElement.width());
        var elementHeight = stickyElement.outerHeight(),
            stickyWrapper = stickyElement.parent();
        stickyWrapper
            .css('width', stickyElement.outerWidth())
            .css('height', elementHeight)
            .css('clear', stickyElement.css('clear'));
        sticked.push({
            topSpacing: o.topSpacing,
            bottomSpacing: o.bottomSpacing,
            stickyElement: stickyElement,
            currentTop: null,
            stickyWrapper: stickyWrapper,
            elementHeight: elementHeight,
            className: o.className
        });
    });
};
})(jQuery);
/*]]>*/
</script>
<script type='text/javascript'>
$(document).ready(function(){
$("#mblfloater").sticky({topSpacing:0});
});
1

There are 1 best solutions below

1
mplungjan On

Depending on your HTML and css the code works. For example

// Sticky Plugin
// =============
// Author: abc
(function($) {
  var defaults = {
      topSpacing: 0,
      bottomSpacing: 0,
      className: 'is-sticky',
      center: false
    },
    $window = $(window),
    $document = $(document),
    sticked = [],
    windowHeight = $window.height(),
    scroller = function() {
      var scrollTop = $window.scrollTop(),
        documentHeight = $document.height(),
        dwh = documentHeight - windowHeight,
        extra = (scrollTop > dwh) ? dwh - scrollTop : 0;
      for (var i = 0; i < sticked.length; i++) {
        var s = sticked[i],
          elementTop = s.stickyWrapper.offset().top,
          etse = elementTop - s.topSpacing - extra;
        if (scrollTop <= etse) {
          if (s.currentTop !== null) {
            s.stickyElement.css('position', '').css('top', '').removeClass(s.className);
            s.currentTop = null;
          }
        } else {
          var newTop = documentHeight - s.elementHeight - s.topSpacing - s.bottomSpacing - scrollTop - extra;
          if (newTop < 0) {
            newTop = newTop + s.topSpacing;
          } else {
            newTop = s.topSpacing;
          }
          if (s.currentTop != newTop) {
            s.stickyElement.css('position', 'fixed').css('top', newTop).addClass(s.className);
            s.currentTop = newTop;
          }
        }
      }
    },
    resizer = function() {
      windowHeight = $window.height();
    };
  // should be more efficient than using $window.scroll(scroller) and $window.resize(resizer):
  if (window.addEventListener) {
    window.addEventListener('scroll', scroller, false);
    window.addEventListener('resize', resizer, false);
  } else if (window.attachEvent) {
    window.attachEvent('onscroll', scroller);
    window.attachEvent('onresize', resizer);
  }
  $.fn.sticky = function(options) {
    var o = $.extend(defaults, options);
    return this.each(function() {
      var stickyElement = $(this);
      if (o.center)
        var centerElement = "margin-left:auto;margin-right:auto;";
      stickyId = stickyElement.attr('id');
      stickyElement
        .wrapAll('<div id="' + stickyId + 'StickyWrapper" style="' + centerElement + '"></div>')
        .css('width', stickyElement.width());
      var elementHeight = stickyElement.outerHeight(),
        stickyWrapper = stickyElement.parent();
      stickyWrapper
        .css('width', stickyElement.outerWidth())
        .css('height', elementHeight)
        .css('clear', stickyElement.css('clear'));
      sticked.push({
        topSpacing: o.topSpacing,
        bottomSpacing: o.bottomSpacing,
        stickyElement: stickyElement,
        currentTop: null,
        stickyWrapper: stickyWrapper,
        elementHeight: elementHeight,
        className: o.className
      });
    });
  };
})(jQuery);

$(document).ready(function() {
  $("#mblfloater").sticky({
    topSpacing: 0
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<div id="mblfloater" style="width:40px">Hello</div>
<div style="height:5000px; position: relative; margin-left:40px"> <!-- Parent with relative position -->
  Some text
  <div style="position: absolute; bottom: 0; width: 100%;"> <!-- Child with absolute position -->
    bottom of div
  </div>
</div>
<div style="position: fixed; bottom: 0;">Footer</div>