简体   繁体   English

使用jQuery设置flex css

[英]Setting flex css with jQuery

I"m trying to set the flex css property of image containers to the aspect ratio of the image. I want the flex css to be in form flex: 1.798245 with no px or 1 1 1px. 我试图将图像容器的flex css属性设置为图像的宽高比。我希望flex css处于flex: 1.798245形式flex: 1.798245 ,没有px或1 1 1px。

Using jQuery css() method it keeps setting it to flex: 1 1 1.798245px 使用jQuery css()方法,它将其设置为flex: 1 1 1.798245px

//gallery flex
$('.gallery-item').each(function(){
  var value = $("img", this).width() / $("img", this).height();
  $(this).css("flex", value);
});

How should I pass value to css() to get the result I want? 我应该如何将值传递给css()以获得我想要的结果?

what you want is equivalent to setting the flex-grow attribute: 你想要的相当于设置flex-grow属性:

在此输入图像描述

so what you can do is just set the flex-grow attribute directly: 所以你可以做的就是直接设置flex-grow属性:

$(this).css('flex', value + ' 1 0%');

像这样设置它

 $(this).css("flex", value);

Add px after value variable value变量后添加px

$('.gallery-item').each(function(){
  var value = $("img", this).width() / $("img", this).height();
  $(this).css("flex", value); // You had used `:` but should be `,` to separate two arguments.
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM