Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery-使用父高度进行垂直居中。某种程度上_Jquery_Css_Image_Center - Fatal编程技术网

Jquery-使用父高度进行垂直居中。某种程度上

Jquery-使用父高度进行垂直居中。某种程度上,jquery,css,image,center,Jquery,Css,Image,Center,因此,我有X个图像要垂直居中于a(下面jquery中的类名是实际的)。 这很有效,但是。。。它仅获取第一个值并将其应用于其余图像,导致第一个图像居中,而其他图像不居中 我可以补充一点,所有的图像都有不同的高度和宽度。从现在开始,只需使用text align:center $(window).load(function(){ var parent_height = $('.tv-list-channel-logo').parent().height(); var image_hei

因此,我有X个图像要垂直居中于a
    • (下面jquery中的类名是实际的
      )。
      这很有效,但是。。。它仅获取第一个值并将其应用于其余图像,导致第一个图像居中,而其他图像不居中

      我可以补充一点,所有的图像都有不同的高度和宽度。从现在开始,只需使用
      text align:center

      $(window).load(function(){
          var parent_height = $('.tv-list-channel-logo').parent().height();
          var image_height = $('.tv-list-channel-logo').height();
          var top_margin = (parent_height - image_height)/2;
          $('.tv-list-channel-logo').css( 'margin-top' , top_margin);
      });
      

      我已经盯着它看了很长一段时间了,我肯定错过了一些明显的东西。

      如果图像需要不同的值,你需要在它们之间循环。尝试使用
      $('.tv list channel logo')。每个(函数…
      )。在
      内部。每个
      引用当前元素

      编辑:这里有一个
      的示例。每个

      // `.each` takes each selected element and calls the callback on each one
      $('.tv-list-channel-logo').each( function( ) {
          // `this` refers to the current .tv-list-channel-logo element
          var parent_height = $(this).parent().height();
          var image_height = $(this).height();
          var top_margin = (parent_height - image_height)/2;
          $(this).css( 'margin-top' , top_margin);
          // I just replaced all instances of `'.tv-list-channel-logo'` with `this`
      } );
      

      你试过把它们完全放在一个相对的容器里吗?是的,我试过了,但我应该在我的第一篇文章中提到,这些图像的高度和宽度都不同。请使用我的建议以及下面的答案
      Ryan P
      。循环浏览每个图像并使用
      每个
      执行此计算我不太确定这是否是您的意思,但它不起作用<代码>var image_height=$('.tv list channel logo')。each().height()我的脑子里肯定有一些有价值的东西。我搞不懂这个…我今天早上在电话里接的。。。现在在工作,所以我给你打了个例子。