jquery 图像大小调整和性能

发布于 2024-09-25 16:55:06 字数 926 浏览 5 评论 0原文

我这里有一个很奇怪的问题 - 我正在使用 jquery 调整背景图像的大小(png,宽度:1793,高度:1200,大小:872kb)。

我的函数在这里:

bgInit = function(img, clbk) {
var bgObj = $('#bgImg');
var bgHeight = bgWidth = 0;
    bgObj.attr('src',img).ready(function(){
    var bgRatio = bgObj.height()/bgObj.width();
        if (bgHeight < screen.height) {
        bgHeight = screen.height;
        bgWidth = bgHeight/bgRatio;
        }
        if (bgWidth < screen.width) {
        bgWidth = screen.width;
        bgHeight = bgWidth*bgRatio;
        }
    //resize and center horizontally
    bgObj.height(bgHeight).width(bgWidth).css('margin-left',(screen.width-bgWidth)/-2);
    clbk();
    });
}

这就是我的调用方式:

bgInit('img/bg.png', function(){
alert('done!');
});

函数在所有浏览器中都能正常工作,但问题是在调整大小后使用淡入淡出效果时。它确实很延迟 - Opera 没有问题,但我想说 IE 中只有 2fps。

有没有更好的方法来进行这种调整大小(保持纵横比至关重要)?

提前致谢,

米克

I have quite an odd problem here - I am using jquery for resizing my background image (png, width: 1793, height: 1200, size: 872kb).

My function is here:

bgInit = function(img, clbk) {
var bgObj = $('#bgImg');
var bgHeight = bgWidth = 0;
    bgObj.attr('src',img).ready(function(){
    var bgRatio = bgObj.height()/bgObj.width();
        if (bgHeight < screen.height) {
        bgHeight = screen.height;
        bgWidth = bgHeight/bgRatio;
        }
        if (bgWidth < screen.width) {
        bgWidth = screen.width;
        bgHeight = bgWidth*bgRatio;
        }
    //resize and center horizontally
    bgObj.height(bgHeight).width(bgWidth).css('margin-left',(screen.width-bgWidth)/-2);
    clbk();
    });
}

And this is how I'm calling it:

bgInit('img/bg.png', function(){
alert('done!');
});

Function works fine in all browsers, but the problem is when it comes to using fade effects after resizing. It's really laggy - Opera has no problems, but 2fps in IE I'd say.

Is there any better way for doing this kind of resizing (maintaining aspect ratio is crucial)?

Thanks in advance,

Mikk

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

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

发布评论

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

评论(1

等风来 2024-10-02 16:55:06

Internet Explorer JavaScript 引擎与浏览器核心分离,与其他现代浏览器相比运行速度非常慢。由于 Javascript 是动态调整大小的唯一方法,因此您对 IE 的运气不太好。我自己的网站也遇到同样的问题。而且我的脚本不是基于 jQuery,它是高度优化的。

您可以做的是使用谷歌插件将 IE javascript 引擎加速大约 10 倍,您可以在您的网站中包含一个简单的脚本,如果客户愿意,可以帮助他们安装它 - http://code.google.com/chrome/chromeframe/

在此处查看网络开发资源 - http://www.chromium.org/developers/how-tos/chrome-frame-getting-started< /a>

查看一些最近的性能测试结果以了解我在说什么:http://www .legitreviews.com/article/1347/1/

Internet explorer JavaScript engine is separate from the browser core and runs very slowly compared to other modern browsers. Since Javascript is the only way to dynamically resize, you are out of luck with IE. My own site suffers from the same issue. And my script is not based on jQuery, it is highly optimized.

What you can do is use a google plugin to speed up IE javascript engine around 10X times, you can include a simple script in your site that will help customers to install it if they want to - http://code.google.com/chrome/chromeframe/

Check out web dev resources here - http://www.chromium.org/developers/how-tos/chrome-frame-getting-started

See some recent performance test results to know what I am talking about: http://www.legitreviews.com/article/1347/1/

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