2011-04-24 23 views
183

如何使用CSS翻转任何背景图片?可能吗?如何使用CSS翻转背景图片?

currenty我在CSS

enter image description here

使用该箭头图像中的一个libackground-imagevisited我需要水平翻转这个箭头。我能做到这一点,使箭头的另一个形象,我只是好奇,想知道是否有可能在翻转的CSS图像:visited

+3

很好的问题!这可以在许多不同的情况下派上用场。 – AshBrad 2012-03-23 16:57:30

+0

@AshBrad - 感谢您的评论。是的,它可能在许多条件下有用 – 2012-03-24 06:05:06

回答

57

我发现我的方式只看到一个线索翻转亚历克斯的答案后,只翻转背景不是整个元素。感谢亚历克斯你的答案

HTML

<div class="prev"><a href="">Previous</a></div> 
<div class="next"><a href="">Next</a></div> 

CSS

.next a, .prev a {width:200px;background:#fff} 
    .next {float:left} 
    .prev {float:right} 
    .prev a:before, 
    .next a:before { 
     content:""; 
     width:16px; 
     height:16px; 
     margin:0 5px 0 0; 
     background:url(http://i.stack.imgur.com/ah0iN.png) no-repeat 0 0; display:inline-block 
    } 


    .next a:before { 
     margin:0 0 0 5px; 
     transform:scaleX(-1); 

    } 

看到这里http://jsfiddle.net/qngrf/807/

+1

这工作奇迹!很酷。谢谢 – AshBrad 2012-03-23 16:56:52

+7

@Jitendra你能否检查Jsfiddle网址。我认为它没有相同的代码,你发布在你的答案+它不工作.. – sachinjain024 2013-04-03 14:45:19

+3

你的帖子jsfiddle代码是文本文件?这是你想要的,还是只是背景图片? – mandza 2014-09-11 13:00:21

187

你可以用CSS水平翻转...

a:visited { 
    -moz-transform: scaleX(-1); 
    -o-transform: scaleX(-1); 
    -webkit-transform: scaleX(-1); 
    transform: scaleX(-1); 
    filter: FlipH; 
    -ms-filter: "FlipH"; 
} 

jsFiddle

如果要垂直翻转而不是...

a:visited { 
    -moz-transform: scaleY(-1); 
    -o-transform: scaleY(-1); 
    -webkit-transform: scaleY(-1); 
    transform: scaleY(-1); 
    filter: FlipV; 
    -ms-filter: "FlipV"; 
} 

Source

+1

如果我只考虑Web-kit浏览器,那么只有'-webkit-transform:scaleX(-1);'就够了? – 2011-04-24 06:32:18

+0

@Jitendra它应该是,加上非供应商的前缀属性。 – alex 2011-04-24 06:34:21

16

据W3Schools的:在Internet Explorer 10,火狐和Opera支持 http://www.w3schools.com/cssref/css3_pr_transform.asp

变换属性。 Internet Explorer 9支持另一种-ms-transform属性(仅限2D变换)。 Safari和Chrome支持替代方案-webkit-transform属性(3D和2D转换)。 Opera仅支持2D变换。

这是一个2D转换,所以它应该可以在Chrome,Firefox,Opera,Safari和IE9 +上使用供应商前缀。

使用的其他答案:在停止翻转内部内容之前。我用这对我的页脚(垂直镜从我的头图像):

HTML:

<footer> 
<p><a href="page">Footer Link</a></p> 
<p>&copy; 2014 Company</p> 
</footer> 

CSS:

footer { 
background:url(/img/headerbg.png) repeat-x 0 0; 

/* flip background vertically */ 
-webkit-transform:scaleY(-1); 
-moz-transform:scaleY(-1); 
-ms-transform:scaleY(-1); 
-o-transform:scaleY(-1); 
transform:scaleY(-1); 
} 

/* undo the vertical flip for all child elements */ 
footer * { 
-webkit-transform:scaleY(-1); 
-moz-transform:scaleY(-1); 
-ms-transform:scaleY(-1); 
-o-transform:scaleY(-1); 
transform:scaleY(-1); 
} 

所以,你最终翻转的元素,然后再 - 翻转所有的孩子。也适用于嵌套元素。

+0

感谢您的回答!这对我来说效果最好,因为其他人要么翻转所有的孩子,要么需要更复杂的技巧来扭转翻转。 – Salgat 2015-11-22 20:06:45

5

您可以在同一时间

-moz-transform: scaleX(-1) scaleY(-1); 
    -o-transform: scaleX(-1) scaleY(-1); 
    -webkit-transform: scaleX(-1) scaleY(-1); 
    transform: scaleX(-1) scaleY(-1); 

并与过渡财产翻转垂直和水平你可以得到一个凉爽翻转

-webkit-transition: transform .4s ease-out 0ms; 
    -moz-transition: transform .4s ease-out 0ms; 
    -o-transition: transform .4s ease-out 0ms; 
    transition: transform .4s ease-out 0ms; 
    transition-property: transform; 
    transition-duration: .4s; 
    transition-timing-function: ease-out; 
    transition-delay: 0ms; 

其实它翻转元素,而不仅仅是background-image

摘要:SNIPPET

function flip(){ 
 
\t var myDiv = document.getElementById('myDiv'); 
 
\t if (myDiv.className == 'myFlipedDiv'){ 
 
\t \t myDiv.className = ''; 
 
\t }else{ 
 
\t \t myDiv.className = 'myFlipedDiv'; 
 
\t } 
 
}
#myDiv{ 
 
    display:inline-block; 
 
    width:200px; 
 
    height:20px; 
 
    padding:90px; 
 
    background-color:red; 
 
    text-align:center; 
 
    -webkit-transition:transform .4s ease-out 0ms; 
 
    -moz-transition:transform .4s ease-out 0ms; 
 
    -o-transition:transform .4s ease-out 0ms; 
 
    transition:transform .4s ease-out 0ms; 
 
    transition-property:transform; 
 
    transition-duration:.4s; 
 
    transition-timing-function:ease-out; 
 
    transition-delay:0ms; 
 
} 
 
.myFlipedDiv{ 
 
    -moz-transform:scaleX(-1) scaleY(-1); 
 
    -o-transform:scaleX(-1) scaleY(-1); 
 
    -webkit-transform:scaleX(-1) scaleY(-1); 
 
    transform:scaleX(-1) scaleY(-1); 
 
}
<div id="myDiv">Some content here</div> 
 

 
<button onclick="flip()">Click to flip</button>

+0

“同时翻转垂直和水平”的效果与将图像旋转180度的效果相同,效果很好,正是我所寻找的, – Stonecrusher 2016-04-21 12:53:20