Jquery 循环 + Firefox 压缩图像

发布于 2024-08-10 11:20:36 字数 1707 浏览 3 评论 0原文

我正在为图片库运行 jQuery Cycle。查看链接:此处

我的问题是在 Firefox 中查看时,图像会被压扁。当我重新加载页面时,问题就消失了。这让我相信 Javascript 在加载所有图像之前触发(通常第一个图像工作正常,其余图像被压扁。)

硬刷新会重现该问题。

我已将所有内容包装在 $(document).ready(function(){ }); 中但它仍然发生。

其他信息:如果我指定图像的宽度和高度,一切正常。然而,有数百张不同尺寸的图像......

我对这个问题感到非常沮丧。非常感谢任何想法/帮助!

这是我的代码:

$(document).ready(function(){
//function onBefore(curr,next,opts) {
//    var $slide = jQuery(next);
//    var w = $slide.outerWidth();
//    var h = $slide.outerHeight();
//    $slide.css({
//        marginTop: (482 - h) / 2,
//        marginLeft: (560 - w) / 2
//    });
//};

// Decare the function that center the images...
function onBefore(curr,next,opts) {
    var $slide = jQuery(next);
    var w = $slide.outerWidth();
    var h = $slide.outerHeight();
    $slide.css({
        marginTop: (480 - h) / 2,
        marginLeft: (560 - w) / 2
    });
};


$(document).ready(function() {
    $('#slideshow').cycle({
     fx:     'fade', 
    next:   '#next', 
    pause: 0,
    speed: 500,
    before: onBefore,
    prev:   '#prev',
    pause:  '#pause',
    pager:  '.thumbs',
    pagerClick:function(zeroBasedSlideIndex, slideElement) {$(slideElement).find('div.cover').hide();},
    pagerAnchorBuilder: function(idx, slide) {
                        var src = $('img',slide).attr('src');
                        //Change height of thumbnail here
                         return '<li><a href="#"><img src="' + slide.src + '" height="90" /></a></li>'; 
                    
                } 
    });});});

I am running jQuery Cycle for an image gallery. View the link: Here

My problem is that the images are getting squished when viewed in firefox. The problem disappears when I re-load the page. This leads me to believe that the Javascript is triggering before all the images are loaded (usually the first image works fine and the rest are squished.)

A hard re-fresh reproduces the problem.

I've wrapped everything in a $(document).ready(function(){ }); but it still happens.

Additional Info: If I specify the width and height of the image, everything works fine. However there are hundreds of images all at different sizes..

I'm pretty frustrated with this problem. Any ideas/help is greatly appreciated!

Here is my code:

$(document).ready(function(){
//function onBefore(curr,next,opts) {
//    var $slide = jQuery(next);
//    var w = $slide.outerWidth();
//    var h = $slide.outerHeight();
//    $slide.css({
//        marginTop: (482 - h) / 2,
//        marginLeft: (560 - w) / 2
//    });
//};

// Decare the function that center the images...
function onBefore(curr,next,opts) {
    var $slide = jQuery(next);
    var w = $slide.outerWidth();
    var h = $slide.outerHeight();
    $slide.css({
        marginTop: (480 - h) / 2,
        marginLeft: (560 - w) / 2
    });
};


$(document).ready(function() {
    $('#slideshow').cycle({
     fx:     'fade', 
    next:   '#next', 
    pause: 0,
    speed: 500,
    before: onBefore,
    prev:   '#prev',
    pause:  '#pause',
    pager:  '.thumbs',
    pagerClick:function(zeroBasedSlideIndex, slideElement) {$(slideElement).find('div.cover').hide();},
    pagerAnchorBuilder: function(idx, slide) {
                        var src = $('img',slide).attr('src');
                        //Change height of thumbnail here
                         return '<li><a href="#"><img src="' + slide.src + '" height="90" /></a></li>'; 
                    
                } 
    });});});

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

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

发布评论

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

