CSS CSS3 背景图片过渡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9483364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 03:07:37  来源:igfitidea点击:

CSS3 background image transition

cssbackgroundbackground-imagecss-transitions

提问by Nick Zani

I'm trying to make a "fade-in fade-out" effect using the CSS transition. But I can't get this to work with the background image...

我正在尝试使用 CSS 过渡制作“淡入淡出”效果。但我不能让它与背景图像一起工作......

The CSS:

CSS:

.title a {
    display: block;
    width: 340px;
    height: 338px;
    color: black;
    background: transparent;
    /* TRANSITION */
    -webkit-transition: background 1s;
    -moz-transition: background 1s;
    -o-transition: background 1s;
    transition: background 1s;
}

.title a:hover {
    background: transparent;
    background: url(https://lh3.googleusercontent.com/-p1nr1fkWKUo/T0zUp5CLO3I/AAAAAAAAAWg/jDiQ0cUBuKA/s800/red-pattern.png) repeat;
    /* TRANSITION */
    -webkit-transition: background 1s;
    -moz-transition: background 1s;
    -o-transition: background 1s;
    transition: background 1s;
}?

Take a look: http://jsfiddle.net/AK3La/

看一看:http: //jsfiddle.net/AK3La/

回答by Hengjie

You can transition background-image. Use the CSS below on the imgelement:

你可以过渡background-image。在img元素上使用以下 CSS :

-webkit-transition: background-image 0.2s ease-in-out;
transition: background-image 0.2s ease-in-out;

This is supported natively by Chrome, Opera and Safari. Firefox hasn't implemented it yet (bugzil.la). Not sure about IE.

Chrome、Opera 和 Safari 原生支持此功能。Firefox 还没有实现它(bugzil.la)。不知道IE。

回答by Ruffo

The solution (that I found by myself) is a ninja trick, I can offer you two ways:

解决方案(我自己找到的)是忍者技巧,我可以为您提供两种方法:

first you need to make a "container" for the <img>, it will contain normaland hoverstates at the same time:

首先,您需要为 制作一个“容器” <img>,它将同时包含正常悬停状态:

<div class="images-container">
    <img src="http://lorempixel.com/400/200/animals/9/">
    <img src="http://lorempixel.com/400/200/animals/10/">
</div>

Basically, you need to hide "normal" state and show their "hover" when you hover it

基本上,你需要隐藏的“正常”状态,并显示自己的“悬停”当你悬停它

and that's it, I hope somebody find it useful.

就是这样,我希望有人觉得它有用。

回答by Austin Parrish Thomas

I've figured out a solution that worked for me...

我想出了一个对我有用的解决方案......

If you have a list item (or div) containing only the link, and let's say this is for social links on your page to facebook, twitter, ect. and you're using a sprite image you can do this:

如果您有一个仅包含链接的列表项(或 div),假设这是您页面上到 facebook、twitter 等的社交链接。并且您正在使用精灵图像,您可以执行以下操作:

<li id="facebook"><a href="facebook.com"></a></li>

Make the "li"s background your button image

使“li”的背景成为您的按钮图像

#facebook {
   width:30px;
   height:30px;
   background:url(images/social) no-repeat 0px 0px;
}

Then make the link's background image the hover state of the button. Also add the opacity attribute to this and set it to 0.

然后使链接的背景图像成为按钮的悬停状态。还要为此添加不透明度属性并将其设置为 0。

#facebook a {
   display:inline-block;
   background:url(images/social) no-repeat 0px -30px;
   opacity:0;
}

Now all you need is "opacity" under "a:hover" and set this to 1.

现在您只需要“a:hover”下的“opacity”并将其设置为 1。

#facebook a:hover {
   opacity:1;
}

Add the opacity transition attributes for each browser to "a" and "a:hover" so the the final css will look something like this:

将每个浏览器的不透明度过渡属性添加到“a”和“a:hover”,这样最终的 css 将如下所示:

#facebook {
   width:30px;
   height:30px;
   background:url(images/social) no-repeat 0px 0px;
}
#facebook a {
   display:inline-block;
   background:url(images/social) no-repeat 0px -30px;
   opacity:0;
   -webkit-transition: opacity 200ms linear;
   -moz-transition: opacity 200ms linear;
   -o-transition: opacity 200ms linear;
   -ms-transition: opacity 200ms linear;
   transition: opacity 200ms linear;
}
#facebook a:hover {
   opacity:1;
   -webkit-transition: opacity 200ms linear;
   -moz-transition: opacity 200ms linear;
   -o-transition: opacity 200ms linear;
   -ms-transition: opacity 200ms linear;
   transition: opacity 200ms linear;
}

If I explained it correctly that should let you have a fading background image button, hope it helps at least!

如果我解释正确应该让你有一个褪色的背景图像按钮,希望它至少有帮助!

回答by Olwaro

Unfortunately you can't use transition on background-image, see the w3c list of animatable properties.

不幸的是,您不能在 上使用过渡background-image,请参阅w3c 可动画属性列表

You may want to do some tricks with background-position.

你可能想用background-position.

回答by MaxCloutier

You can use pseudo element to get the effect you want like I did in that Fiddle.

你可以使用伪元素来获得你想要的效果,就像我在那个Fiddle 中所做的那样。

CSS:

CSS:

