5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JQuery - 画像サムネイル

Last updated at Posted at 2013-02-24
    /**
     * 画像の縦横比を変えずに画像縮小をします。
     * @param max_width 横幅最大値
     * @param max_height 縦幅最大値
     */
    $.fn.thumbnailImg = function(option){
        $(this).each(function(){
            var h = $(this).height();
            var w = $(this).width();
            var hRange = option.max_height / h;
            var wRange = option.max_width / w;
            if ( hRange > wRange && w > option.max_width) {
                $(this).attr("width", option.max_width);
            } else if (h > option.max_height) {
                $(this).attr("height", option.max_height);
            } else {
                $(this).attr({"height":option.max_height, "width":option.max_width});
            }
        }).show();
    };

使い方

    $('img').thumbnailImg({'max_width':100, 'max_height':100});
5
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?