简体   繁体   English

jQuery调整了类准备好文档上所有图像的大小

[英]jQuery resize all images on document ready by class

I have a page with images which I want them all to be resized (by acquiring certain CSS classes) on document ready using jQuery. 我有一个包含图像的页面,我希望在使用jQuery的文档上调整它们的大小(通过获取某些CSS类)。 The images have an initial ".test" which has no attributes in the CSS file. 这些图像的初始“ .test”在CSS文件中没有任何属性。 I know how to give them the new classes so they can be resized. 我知道如何给他们新的课程,以便他们可以调整大小。 But, I want to resize depending on width or height so they can all look the same. 但是,我想根据宽度或高度调整大小,以便它们看起来都一样。

My problem is that I cannot get their width/height to proceed. 我的问题是我无法获取其宽度/高度。 My code is this: 我的代码是这样的:

$(document).ready(function(){

    $('.test').each(function(){
        var img_width = $(this).width();
        var img_height = $(this).height();
        alert("My width:" + img_width + "px, My Height: " + img_height + "px");

        if (img_width > img_height)
        {
            //resize to height
        }else{
            //resize to width
        }
    });
})

The alert I have before the if statement always returns 0 - although it does appear the correct number of times. 我在if语句之前收到的警报始终返回0-尽管它确实显示了正确的次数。

I also tried getting the width (and also the height) with $(this).width; 我还尝试使用$(this).width;获得宽度(以及高度) $(this).width; and $(this).offsetHeight; $(this).offsetHeight; and $(this).attr('width'); $(this).attr('width'); but when the result is not 0, it's undefined . 但是,如果结果不为0,则为undefined

What am I doing wrong? 我究竟做错了什么?

Use 采用

$(window).load(function() {

instead of 代替

$(document).ready(function(){

The load event is triggered a bit later when all the images have actually loaded. 当所有图像都实际加载后,将触发加载事件。

use jQuery's .load() method to attach load event handlers to the window or to more specific items, like images. 使用jQuery的.load()方法将加载事件处理程序附加到窗口或图像等更特定的项目。

Source: http://api.jquery.com/ready/ 资料来源: http//api.jquery.com/ready/

$(document).ready(function(){

    $('.test').load(function(){
        var img_width = $(this).width();
        var img_height = $(this).height();
        alert("My width:" + img_width + "px, My Height: " + img_height + "px");

        if (img_width > img_height)
        {
            //resize to height
        }else{
            //resize to width
        }
    });
})

您还可以签出SLIR: https : //github.com/lencioni/SLIR

I ran across this problem a while ago too. 不久前,我也遇到了这个问题。 The problem is that the images are not fully loaded yet. 问题是图像尚未完全加载。

However, I would suggest NOT to do it this way. 但是,我建议不要这样做。 Loading a big picture, then resize it clientside is not a good method. 加载大图,然后在客户端调整大小不是一个好方法。 You should consider resizing it serverside so users don't have to download large images for no use. 您应该考虑在服务器端调整它的大小,以便用户不必下载大图像也不用。

You should take a look at phpThumb, which is a great PHP-class that resizes and caches the images for you. 您应该看看phpThumb,它是一个很棒的PHP类,可以为您调整大小并缓存图像。 It's a great tool and a much better way to achieve this. 这是一个很好的工具,也是实现这一目标的更好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM