javascript - 如何遍历类的每个实例并在函数中使用它?

标签 javascript jquery html css

我有一个函数 cropAndCenterImage(),我需要使用它来迭代每个名为 .photo-card-image-wrapper 的类。我知道如何在 jquery 中执行 each() 函数,但是 cropAndCenterImage() 函数的输入参数需要是类的名称,如 cropAndCenterImage(".photo-card-image-wrapper"),它的每个实例。

这是我目前所拥有的:

$(function() {
    $("div.photo-card-image-wrapper").each(function() {
        cropAndCenterImage("every instance of photo-card-image-wrapper class");
    });
});`

编辑

        function cropAndCenterImage(el, size) {
            //el = img wrapper
            //img = img element
            if (size === undefined || size === null) {
                var portfolio = document.getElementById("portfolio").offsetWidth;
                console.log(portfolio);
                if (portfolio < 1200) {
                    size = portfolio / 4;
                } else if (portfolio < 960) {
                    size = portfolio / 3;
                } else {
                    size = portfolio / 5;
                }
            }
            var $el = $(el);
            var $img = $(el + " img");
            console.log($el);
            console.log($img);
            $img.width("auto").height("auto");
            var imgWidth = $img.width();
            var imgHeight = $img.height();
            console.log(imgHeight, imgWidth);

            //hopefully that returns the img to it's default height and width by making the inline style to empty quotes

            if( imgWidth > imgHeight ) {
                //Crop image
              //console.log("width greater than height");
                if ( imgHeight >= size  ) {
                    console.log("hit if");
                    $img.height(size);
                    imgHeight = $img.height();
                    imgWidth = $img.width();
                    $el.height(imgHeight).width(imgHeight);
                } else {
                    console.log("hit else");
                    $el.height(imgHeight).width(imgHeight);
                    $img.height(imgHeight).width(imgWidth);
                }

                //Center image horizontally
                var leftInt = (imgWidth - $el.width()) / 2;
                $img.css({ "margin-left": "-" + leftInt + "px", "margin-top": "0" });
            } else {
                //Crop image
                console.log("height greater than width");
                if ( imgWidth >= size  ) {
                    $img.width(size);
                    imgHeight = $img.height();
                    imgWidth = $img.width();
                    $el.height(imgWidth).width(imgWidth);
                } else {
                    $el.height(imgWidth).width(imgWidth);
                    $img.height(imgHeight).width(imgWidth);
                }
                imgHeight = $img.height();
                //Center image vertically
                var topInt = (imgHeight - $el.height()) / 2;
                //console.log(topInt);
                $img.css({ "margin-top": "-" + topInt + "px", "margin-left": "0"});
            }


        }

HTML

<div class="photo-card-image-wrapper"><a href="images/art1.jpg" class="gallery" data-lightbox="portfolio"><img src="images/art1.jpg" class="art" /></a></div>
<div class="photo-card-image-wrapper"><a href="images/art2.jpg" class="gallery" data-lightbox="portfolio"><img src="images/art2.jpg" class="art" /></a></div>
<div class="photo-card-image-wrapper"><a href="images/art3.jpg" class="gallery" data-lightbox="portfolio"><img src="images/art3.jpg" class="art" /></a></div>
<div class="photo-card-image-wrapper"><a href="images/art4.jpg" class="gallery" data-lightbox="portfolio"><img src="images/art4.jpg" class="art" /></a></div>
<div class="photo-card-image-wrapper"><a href="images/art5.jpg" class="gallery" data-lightbox="portfolio"><img src="images/art5.jpg" class="art" /></a></div>
                    etc

最佳答案

$(function() {
    $("div.photo-card-image-wrapper").each(function() {
        cropAndCenterImage($(this));
    });
});

关于javascript - 如何遍历类的每个实例并在函数中使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46634303/

相关文章:

javascript - Internet Explorer 11中的<audio>标记兼容性

html - CSS类和id之间的处理速度差异

javascript - 可以在按钮的 onclick 事件上运行 PHP 代码还是使用 Ajax?

php - FPDF - 找不到类 'App\Http\Controllers\FPDF'

JavaScript 优化

javascript - 如何在 for 循环的每次传递中增加动画延迟

javascript - 带有 Ref : Document Not Updating With Save 的 Mongo 模式

javascript - 在 Chrome 中创建 MediaStream 对象?

jquery - Blueimp 文件上传 : file is not deleting correctly from queue

jquery - 我可以将我的主页用于带有条件弹出 div 的 404 页面吗?