评论(7

和影子一齐双人舞 2024-08-17 11:20:37

我用一个比已经提出的更简单、更清晰的解决方案来解决这个问题:

使用 jQuery,您需要使用 $(window).load 而不是 $(document ).为您的特定情况做好准备。要解决此问题,请将以下内容更改

$(document).ready(function() {
  $('#slideshow').cycle({
    /* ... */
  });
});

$(window).load(function() {
  $('#slideshow').cycle({
    /* ... */
  });
});

:为什么这有效?因为 window.onload 在加载页面上所有引用的图像后触发(请参阅 https://developer.mozilla.org/en/DOM/window.onload.load() - jQuery API),这是您的情况下所需的行为。 $(document).ready,更广为人知的名称是“DOM Ready”,将在图像加载之前触发。这通常是所需的行为,但在您的情况下还为时过早。

There is a much simpler and cleaner solution that I used to solve this problem than what has already been proposed:

Using jQuery, you need to use $(window).load instead of $(document).ready for your particular situation. To fix the issue, change this:

$(document).ready(function() {
  $('#slideshow').cycle({
    /* ... */
  });
});

To this:

$(window).load(function() {
  $('#slideshow').cycle({
    /* ... */
  });
});

Why does this work? Because window.onload fires after all referenced images on the page are loaded (See https://developer.mozilla.org/en/DOM/window.onload, and .load() - jQuery API), which is the desired behavior in your situation. $(document).ready, better known as "DOM Ready", will fire before images have loaded. This is typically the desired behavior, but in your situation it's too early.

夏尔 2024-08-17 11:20:37

几个月前我在网站上工作时遇到了同样的问题(链接如下)。如果您在 $(document).ready() 中开始循环,则当客户端浏览您的页面时会发生以下情况:

1) 客户端浏览器为每个 img 发送一个请求代码>元素。这些请求需要不同的时间才能完成。

2) 在图像请求完成之前,循环开始。 Cycle 的工作原理是隐藏幻灯片放映中除第一张图像之外的所有图像:它在每个图像上设置 visibility:hiddendisplay:none

问题是 Firefox 在显示样式设置为 none 时一劳永逸地修复了 img 元素的大小。因此,如果图像尚未完成加载,则其高度和宽度样式属性会很小(我不确定它们到底对应什么 - 也许是 Firefox 图像占位符的大小)。当cycle通过将其样式属性设置为display:block来显示图像时,它会使用隐藏时的任何尺寸。

我通过更改代码解决了这个问题,以便在所有图像加载完成之前它不会启动循环插件。为此,我将一个计数器变量初始化为我正在循环的图像数量,然后将加载事件绑定到每个图像,如下所示:

var imagesRemaining = 12; // 12 is just the number of images in the slideshow div

$(document).ready(function() {
    $('#slideshow > img').bind('load', function(e) {
        imagesRemaining = imagesRemaining - 1;
        if (imagesRemaining == 0) {
            // I'm doing some other stuff when initializing cycle
            startCycle();
            // My images all start with visibility:hidden so they don't show
            // before cycle hides them in a 'stack', so ...
            $('#slideshow > img').css('visibility', 'visible');
        }
    });
});

function onBefore(curr, next, opts) { // Your code here ... }

function startCycle() {
    $('#slideshow').cycle({ ... // your initialization here });
}

您可以通过查看 此网站(在 Firefox 中)。我正在动态构建图库页面,因此它的结构与您的页面略有不同,但如果您使用 Firebug 浏览,您可以看到更多详细信息。

I had the same problem when working on a site several months ago (linked below). If you're starting cycle in $(document).ready(), here's what happens when a client browses to your page:

1) The client's browser sends a request for each img element. Those requests take variable amounts of time to fulfill.

2) Before the image requests are completed, cycle starts. Cycle works by hiding all but the first image in the slide show: it sets visibility:hidden and display:none on each of its images.

The problem is that Firefox fixes the img element's size once and for all at the point the display style is set to none. So if the image hasn't finished loading, its height and width style attributes are small (I'm not sure exactly what they correspond to - perhaps the size of Firefox's image placeholder). When cycle shows the image by setting its style attribute to display:block, it uses whatever dimensions it had at the time it was hidden.

I solved this by changing my code so that it doesn't start the cycle plugin until all the images are finished loading. To do that, I initialize a counter variable to the number of images I'm cycling, then bind a load event to each image like this:

var imagesRemaining = 12; // 12 is just the number of images in the slideshow div

$(document).ready(function() {
    $('#slideshow > img').bind('load', function(e) {
        imagesRemaining = imagesRemaining - 1;
        if (imagesRemaining == 0) {
            // I'm doing some other stuff when initializing cycle
            startCycle();
            // My images all start with visibility:hidden so they don't show
            // before cycle hides them in a 'stack', so ...
            $('#slideshow > img').css('visibility', 'visible');
        }
    });
});

function onBefore(curr, next, opts) { // Your code here ... }

function startCycle() {
    $('#slideshow').cycle({ ... // your initialization here });
}

You can see it in action by viewing the galleries on this site in Firefox. I'm building the gallery pages dynamically, so it's structured a bit differently than your page, but you can see more details if you poke around with Firebug.

帅气称霸 2024-08-17 11:20:37

我还想补充一点,似乎添加宽度和高度属性可以解决这个问题。

I'd also like to add that it seems adding a width and height attribute solves this problem.

妥活 2024-08-17 11:20:37

好吧,我知道这可能是一种糟糕的调用 load 的方式,但我只是可以将我的循环代码绑定到 .load ,由于某种原因它不起作用,所以我在...中调用了整个 Cycle 初始值设定项...

我无法强制大小因为我正在循环浏览包含动态图像和数据的 li,

所以它可能在某种程度上存在缺陷,但对于那些像我一样绝望的人......

Ok i know its probably an awfull way of calling load but i just coulnd bind my cycle code to .load for some reason it just don't work so i called the whole Cycle initializer inside the ...

i couldn't force the sizes since i'm cycling through li containing dynamic images and data

its probably flawed at some extend but for those as desperated as me...

绝不服输 2024-08-17 11:20:37

乔希,你的解决方案让我不再头痛,非常感谢你!

我想我已经稍微修改了它,以便处理您不知道图像总数的页面。它似乎对我来说工作得很好,如果有人发现任何缺陷,请指出它们 - 我仍在学习。

$(document).ready(function () {
    $('#slideshow > img').each(
        function go() {
            $(this).bind('load', function (e) {
                projects();
                $('#slideshow > img').css('visibility', 'visible');
            });
         });
    });

function projects() {
    $('#slideshow').cycle({
        fx: 'scrollHorz',
        speed: 300,
        timeout: 0,
        next: '#next ',
        prev: '#prev ',
        after: onAfter,
        nowrap: 1,
        autostop: 1 
    });
}

Josh, your solution has just saved me a headache, thank you very much!

I think i've amended it slightly in order to handle pages where you don't know the total number of images. It seems to be working fine for me, if anyone can see any flaws, please point them out - i'm still learning.

$(document).ready(function () {
    $('#slideshow > img').each(
        function go() {
            $(this).bind('load', function (e) {
                projects();
                $('#slideshow > img').css('visibility', 'visible');
            });
         });
    });

function projects() {
    $('#slideshow').cycle({
        fx: 'scrollHorz',
        speed: 300,
        timeout: 0,
        next: '#next ',
        prev: '#prev ',
        after: onAfter,
        nowrap: 1,
        autostop: 1 
    });
}
爱的十字路口 2024-08-17 11:20:37

如果您使用数据库来填充幻灯片,您可以尝试从图像本身访问图像尺寸。

例如,使用 django,您可以

 width="{{ xxx.image.width }}px"  height="{{ xxx.image.height }}px" 

在 img 标签中使用。

If you're using a database to populate the slideshow you could try accessing the image dimensions from the image itself.

For example, using django you can use

 width="{{ xxx.image.width }}px"  height="{{ xxx.image.height }}px" 

in your img tag.

乱世争霸 2024-08-17 11:20:37

您可以使用类似于使 YouTube 视频响应的解决方案。您需要知道宽度与高度的比率,并将其添加为循环 div 的 padding-bottom。对于我的 1024X680 照片,我使用了 680/1024 = 66.4%

对于你的情况,我相信

#slideshow{
底部填充:66.4%;
}

将显示未缩小的图像。我不知道您正在使用的实际高度和宽度值是多少,因此请替换您自己的值。当 $(window).load 解决方案被证明极其无效时,我不得不使用这个解决方案 - 所以现在我同时使用这两个解决方案。

这比设置图像的尺寸更好,因为它会滑入一个流动的、响应灵敏的环境中。

You can use a solution similar to making youtube videos responsive. You need to know the ratio of your width to height, and add that as padding-bottom to the cycling div. For my 1024X680 photos, I used 680/1024 = 66.4%

In your case, I believe

#slideshow{
padding-bottom:66.4%;
}

will show the image unshrunk. I have no idea what the actual height and width values you are working with, so substitute your own. I had to use this solution when the $(window).load solution proved maddeningly ineffective -- so now I use both.

This is better than setting the dimensions of the image, because it's slides into a fluid, responsive enviroment.

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