简体   繁体   English

当背景图像缩放时,它会在Chrome中开始闪烁

[英]When background image scaled, it starts to flicker in Chrome

I have a div with a background image on it. 我有一个带有背景图像的div。 When it has simple transform scale animation, it starts to flicker in Google Chrome and Opera. 当它具有简单的变换比例动画时,它开始在谷歌Chrome和Opera中闪烁。 Here is a simple exmple: 这是一个简单的例子:

http://codepen.io/anon/pen/bWpNYq http://codepen.io/anon/pen/bWpNYq

CSS: CSS:

body {
  height: 100vh;
  overflow: hidden
}

div {
  width: 100%;
  height: 100%;
  background-color: #f00;
  background-position: 50% 50%;
  background-image: url(".....jpg");
  background-size: cover;
}

Script: 脚本:

TweenLite.set('div', {
  backfaceVisibility: 'hidden',
  perspective: 1000
});
TweenLite.fromTo('div', 10, {
  scale: 1.1
}, {
  scale: 1
});

When the image is a simple img element, the same scale animation works fine. 当图像是一个简单的img元素时,相同的缩放动画可以正常工作。 The transition is smooth: 转型顺利:

http://codepen.io/anon/pen/pPyvdp http://codepen.io/anon/pen/pPyvdp

The examples use GASP for animations. 这些示例使用GASP进行动画制作。 I need a solution which use GSAP to scale the div with better result. 我需要一个使用GSAP来扩展div并获得更好结果的解决方案。

Do you any idea how to make it smooth with background image? 你知道如何使它与背景图像平滑吗?

Try this: Add transition: all 1s linear; 试试这个:添加transition: all 1s linear; so it scale smoothly. 所以它顺利扩展。

body {
  height: 100vh;
  overflow: hidden
}

div {
  width: 100%;
  height: 100%;
  background-position: 50% 50%;
  background-image: url("https://smartslider3.com/wp-content/uploads/2015/10/slide52.jpg");
  background-size: cover;
  transition: all 1s linear;
}

Hey maybe you can try out this css animation. 嘿,也许你可以试试这个css动画。 For better browser support add 为了更好的浏览器支持添加

-webkit-animation
-moz-animation
-o-animation

 body { height: 100vh; overflow: hidden } div { width: 100%; height: 100%; background-position: 50% 50%; background-image: url("https://smartslider3.com/wp-content/uploads/2015/10/slide52.jpg"); background-size: cover; -webkit-animation: animate 5s forwards; animation: animate 5s forwards; } @-webkit-keyframes animate { 0% { transform: scale(1); } 100% { transform: scale(1.1); } } @keyframes animate { 0% { transform: scale(1); } 100% { transform: scale(1.1); } } 
 <div> </div> 

CSS3 allows you to add native transition to your transformations. CSS3允许您为转换添加本机转换。 Try to use code below: 尝试使用以下代码:

 document.body.addEventListener('click', function(){ var div = document.getElementById('img'); div.style.transform = 'scale(.5)'; }) 
 body { height: 100vh; overflow: hidden } div { width: 100%; height: 100%; background-position: 50% 50%; background-image: url("https://smartslider3.com/wp-content/uploads/2015/10/slide52.jpg"); background-size: cover; transition: transform 30s; } 
 <div id="img"></div> 

It uses css property "transition" and starts transition on body click. 它使用css属性“transition”并在body click上开始转换。

Just use css, way better. 只需使用css,方式更好。 If you open up your inspector you'll see that your tweenlite code is setting/ updating the style attribute of your div very fast with this piece of code: transform: translate3d(0px, 0px, 0px) scale(1.00212, 1.00212); 如果你打开你的检查器,你会看到你的tweenlite代码用这段代码非常快地设置/更新div的style属性: transform: translate3d(0px, 0px, 0px) scale(1.00212, 1.00212); . This is JS calculating something and then telling CSS what to do (very basic explanation). 这是JS计算的东西,然后告诉CSS做什么(非常基本的解释)。 CSS can do this on it's own. CSS可以自己做这件事。 Why do you want to stick with your GSAP engine so badly? 你为什么要坚持使用你的GSAP引擎?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM