You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blogger侧边栏粘性广告无法滚动到底部 代码调试求助

修复Blogger侧边栏粘性广告滚动到底部受限问题

问题情况

在Blogger搭建博客时,为实现侧边栏广告组件随页面滚动固定悬浮,使用了一段代码,虽能实现固定效果,但存在桌面端和移动端均无法滚动至页面底部的问题。尝试过其他方法无效,且无法使用模板内置变量功能(会引发其他故障),需要调试这段代码。

问题根源

原代码在计算粘性元素的newTop值时逻辑错误,当滚动到页面底部区域时,错误的top值计算导致页面滚动被限制,无法触及底部。

修复后的完整代码

/*<![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 {
                // 修复核心:调整newTop计算逻辑,确保滚动到底部时不限制页面滚动
                var maxBottomScroll = documentHeight - windowHeight - s.bottomSpacing - s.elementHeight;
                if (scrollTop > maxBottomScroll) {
                    // 当滚动到接近底部时,让元素随页面上移
                    var newTop = scrollTop - maxBottomScroll;
                    s.stickyElement.css('position', 'fixed').css('top', -newTop).addClass(s.className);
                } else {
                    // 正常滚动时保持顶部固定
                    s.stickyElement.css('position', 'fixed').css('top', s.topSpacing).addClass(s.className);
                }
                s.currentTop = s.stickyElement.css('top');
            }
        }
    },
    resizer = function() {
        windowHeight = $window.height();
    };
// 绑定滚动和 resize 事件
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);
        var centerElement = o.center ? "margin-left:auto;margin-right:auto;" : "";
        var 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, bottomSpacing:20}); // 可调整bottomSpacing值,控制底部留白
});

使用说明

  1. 将上述代码替换原有的粘性广告代码
  2. 可根据需求调整bottomSpacing参数(示例中设为20),控制粘性广告距离页面底部的留白距离
  3. 确保你的Blogger模板已经引入了jQuery库(大部分Blogger模板默认已包含)

内容的提问来源于stack exchange,提问作者Mr.IT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 20:22:49