jQuery.Cycle 在第一次加载时显示小图像

发布于 2024-08-18 16:46:04 字数 161 浏览 8 评论 0原文

我的 jQuery.cycle 插件有问题。第一次加载页面时(当未缓存图像时),它显示小图像,例如缩略图。您可以在(编辑:抱歉,旧链接)看到它 - 只需等待第二张图片显示即可 - 它很小。重新加载/刷新可以解决这个问题,但这不是真正的解决方案,你知道。

有谁知道这个问题的解决方案是什么?多谢

I've got problem with jQuery.cycle plugin. At first load of page (when imgs aren't cached) it's showing small imgs, like thumbnails. You can see it at (edit: sorry, old link) - just wait when the second img shows - it's small. Reload/refresh solves it, but it's not real solution, you know.

Does anybody know what's the solution of this problem? Thanks a lot

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

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

发布评论

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

评论(6

靖瑶 2024-08-25 16:46:05

我遇到了同样的问题,这让我发疯!我尝试预加载图像,检查所有内容的版本等,但无济于事。似乎解决此问题的唯一方法是在页面上的每张图片上设置宽度和高度,而不是在 CSS 中。

I had the same issue and it was driving me crazy! I tried pre-loading the images, checked versions on everything etc to no avail.. The only thing that seemed to fix this was setting width and height on each picture on the page, rather than in css.

泅人 2024-08-25 16:46:05

我通过在 标记中设置宽度和高度解决了这个问题。但由于我使用脚本来生成图像列表,并且它们有时具有不同的大小,所以我使用了一点 php 来解决这个问题:

function displayimage($filename) {
    $imgsize = getimagesize("$filename");
    echo "<img src=\"$filename\" $imgsize[3] />\n";
}

例如, $imgsize[3] 是, 宽度=“585”高度=“285”

然后,在幻灯片中显示目录中的所有 jpg:

<div class="slideshow">
<?php
foreach (glob("*.jpg") as $filename) {
    displayimage($filename);
}
?>
</div>

输出

<div class="slideshow">
<img src="cat.jpg" width="575" height="256" />
<img src="dog.jpg" width="475" height="350" />
<img src="frog.jpg" width="675" height="300" />
</div>

I solved this by setting the width and height in the <img> tag. But since I'm using a script to generate the list of images, and they sometimes have various sizes, I used a tiny bit of php to solve this:

function displayimage($filename) {
    $imgsize = getimagesize("$filename");
    echo "<img src=\"$filename\" $imgsize[3] />\n";
}

$imgsize[3] is, for example, width="585" height="285".

And then, to show all jpgs in the directory in the slideshow:

<div class="slideshow">
<?php
foreach (glob("*.jpg") as $filename) {
    displayimage($filename);
}
?>
</div>

Output

<div class="slideshow">
<img src="cat.jpg" width="575" height="256" />
<img src="dog.jpg" width="475" height="350" />
<img src="frog.jpg" width="675" height="300" />
</div>
暖风昔人 2024-08-25 16:46:05

虽然可能不能直接回答这个问题(我不知道 jQuery.cycle 插件),但这里的许多答案都解决了加载后处理图像的问题,这也是人们来这里寻求的。所以这就是发生的事情:

由于浏览器端的逻辑错误,导致 24px 出现 - 过早返回占位符控件的尺寸。它们具有不同的控制逻辑来处理缓存图像,IE 甚至不会为它们触发 onload,这就是出现所有混乱的原因。

(width===0) 检查是否加载图像在整个互联网上找到更可靠, .complete DOM 属性很少被提及,显然受到所有主要支持浏览器: http://www.w3schools.com/jsref/prop_img_complete.asp

这个为我工作:

$("img")
    .one('load', onLoadRoutine)
    .each(function() {
        if(!this.complete) {
            return; //.one() will fire eventually
        } else {
            onLoadRoutine({delegateTarget:this}); //fake event object
        }
    });

注意事件模型对象{delegateTarget:this}。最好是 jQuery(this).trigger('load'),但我不能推荐它,因为我当时没有尝试过......

.complete 测试应该解决 24px 占位符控件逻辑错误的情况,即 .one('load').each()。涵盖缓存/远程上的控制流分割。

While possibly not directly answer to the question (the jQuery.cycle plugin is unknown to me), many of the answers here address the problem of processing images after loading and that's also what people come here to seek for. So here it goes what's going on:

The 24px come up due to faulty logic on browser side - returning prematurely dimensions for a placeholder control. They have a different control-logic for handling cached images, IE does not even trigger onload for them, and that's why all the confusion comes up.

More reliable than (width===0) checking to be found all over the internet if an image is loaded, the .complete DOM property seldom mentioned apparently supported by all major browsers: http://www.w3schools.com/jsref/prop_img_complete.asp

this worked for me:

$("img")
    .one('load', onLoadRoutine)
    .each(function() {
        if(!this.complete) {
            return; //.one() will fire eventually
        } else {
            onLoadRoutine({delegateTarget:this}); //fake event object
        }
    });

Note the event mockup object {delegateTarget:this}. Preferable would be jQuery(this).trigger('load'), but I can't recommend it, since I didn't try it out at the time...

the .complete test should solve the case of the faulty 24px placeholder control's logic, the .one('load').each(). covers the control-flow split on cached/distant.

海的爱人是光 2024-08-25 16:46:04

使用 $(window).load(function() { }); 而不是 $(document).ready(function() { });

Use $(window).load(function() { }); rather than $(document).ready(function() { });

迷雾森÷林ヴ 2024-08-25 16:46:04

在图像容器 DIV 上使用 overflow:hidden

Use overflow:hidden on the images container DIV

国粹 2024-08-25 16:46:04

同样的问题。随机图像的内联样式为 24px x 24px。我没有设置每个图像的高度和宽度,而是通过 CSS 来设置。由于我的宽度是固定宽度,所以我设置了它,但将高度设置为 100%:

.ParentDiv img { width: 400px !important; height: 100% !important; }

!important 对于覆盖 jQuery 循环应用的内联样式是必要的。

这似乎也解决了我在 IE8 中遇到的另一个问题。页面可以正常加载,但图像似乎是在加载所有 CSS 和 jquery 之后才加载的。这导致该框无法扩展以适应其中图像的高度和宽度。我想象设置宽度和高度,保存图像的盒子立即调整大小。

Same issue. Random images were getting inline styles of 24px x 24px. Rather than setting each image height and width, I did so through CSS. Since my width was a fixed width, I set that but set the height to 100%:

.ParentDiv img { width: 400px !important; height: 100% !important; }

The !important is necessary to override the inline styles applied by jQuery cycle.

This also seemed to solve another issue I was having with IE8. The page would load fine, but the images seemed to be loading after all the CSS and jquery loaded. This caused the box to not expand to fit the height and width of the images within. I imagine setting the width and height, the box holding the images was sized right off the bat.

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