如何翻转图像而不移动它

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

How to flip an image without moving it

问题

在下面的示例中,单击标志,您会看到 - 它被翻转了,但也被移动了
如何在不移动的情况下翻转它?
我也尝试过这个,但没有成功:

.flip{transform:scaleX(-1);}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class='wrap'>
<img src='https://abuena.net/img/logo_01.png' alt='img'>
</div>
英文:

In the example below, click on logo and you'll see - it is flipped but also moved
How to flip it without moving ?
I also tried this, without success:

.flip{transform:scaleX(-1) translatex(-50%);}

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

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

$(&#39;img&#39;).on(&#39;click&#39;, function(){
	$(this).addClass(&#39;flip&#39;);
 });

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

.flip{transform:scaleX(-1);}

.wrap{
  position:relative;
  width:50%;
  height:100px;
  background:darkorange;
 }
img{
  position:absolute;
  bottom:0;
  width:140px;
  left:50%;
  transform:translatex(-50%);
 }

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

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

&lt;div class=&#39;wrap&#39;&gt;
&lt;img src=&#39;https://abuena.net/img/logo_01.png&#39; alt=&#39;img&#39;&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 6

你还需要在.flip类中包括translate属性。

注意属性的顺序很重要,例如在translate之前加上scale,之后会给你不同的输出:

.flip {
  transform: translateX(-50%) scaleX(-1);
}
英文:

You also need to include the translate property in the .flip class.

Note that the order of properties is important, eg. scale before translate, and after will give you different outputs:

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

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

$(&#39;img&#39;).on(&#39;click&#39;, function() {
  $(this).toggleClass(&#39;flip&#39;);
});

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

.flip {
  transform: translateX(-50%) scaleX(-1);
}

.wrap {
  position: relative;
  width: 50%;
  height: 100px;
  background: darkorange;
}

img {
  position: absolute;
  bottom: 0;
  width: 140px;
  left: 50%;
  transform: translateX(-50%);
}

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

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

&lt;div class=&#39;wrap&#39;&gt;
&lt;img src=&#39;https://abuena.net/img/logo_01.png&#39; alt=&#39;img&#39;&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 3

需要对 scaleX -1 应用相反的 translateX 以抵消效果:

.flip{
  transform: scaleX(-1) translateX(50%);
}

请注意,“新”的 transform 会替换掉所有现有的 transform 属性,它们不会叠加。

$('img').on('click', function(){
    $(this).toggleClass('flip');
 });
.flip{
  transform: scaleX(-1) translateX(50%);
}

.wrap{
  position:relative;
  width:50%;
  height:100px;
  background:darkorange;
 }
img{
  position:absolute;
  bottom:0;
  width:140px;
  left:50%;
  transform: translateX(-50%);
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class='wrap'>
<img src='https://abuena.net/img/logo_01.png' alt='img'>
</div>
英文:

You need to apply an opposite translateX to counter the scaleX -1:

.flip{
transform:scaleX(-1) translatex(50%)
}

Note that a "new" transform will replace all of the existing transform properties, they do not add on.

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

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

$(&#39;img&#39;).on(&#39;click&#39;, function(){
    $(this).toggleClass(&#39;flip&#39;);
 });

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

.flip{
transform:scaleX(-1) translatex(50%)
}

.wrap{
  position:relative;
  width:50%;
  height:100px;
  background:darkorange;
 }
img{
  position:absolute;
  bottom:0;
  width:140px;
  left:50%;
  transform:translatex(-50%);
 }

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

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

&lt;div class=&#39;wrap&#39;&gt;
&lt;img src=&#39;https://abuena.net/img/logo_01.png&#39; alt=&#39;img&#39;&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月3日 14:51:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602447.html
匿名

发表评论

匿名网友

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

确定