:: jQuery :: 分割CSS值的插入部分,并将值分配给上、右、下和左

huangapple go评论65阅读模式
英文:

:: jQuery :: Split CSS value of inset and assign values to top, right, bottom and left

问题

Sure, here's the translated CSS code:

top: 8.01562%,
right: auto,
bottom: 3%,
left: 7.8047%

Please note that this is the CSS code extracted from your provided content.

英文:

I am getting CSS inset (absolute position) value to div dynamically like below.

inset: 8.01562% auto 3% 7.8047%

From above, I want to assign as CSS:

  • first value (8.01562%) to top
  • second value to right
  • third value to bottom
  • fourth value to left

jsFiddle

Like below:

top: 8.01562%,
right: auto,
bottom: 3%,
left: 7.8047%

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var __top = jQuery(&#39;.wrapper&#39;).css(&#39;inset&#39;)[0]
var __right = jQuery(&#39;.wrapper&#39;).css(&#39;inset&#39;)[1]
var __bottom = jQuery(&#39;.wrapper&#39;).css(&#39;inset&#39;)[2]
var __left = jQuery(&#39;.wrapper&#39;).css(&#39;inset&#39;)[3]

jQuery(&#39;.wrapper&#39;).css(&#39;inset&#39;, &#39;&#39;)

jQuery(&#39;.wrapper&#39;).css(&#39;top&#39;, __top);
jQuery(&#39;.wrapper&#39;).css(&#39;right&#39;, __right);
jQuery(&#39;.wrapper&#39;).css(&#39;bottom&#39;, __bottom);
jQuery(&#39;.wrapper&#39;).css(&#39;left&#39;, __left);

<!-- language: lang-css -->

.wrapper{position:absolute;}

<!-- language: lang-html -->

&lt;div class=&quot;wrapper&quot; style=&quot;inset: 8.01562% auto 3% 7.8047%&quot;&gt;My content&lt;/div&gt;


&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案1

得分: 1

The "inset" property is a shorthand property. To get the values set in your element with "inset," you have to use the underlying four properties:

var __top = jQuery('.wrapper').css('top');
var __right = jQuery('.wrapper').css('right');
var __bottom = jQuery('.wrapper').css('bottom');
var __left = jQuery('.wrapper').css('left');

You can get all of the properties at once if you like:

var props = jQuery('.wrapper').css(["top", "right", "bottom", "left"])
var __top = props.top;
var __right = props.right;
var __bottom = props.bottom;
var __left = props.left;

As per the jQuery documentation for .css(), retrieval of shorthand properties is not guaranteed. The underlying component properties should be retrieved, as in this answer.

英文:

The "inset" property is a shorthand property. To get the values set in your element with "inset", you have to use the underlying four properties:

var __top = jQuery(&#39;.wrapper&#39;).css(&#39;top&#39;);
var __right = jQuery(&#39;.wrapper&#39;).css(&#39;right&#39;);
var __bottom = jQuery(&#39;.wrapper&#39;).css(&#39;bottom&#39;);
var __left = jQuery(&#39;.wrapper&#39;).css(&#39;left&#39;);

You can get all of the properties at once if you like:

var props = jQuery(&#39;.wrapper&#39;).css([&quot;top&quot;, &quot;right&quot;, &quot;bottom&quot;, &quot;left&quot;])
var __top = props.top;
var __right = props.right;
var __bottom = props.bottom;
var __left = props.left;

As per the jQuery documentation for .css(), retrieval of shorthand properties is not guaranteed. The underlying component properties should be retrieved, as in this answer.

huangapple
  • 本文由 发表于 2023年5月7日 19:55:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定