【问题标题】:Run jQuery function on each element in a class对类中的每个元素运行 jQuery 函数
【发布时间】:2011-11-10 22:35:43
【问题描述】:

我有一个函数可以根据某些标准调整图像大小,并希望它单独计算 .z 类中的所有元素,然后返回它们。

使用.each 通过函数运行所有图像,但返回所有图像的相同尺寸。不知道我在哪里搞砸了......

测试站点在这里:

http://brantley.dhut.ch/

(还必须在图像完全调整大小之前解决淡入问题,但这将是另一个问题)

JavaScript:

jQuery(function($) {
    $('.z').respond();
});

(function($){
    $.fn.respond = function() {

        /* initialize function on window load and resize */
        $(document).ready(function() {
            dimensions();
        });
        $(window).load(function() {     
            dimensions();
        });
        $(window).resize(function() {
            dimensions();
        });

        /* declare affected elements */
        var pic = this

        /* set image's height or width depending on the browser window's size */
        function dimensions() {
            return pic.each(function() {

            /* declare variables */
            var browserWidth = $(window).width();
            var imgRatio = pic.width() / pic.height()
            var availableHeight = ($(window).height() - $('header').height() - $('footer').height() - 80)
            var browserRatio = browserWidth / availableHeight

            /* image sizing logic */
            if (browserRatio >= imgRatio) {
                if (browserWidth > 640) {
                    /* if the browser is landscape and bigger than mobile, set the image's height to fill the available space */
                    pic.height(availableHeight).width('auto');
                    //$('body').css('background', 'blue');
                } else {
                    /* it's mobile */
                    pic.width(browserWidth - 40).height('auto');
                    //$('body').css('background', 'red');
                }
            } else {
                /* if the browser is portrait, set the image's width to fill the page */
                pic.width(browserWidth - 40).height('auto');
                //$('body').css('background', 'green');
            }

            /* horizontally center content */
            pic.css('margin-left', (browserWidth - pic.width())/2);

            });

        };

    };
})( jQuery );

【问题讨论】:

    标签: jquery class function dynamic each


    【解决方案1】:

    您正在使用pic.each(),并且在应该在 jquery 对象“pic”选择的每个元素上调用的函数内部,您也在使用“pic”。尝试在那里使用this

    return pic.each(function() {
            /* declare variables */
            var browserWidth = $(window).width();
            var imgRatio = $(this).width() / $(this).height()
    

    【讨论】:

      【解决方案2】:
      function dimensions() {
                  return pic.each(function() {
      
                  /* declare variables */
                  var browserWidth = $(window).width();
                  var imgRatio = $(this).width() / $(this).height()
                  var availableHeight = ($(window).height() - $('header').height() - $('footer').height() - 80)
                  var browserRatio = browserWidth / availableHeight
      
                  /* image sizing logic */
                  if (browserRatio >= imgRatio) {
                      if (browserWidth > 640) {
                          /* if the browser is landscape and bigger than mobile, set the image's height to fill the available space */
                          $(this).height(availableHeight).width('auto');
                          //$('body').css('background', 'blue');
                      } else {
                          /* it's mobile */
                          $(this).width(browserWidth - 40).height('auto');
                          //$('body').css('background', 'red');
                      }
                  } else {
                      /* if the browser is portrait, set the image's width to fill the page */
                      $(this).width(browserWidth - 40).height('auto');
                      //$('body').css('background', 'green');
                  }
      
                  /* horizontally center content */
                  $(this).css('margin-left', (browserWidth - $(this).width())/2);
      
                  });
      
              };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-02
        相关资源
        最近更新 更多