.title a {
    display: block;
    width: 340px;
    height: 338px;
    color: black;
    position: relative;
}
.title a:after {
    background: url(https://lh3.googleusercontent.com/-p1nr1fkWKUo/T0zUp5CLO3I/AAAAAAAAAWg/jDiQ0cUBuKA/s800/red-pattern.png) repeat;
    content: "";
    opacity: 0;
    width: inherit;
    height: inherit;
    position: absolute;
    top: 0;
    left: 0;
    /* TRANSISITION */
    transition: opacity 1s ease-in-out;
    -webkit-transition: opacity 1s ease-in-out;
    -moz-transition: opacity 1s ease-in-out;
    -o-transition: opacity 1s ease-in-out;
}
.title a:hover:after{   
    opacity: 1;
}

HTML:

HTML:

<div class="title">
    <a href="#">HYPERLINK</a>
</div>

回答by MaxDhn

If you can use jQuery, you can try BgSwitcherplugin to switch the background-image with effects, it's very easy to use.

如果你会使用jQuery,你可以试试BgSwitcher插件来切换背景图片的效果,非常好用。

For example :

例如 :

$('.bgSwitch').bgswitcher({
        images: ["style/img/bg0.jpg","style/img/bg1.jpg","style/img/bg2.jpg"],
        effect: "fade",
        interval: 10000
});

And add your own effect, see adding an effect types

并添加您自己的效果,请参阅添加效果类型

回答by Pablo S G Pacheco

Considering background-images can't be animated, I created a little SCSS mixin allowing to transition between 2 different background-images using pseudo selectors beforeand after. They are at different z-index layers. The one that is ahead starts with opacity 0 and becomes visible with hover.

考虑到背景图像不能被动画化,我创建了一个小的 SCSS mixin 允许在 2 个不同的背景图像之前之后使用伪选择器之间转换。它们位于不同的 z-index 层。前面的那个从不透明度 0 开始,悬停时变得可见。

You can use it the same approach for creating animations with linear-gradients too.

您也可以使用相同的方法来创建带有线性渐变的动画。

scss

scss

@mixin bkg-img-transition( $bkg1, $bkg2, $transTime:0.5s ){  
  position: relative;  
  z-index: 100; 
  &:before, &:after {
    background-size: cover;  
    content: '';    
    display: block;
    height: 100%;
    position: absolute;
    top: 0; left: 0;    
    width: 100%;    
    transition: opacity $transTime;
  }
  &:before {    
    z-index: -101;
    background-image: url("#{$bkg1}");    
  }
  &:after {    
    z-index: -100;
    opacity: 0;
    background-image: url("#{$bkg2}");    
  }
  &:hover {
     &:after{
       opacity: 1; 
     }
  }  
}

Now you can simply use it with

现在你可以简单地使用它

@include bkg-img-transition("https://picsum.photos/300/300/?random","https://picsum.photos/g/300/300");

You can check it out here: https://jsfiddle.net/pablosgpacheco/01rmg0qL/

你可以在这里查看:https: //jsfiddle.net/pablosgpacheco/01rmg0qL/

回答by Hesham Nasser

Try this, will make the background animated worked on web but hybrid mobile app not working

试试这个,将使背景动画在网络上工作,但混合移动应用程序不起作用

@-webkit-keyframes breath {
 0%   {  background-size: 110% auto; }
 50%  {  background-size: 140% auto; }
 100% {  background-size: 110% auto; }      
}
body {
   -webkit-animation: breath 15s linear infinite;
   background-image: url(images/login.png);
    background-size: cover;
}

回答by shock_one

If animating opacityis not an option, you can also animate background-size.

如果动画opacity不是一个选项,你也可以 animate background-size

For example, I used this CSS to set a backgound-imagewith a delay.

例如,我使用这个 CSS 来设置backgound-image一个延迟。

.before {
  background-size: 0;
}

.after {
  transition: background 0.1s step-end;
  background-image: $path-to-image;
  background-size: 20px 20px;
}

回答by mohammed Suleiman

Salam, this answer works only in Chrome, cause IE and FF support color transition.

Salam,此答案仅适用于 Chrome,因为 IE 和 FF 支持颜色过渡。

There is no need to make your HTML elements opacity:0, cause some times they contain text, and no need to double your elements!.

无需制作 HTML 元素opacity:0,因为有时它们包含文本,并且无需将元素加倍!。

The question with link to an example in jsfiddle needed a small change, that is to put an empty image in .title alike background:url(link to an empty image);same as you put it in .title a:hoverbut make it empty image, and the code will work.

与链接到的jsfiddle一个例子问题需要一个小的变化,那就是把一个空的图像中.title abackground:url(link to an empty image);你一样把它在.title a:hover,但让它空图像,并且代码将工作。

.title a {
display: block;
width: 340px;
height: 338px;
color: black;
background: url(https://upload.wikimedia.org/wikipedia/commons/5/59/Empty.png) repeat;
/* TRANSISITION */
transition: background 1s;
-webkit-transition: background 1s;
-moz-transition: background 1s;
-o-transition: background 1s;
  }
  .title a:hover{   background: transparent;
   background: url(https://lh3.googleusercontent.com/-p1nr1fkWKUo/T0zUp5CLO3I/AAAAAAAAAWg/jDiQ0cUBuKA/s800/red-pattern.png) repeat;
/* TRANSISITION */
transition: background 1s;
-webkit-transition: background 1s;
-moz-transition: background 1s;
-o-transition: background 1s;
}

Check this out https://jsfiddle.net/Tobasi/vv8q9hum/

看看这个 https://jsfiddle.net/Tobasi/vv8q9hum/