【问题标题】:How to make this function neater如何让这个功能更整洁
【发布时间】:2013-08-21 09:20:34
【问题描述】:

在使用 JS 和 jQuery-sn-ps 几年后,我编写了自己的小函数来替换 imagefill。我需要为这段代码感到羞耻还是可以?我还能做些什么更好?是否有明显的(在你看来)错误,不是在基本功能上——毕竟它有效——而是在风格上。

function fillsensible(maxWidth, maxHeight, container) {

var img = $(container).find('img');
var container = $(container);
img.css("opacity", "0");

img.load(function() {
    var ratio = 0;
    var containerHeight = container.height();
    var containerWidth = container.width();

    if(img.width() > maxWidth) {
        ratio = maxWidth / img.width();
        img.css("width", maxWidth);
        img.css("height", img.height() * ratio); // Scale height based on ratio
    } else if (img.height() > maxHeight) {
        ratio = maxHeight / img.height();
        img.css("height", maxHeight);
        img.css("width", img.width() * ratio); // Scale height based on ratio
    };

    container.css('position', 'relative');
    img.css('position', 'absolute');
    marginTop = (containerHeight - img.height())/2;
    marginLeft = (containerWidth - img.width())/2;  
    img.css({
      "top": marginTop,
      "left": marginLeft,
      "opacity": "1"
    });
});
}

如果从一开始就不清楚:这会在 DOM 中查找给定容器元素以获取 img,将其与其原始大小成比例地缩放到以像素为单位的给定最大大小,并将其放置在容器的中心。

据我所知,使用背景图像这一切都很容易,但使用 img 元素是不可能的。

感谢您的意见!

【问题讨论】:

标签: jquery image function scaling image-scaling


【解决方案1】:

除了你不需要调整任何img元素的高度

代码可能只是

function fillsensible(maxWidth, maxHeight, container) {

var img = $(container).find('img');
var container = $(container);
img.css("opacity", "0");

img.load(function() {
    var ratio = 0;
    var containerHeight = container.height();
    var containerWidth = container.width();

    if(img.width() > maxWidth) {
        ratio = maxWidth / img.width();
        img.css("width", maxWidth);
        img.css("height", "auto"); // Scale height based on ratio
    } else if (img.height() > maxHeight) {
        ratio = maxHeight / img.height();
        img.css("height", maxHeight);
        img.css("width", "auto"); // Scale height based on ratio
    };

    container.css('position', 'relative');
    marginTop = (containerHeight - img.height())/2;
    marginLeft = (containerWidth - img.width())/2;  
    img.css({
      "position": "absolute"
      "top": marginTop,
      "left": marginLeft,
      "opacity": "1"
    });
});
}

【讨论】:

    【解决方案2】:

    marginTop 和 marginLeft 变量应该在本地声明

    var marginLeft, marginTop;
    

    【讨论】:

    • 你介意告诉我这是为什么吗?我一直认为 javascript vars 的优势在于它们可以被隐式初始化。
    • 如果它们没有用 var 关键字声明,它们就会变成全局的,你可以在其他地方覆盖一些东西。
    【解决方案3】:

    下面的代码更整洁,我删除了多余的代码。

    function fillsensible(maxWidth, maxHeight, container) {
    
        var container = $(container).css('position', 'relative'),
            img = container.find('img').css({"opacity": 0, 'position': 'absolute'});
    
        img.load(function() {
            var ratio = 0,
                containerHeight = container.height(),
                containerWidth = container.width(),
                marginLeft, marginTop;
    
            if(img.width() > maxWidth) {
                ratio = maxWidth / img.width();
                img.css({ 
                    "width": maxWidth,
                    "height": img.height() * ratio
                });
            } else if (img.height() > maxHeight) {
                ratio = maxHeight / img.height();
                img.css({
                    "height": maxHeight,
                    "width": img.width() * ratio
                });
            };
    
            marginTop = (containerHeight - img.height())/2;
            marginLeft = (containerWidth - img.width())/2;  
            img.css({
              "top": marginTop,
              "left": marginLeft,
              "opacity": 1
            });
        });
    } 
    

    【讨论】:

    • 谢谢。我知道你在那里做了什么。
    【解决方案4】:

    一个巧妙的事情是让它成为一个插件。例如:

    (function($) {
    
        function fillsensible(maxWidth, maxHeight, container) {
            // your previous code
        }
    
        $.fn.fillsensible = function(maxWidth, maxHeight) {
            fillsensible(maxWidth, maxHeight, this);
            return this;
        }
    })(jQuery);
    

    所以你可以这样使用它并将其他函数调用链接到容器元素

    $("#container").fillsensible(100, 200);
    

    【讨论】:

    • 如果你打算把它做成一个插件,你应该使用可配置的选项而不是函数参数。
    • 好的,我明白了。这确实更好用。谢谢你的主意。我将探索插件创作的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 2021-05-03
    • 2020-07-29
    相关资源
    最近更新 更多