【问题标题】:Sad tomato ( Image .load function in jQuery )悲伤的番茄(jQuery 中的 Image .load 函数)
【发布时间】:2012-07-16 18:12:44
【问题描述】:

我有一个非常难过的番茄,因为它没有调整大小,可怜的东西。如果我不放“加载”功能,那么我的一些拇指将不会调整大小,如果我这样做,那么其他拇指将不会调整大小。

startDiv.find('img').each(function(p) {
    $(this).parent().css({ height: thumbs_size, width:thumbs_size });
    $(this).css({ height: thumbs_size, width: thumbs_size }); //temporary
    $(this).load(function() {
        if ($(this).height() > $(this).width()) {
            var w = thumbs_size;
            var h = Math.ceil($(this).height() / $(this).width() * thumbs_size);
        } else {
            var h = thumbs_size;
            var w = Math.ceil($(this).width() / $(this).height() * thumbs_size);
        }
        $(this).css({ height: h, width: w });
    });
});

谁能拯救我的番茄? 谢谢! :)

【问题讨论】:

标签: jquery image load thumbnails


【解决方案1】:

这是因为调用 $(this).load( 时可能已经加载了一些。执行以下操作或类似操作

startDiv.find('img').each(function(p) {
    $(this).parent().css({ height: thumbs_size, width:thumbs_size });
    $(this).css({ height: thumbs_size, width: thumbs_size }); //temporary

    var $this = $(this);
    function onLoad() {
        var height = $this.height(), width = $this.width();
        var isPortrait = height > width;
        var w = isPortrait ? thumbs_size : Math.ceil(width / height * thumbs_size);
        var h = isPortrait ? Math.ceil( height / width * thumbs_size) : thumbs_size;
        $this.css({ height: h, width: w });
    }
    // Set it on the load event handler in case it's not loaded
    // If it has a width, it's already loaded
    if ($(this).width() > 0) {
        onLoad();
    } else {
       $this.load(onLoad);
    }
});

或者您可以在 onload 处理程序中使用它,以保证 DOM 中的所有图像都已加载(也包括所有脚本)

$(window).load(function(){
    startDiv.find('img').each(function(p) {
        $(this).parent().css({ height: thumbs_size, width:thumbs_size });
        var height = $this.height(), width = $this.width();
        var isPortrait = height > width;
        $this.css({ 
            height: isPortrait ? Math.ceil( height / width * thumbs_size) : thumbs_size,
            width: isPortrait ? thumbs_size : Math.ceil(width / height * thumbs_size) 
        });
    });    
});

您可能希望使用 CSS 为图像设置默认宽度和高度

【讨论】:

  • 这段代码看起来应该可以工作,我试过了,但结果有些奇怪。中途加载的图像的宽度和高度是否会成为问题?如果您在 Firefox 中 shift-reload 几次,您会看到警报不会总是返回相同的宽度和高度。这里:instancia.net/?p=104
  • jsfiddle 对我有用,你在观察什么?什么浏览器?
  • 两种浏览器都会随机执行:不调整图像大小,获取错误的 w、h 值等。我认为在 Fiddle 中很难看到,因为它正在缓存。在 Firefox 中使用 shift reload 检查我的页面链接(其中包含您的代码)。同样的问题存在于 Wordpress 页面之外,但加载时间在 Wordpress 中较慢,因此显示更多。
  • @JenniferMichelle 另一种解决方案是将所有内容添加到窗口的加载处理程序中,该处理程序在所有图像加载后运行,而不是在 DOM 准备好后运行,请参阅编辑
  • 嗯,突然间我无法再在 Wordpress 之外重现该问题,所以 Wordpress 添加的乱码类中可能还有第二个问题来帮助我! :) 让我们看看我是否可以删除它们...
【解决方案2】:

更新: 尽管上述解决方案适用于任何独立的 html 页面,但 Wordpress 处理一切的方式并不相同,所以对于 Wordpress,我使用了这个:

-- 在functions.php中加入这个javascript: https://github.com/alexanderdickson/waitForImages

-- 定义你的 startDiv,例如:startDiv = $(".myThumbnailDiv");。

-- 在你的 ready 函数中,添加如下内容:

//Thumbnail parent container size, so they load within a thumbsize box
startDiv.find('img').each(function(p) {
    $(this).parent().css({ height: thumbs_size, width:thumbs_size });
});

//Call the function in waitingforimages.js 

startDiv.waitForImages(function() {
   //alert('All images have loaded.');
}, function(loaded, count, success) {

        var height = $(this).height(), width = $(this).width();
        var isPortrait = height > width;
        $(this).css({
            height: isPortrait ? Math.ceil( height / width * thumbs_size) : thumbs_size,
            width: isPortrait ? thumbs_size : Math.ceil(width / height * thumbs_size)
        });
});

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
相关资源
最近更新 更多