自定义 JavaScript 在 Chrome 中不起作用

Custom Javascript not working in Chrome

本文关键字:不起作用 Chrome JavaScript 自定义      更新时间:2023-09-26

浏览了很多,摆弄了很多。得出的结论是,别人可能会看到我视而不见的错误。

代码应该根据窗口高度、侧边栏高度

、内容高度等移动侧边栏。

这是代码:

$(document).ready(function() {
    var windowheight = $(window).height();
    var identheight = $(".ident").height();
    var sidebarheight = $(".sidebar").height();
    var mainheight = $(".main").height();
    var pos = $(window).scrollTop();
    var diff = (((sidebarheight + 20) + identheight) - windowheight);
    var cur = ((sidebarheight + 20) + (pos - diff)) - 2;
    var max = (mainheight + 30);
    contentScroll();
$(window).resize(function(){
    windowheight = $(window).height();
    identheight = $(".ident").height();
    sidebarheight = $(".sidebar").height();
    mainheight = $(".main").height();
    pos = $(window).scrollTop();
    diff = (((sidebarheight + 20) + identheight) - windowheight);
    cur = (sidebarheight + 20) + (pos - diff);
    max = (mainheight + 30);
    contentScroll();
});
$(window).scroll(function (){
    pos = $(window).scrollTop();
    diff = (((sidebarheight + 20) + identheight) - windowheight);
    cur = (sidebarheight + 20) + (pos - diff);
    max = (mainheight + 30);
    contentScroll();
});
function contentScroll() {
if (sidebarheight < mainheight) {
        if (diff < identheight) {
            if (pos >= identheight) {
                $('.sidebar').css({
                    'margin-top'    :   (pos - identheight) + 'px'
                });
            }
        } else {
            if (pos >= diff && cur <= max) {
                $('.sidebar').css({
                    'margin-top'    :   (pos - diff) + 'px'
                });
            }
            if (pos <= diff) {
                $('.sidebar').css({
                    'margin-top'    :   '0px'
                });
            }
        }
    }
}
});

我知道它并不完美,它仍处于艰难阶段。它在FireFox中工作得很好,但在chrome中则不然。正在调用该函数(使用警报进行测试)。它只是什么都不做。

可能是关于chrome阅读语法的不同。

如果有人看到我的错误会好心地指出我,我已经太久了,不能继续为此开脑袋。

这是有问题的模拟网站:http://klok-bremen.de/fff/

使用 $(window).

load 而不是 $(document).ready,因为父元素的高度在图像加载后会发生变化。