scrollTo (jQuery) 在 Firefox 中不起作用

发布于 2024-10-08 15:37:20 字数 2570 浏览 10 评论 0原文

由于某种原因,firefox 似乎忽略了我的scrollTo 函数,即使它可以在 chrome 和 safari 中工作。

这是一个示例链接: http://blog.rainbird.me/post/2358248459/ blowholes-are-awesome

Chrome 和 Safari 会自动滚动到图像顶部(偏移量为 20 像素),

它在 Firefox 中不起作用。我很困惑!

代码:

$(document).ready(function() {
$(".photoShell img").lazyload({
placeholder: "http://william.rainbird.me/boston-polaroid/white.gif",
threshold: 200
});


window.viewport =
{
height: function() {
return $(window).height();
},

width: function() {
return $(window).width();
},

scrollTop: function() {
return $(window).scrollTop();
},

scrollLeft: function() {
return $(window).scrollLeft();
}

};

$(".photoShell img").hide();
$(".photoShell .caption").hide();

$(".photoShell img").load(function() {

var maxWidth = viewport.width() - 40; // Max width for the image
if(maxWidth > 960){
    maxWidth = 960;
}
var maxHeight = viewport.height() - 50;    // Max height for the image
var ratio = 0;  // Used for aspect ratio
var width = $(this).width();    // Current image width
var height = $(this).height();  // Current image height

        // Check if the current width is larger than the max
        if(width > maxWidth){
            ratio = maxWidth / width;   // get ratio for scaling image
            $(this).css("width", maxWidth); // Set new width
            $(this).css("height", height * ratio);  // Scale height based on ratio
            height = height * ratio;    // Reset height to match scaled image
            width = width * ratio;    // Reset width to match scaled image

        }

        // Check if current height is larger than max
        if(height > maxHeight){
            ratio = maxHeight / height; // get ratio for scaling image
            $(this).css("height", maxHeight);   // Set new height
            $(this).css("width", width * ratio);    // Scale width based on ratio
            width = width * ratio;    // Reset width to match scaled image
        }


        $(this).parents('div.photoShell').css("width", $(this).width() + 22);
        $(this).parents('div.photoShell').addClass('loaded');
        $(this).next(".caption").show();

        var scrollNum =  $(this).parents('div.photoShell').offset().top;
        $.scrollTo(scrollNum - 20, {duration: 700, axis:"y"});


            $(this).fadeIn("slow");


}).each(function() {
    // trigger the load event in case the image has been cached by the browser
    if(this.complete) $(this).trigger('load');
});

For some reason, firefox seems to ignore my scrollTo function even though it works in chrome and safari.

Here's an example link: http://blog.rainbird.me/post/2358248459/blowholes-are-awesome

Chrome and Safari will automatically scroll to the top of the image (with an offset of 20 pixels)

It doesn't work in firefox. I'm baffled!

code:

$(document).ready(function() {
$(".photoShell img").lazyload({
placeholder: "http://william.rainbird.me/boston-polaroid/white.gif",
threshold: 200
});


window.viewport =
{
height: function() {
return $(window).height();
},

width: function() {
return $(window).width();
},

scrollTop: function() {
return $(window).scrollTop();
},

scrollLeft: function() {
return $(window).scrollLeft();
}

};

$(".photoShell img").hide();
$(".photoShell .caption").hide();

$(".photoShell img").load(function() {

var maxWidth = viewport.width() - 40; // Max width for the image
if(maxWidth > 960){
    maxWidth = 960;
}
var maxHeight = viewport.height() - 50;    // Max height for the image
var ratio = 0;  // Used for aspect ratio
var width = $(this).width();    // Current image width
var height = $(this).height();  // Current image height

        // Check if the current width is larger than the max
        if(width > maxWidth){
            ratio = maxWidth / width;   // get ratio for scaling image
            $(this).css("width", maxWidth); // Set new width
            $(this).css("height", height * ratio);  // Scale height based on ratio
            height = height * ratio;    // Reset height to match scaled image
            width = width * ratio;    // Reset width to match scaled image

        }

        // Check if current height is larger than max
        if(height > maxHeight){
            ratio = maxHeight / height; // get ratio for scaling image
            $(this).css("height", maxHeight);   // Set new height
            $(this).css("width", width * ratio);    // Scale width based on ratio
            width = width * ratio;    // Reset width to match scaled image
        }


        $(this).parents('div.photoShell').css("width", $(this).width() + 22);
        $(this).parents('div.photoShell').addClass('loaded');
        $(this).next(".caption").show();

        var scrollNum =  $(this).parents('div.photoShell').offset().top;
        $.scrollTo(scrollNum - 20, {duration: 700, axis:"y"});


            $(this).fadeIn("slow");


}).each(function() {
    // trigger the load event in case the image has been cached by the browser
    if(this.complete) $(this).trigger('load');
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十级心震 2024-10-15 15:37:20

不确定

scrollTop: function() {
    return $(window).scrollTop(0);
}

Wasim

试试这个我

Try this I am not sure

scrollTop: function() {
    return $(window).scrollTop(0);
}

with regards

Wasim

暖心男生 2024-10-15 15:37:20

尝试

scrollTop: function() {
    return $(window).animate({ scrollTop: 0 }, "slow");
}

一下

Wasim

Try this

scrollTop: function() {
    return $(window).animate({ scrollTop: 0 }, "slow");
}

with regards

Wasim

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文