Safari 16.4 和 iOS 16.4 自行更改动态设置的 CSS 属性。

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

Safari 16.4 and iOS 16.4 change dynamically set CSS property by it self

问题

在Safari 16.4中,background-size属性的值中自动添加了'auto',而background-position属性只显示了一个值。尝试将CSS属性background-size和background-position的值分开设置,确保每个属性都以正确的方式被识别:

var li = $('<li class="item" data-value="' + i + '"></li>').css({
    'background-size': (gridSize * 100) + '%',
    'background-position-x': xpos,
    'background-position-y': ypos
});

通过将background-position-x和background-position-y分开设置,可以避免Safari 16.4中的问题,并确保两个属性的值都被正确应用。

英文:

i made a javascript jigsaw game a couple of years ago and it was working perfectly.
recently after iOS 16.4 and Safari 16.4 update the code just doesn't work in the same way and after investigating I found the problem but I don't know how to fix it.

in my Code i set the 2 CSS properties (background-size & background-position) dynamically like this with the values of (gridSize,xpos and ypos) are set before in the code:

            var li = $(&#39;&lt;li class=&quot;item&quot; data-value=&quot;&#39; + (i) + &#39;&quot;&gt;&lt;/li&gt;&#39;).css({
                &#39;background-size&#39;: (gridSize * 100) + &#39;%&#39;,
                &#39;background-position&#39;: xpos + &#39; &#39; + ypos,
            });

now inspecting the li element on safari 16.4 is showing this:

background-size: 300% auto; background-position: 0%;

with 'auto' added to background-size property & background-position property has only one value (despite the fact that i set each one of them in the code as a string)

so it is supposed to be like this:

background-size: 300%; background-position: 0% 0%;

this behavior happens only on ios 16.4 (both mobile chrome and safari) & MacOS Safari 16.4 and working perfectly as it should in all other android or PC devices and also all earlier iOS or Safari versions.

any advice how to solve this to force it to work correctly on Safari 16.4

Edit 1:

I tried setting x & y positions separately like this:

            var li = $(&#39;&lt;li class=&quot;item&quot; data-value=&quot;&#39; + (i) + &#39;&quot;&gt;. 
                &lt;/li&gt;&#39;).css({
                &#39;background-size&#39;: (gridSize * 100) + &#39;%&#39;,
                &#39;background-position-x&#39;: xpos,
                &#39;background-position-y&#39;: ypos,
            });

but i got the same results and only one value is shown in "background-position".

Edit 2:

I even put the values manually like this:

            if (gridSize == 3) {
              if (i==0) {
                li.css(&#39;background-position&#39;, &#39;0% 0%&#39;);
              } else if (i==1) {
                li.css(&#39;background-position&#39;, &#39;50% 0%&#39;);
              } else if (i==2) {
                li.css(&#39;background-position&#39;, &#39;100% 0%&#39;);
              } else if (i==3) {
                li.css(&#39;background-position&#39;, &#39;0% 50%&#39;);
              } else if (i==4) {
                li.css(&#39;background-position&#39;, &#39;50% 50%&#39;);
              } else if (i==5) {
                li.css(&#39;background-position&#39;, &#39;100% 50%&#39;);
              } else if (i==6) {
                li.css(&#39;background-position&#39;, &#39;0% 100%&#39;);
              } else if (i==7) {
                li.css(&#39;background-position&#39;, &#39;50% 100%&#39;);
              } else if (i==8) {
                li.css(&#39;background-position&#39;, &#39;100% 100%&#39;);
              }
            }

and it still omits one value and leaves the other (for some elements and not the others) like this when i inspect :

&lt;li data-value=&quot;0&quot; style=&quot;background-size: 300% auto; background-position: 0%;&quot;&gt;&lt;/li&gt;
&lt;li data-value=&quot;1&quot; style=&quot;background-size: 300% auto; background-position: 50%;&quot;&gt;&lt;/li&gt;
&lt;li data-value=&quot;2&quot; style=&quot;background-size: 300% auto; background-position: 100%;&quot;&gt;&lt;/li&gt;
&lt;li data-value=&quot;3&quot; style=&quot;background-size: 300% auto; background-position: 50%;&quot;&gt;&lt;/li&gt;
&lt;li data-value=&quot;4&quot; style=&quot;background-size: 300% auto; background-position: 50% 50%;&quot;&gt;&lt;/li&gt;
&lt;li data-value=&quot;5&quot; style=&quot;background-size: 300% auto; background-position: 100% 50%;&quot;&gt;&lt;/li&gt;
&lt;li data-value=&quot;6&quot; style=&quot;background-size: 300% auto; background-position: 100%;&quot;&gt;&lt;/li&gt;
&lt;li data-value=&quot;7&quot; style=&quot;background-size: 300% auto; background-position: 50% 100%;&quot;&gt;&lt;/li&gt;
&lt;li data-value=&quot;8&quot; style=&quot;background-size: 300% auto; background-position: 100% 100%;&quot;&gt;&lt;/li&gt;

So now i realize it omits the "0%" value , so how to force it to keep it;

答案1

得分: 1

经过很多挣扎,我找到了一个可行的解决方案:
我给xposypos的值加了0.000001,这样,以防出现0值,就可以防止Safari将其视为绝对0…… 然后,,它就像魔法般起作用。

英文:

After a lot of struggling through this, I found a solution that works:
I added 0.000001 to the values of xpos and ypos so, in case of a 0 value, I prevent Safari from seeing it as absolute 0 &hellip; and voila, it worked like a charm.

huangapple
  • 本文由 发表于 2023年5月13日 09:39:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240742.html
匿名

发表评论

匿名网友

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

确定