简体   繁体   English

在不冻结动画的情况下更改css线性渐变动画

[英]Changing a css linear gradient animation without freezing the animation

I have an linear gradient animation for my website and I would like themes, so I am trying to use javascript to change the color's in the css, I got it to do something but it freezes the animation when I do so. 我有一个线性渐变动画为我的网站,我想主题,所以我试图使用javascript来改变css中的颜色,我得到它做一些事情,但它冻结动画时,我这样做。

 function changeBackground() { document.body.style.background = "linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB)"; } 
 body { width: 100wh; height: 90vh; color: #fff; background: linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB); background-size: 400% 400%; -webkit-animation: Gradient 15s ease infinite; -moz-animation: Gradient 15s ease infinite; animation: Gradient 15s ease infinite; } @-webkit-keyframes Gradient { 0% { background-position: 0% 50% } 50% { background-position: 100% 50% } 100% { background-position: 0% 50% } } @-moz-keyframes Gradient { 0% { background-position: 0% 50% } 50% { background-position: 100% 50% } 100% { background-position: 0% 50% } } @keyframes Gradient { 0% { background-position: 0% 50% } 50% { background-position: 100% 50% } 100% { background-position: 0% 50% } } 
  <a onclick="changeBackground()">Default</a> <a onclick="clickHandler()">Fire</a> // This will be implemented at a later time. 

Change only the background-image not the whole background . 仅更改background-image而不是整个background Changing the background will override the background-size and will freeze the animation. 更改背景将覆盖background-size并冻结动画。 Better define background-image in the CSS also to avoid other issues. 更好地定义CSS中的background-image也可以避免其他问题。

You can also get rid of the prefix versions and simplify the animation like below: 您还可以删除前缀版本并简化动画,如下所示:

 function changeBackground() { document.body.style.backgroundImage = "linear-gradient(-45deg, blue,red)"; } 
 body { width: 100wh; height: 90vh; color: #fff; background-image: linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB); background-size: 400% 400%; animation: Gradient 15s ease infinite; } @keyframes Gradient { 0%,100% { background-position: left } 50% { background-position: right } } 
 <a onclick="changeBackground()">Default</a> <a onclick="clickHandler()">Fire</a> // This will be implemented at a later time. 

You can check this answer to understand the simplification and to have more details in case you need different animations: https://stackoverflow.com/a/51734530/8620333 您可以查看此答案以了解简化,并在需要不同动画时获取更多详细信息: https//stackoverflow.com/a/51734530/8620333

First, since your "a" tag acting as buttons and not as anchors you should use the button element. 首先,由于你的“a”标签充当按钮而不是锚,你应该使用按钮元素。 Secondly, make a class with the desired background colors and fire it with the onclick event. 其次,创建一个具有所需背景颜色的类,并使用onclick事件激活它。 (by the way body width should be on vw and not as you wrote it) (顺便说一下,身体宽度应该是vw而不是你写的)

 function changeBackground() { document.body.classList.add('changeBackground'); } 
 body { width: 100vw; height: 90vh; color: #fff; background-image: linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB); background-size: 400% 400%; -webkit-animation: Gradient 15s ease infinite; -moz-animation: Gradient 15s ease infinite; animation: Gradient 15s ease infinite; } @-webkit-keyframes Gradient { 0% { background-position: 0% 50% } 50% { background-position: 100% 50% } 100% { background-position: 0% 50% } } @-moz-keyframes Gradient { 0% { background-position: 0% 50% } 50% { background-position: 100% 50% } 100% { background-position: 0% 50% } } @keyframes Gradient { 0% { background-position: 0% 50% } 50% { background-position: 100% 50% } 100% { background-position: 0% 50% } } .changeBackground { background-image: linear-gradient(-45deg, yellow, blue, red, green); } 
 <button onclick="changeBackground()">Default</button> 

***Just another approach. ***只是另一种方法。

Good Luck! 祝好运!

The Fix 修复

This is because you are overriding the values of your animation. 这是因为您要覆盖动画的值。 In CSS inline styles have a higher specificity than linked styles, and the background attribute is a shorthand that sets both background-image and background-position . CSS内联样式比链接样式具有更高的特异性background属性是设置background-imagebackground-position的简写。 The styles you're applying via JavaScript are setting new values with higher specificty than your animation keyframes. 您通过JavaScript应用的样式设置的新值具有比动画关键帧更高的特定性 To fix this, set backgroundImage rather than background . 要解决此问题,请设置backgroundImage而不是background

 function changeBackground() { document.body.style.backgroundImage = "linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB)"; } 
 body { width: 100wh; height: 90vh; color: #fff; background: linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB); background-size: 400% 400%; -webkit-animation: Gradient 15s ease infinite; -moz-animation: Gradient 15s ease infinite; animation: Gradient 15s ease infinite; } @-webkit-keyframes Gradient { 0% { background-position: 0% 50% } 50% { background-position: 100% 50% } 100% { background-position: 0% 50% } } @-moz-keyframes Gradient { 0% { background-position: 0% 50% } 50% { background-position: 100% 50% } 100% { background-position: 0% 50% } } @keyframes Gradient { 0% { background-position: 0% 50% } 50% { background-position: 100% 50% } 100% { background-position: 0% 50% } } 
 <a onclick="changeBackground()">Default</a> <a onclick="clickHandler()">Fire</a> // This will be implemented at a later time. 

An Improved Approach 一种改进的方法

Better yet - use CSS classes to apply the change in styles rather than through JavaScript and avoid the specificity battle altogether. 更好的是 - 使用CSS类来应用样式的变化而不是通过JavaScript,并完全避免特殊性争夺。 This how CSS is intended to be used. 这是如何使用CSS的。

Also worth mentioning a <button> is a more appropriate element to use for behavior, as anchors are for sending the user somewhere. 另外值得一提的是, <button>是一个更适合用于行为的元素,因为锚点用于将用户发送到某个地方。

Though, if you're pulling the linear gradient values programmatically this may not be an option. 但是,如果您以编程方式提取线性渐变值,则可能不是一种选择。

 function setDefault() { document.querySelector('body').setAttribute('class', ''); }; function clickHandler() { document.querySelector('body').classList.add('fire'); }; 
 body { width: 100wh; height: 90vh; color: #fff; background-image: linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB); background-size: 400% 400%; -webkit-animation: Gradient 15s ease infinite; -moz-animation: Gradient 15s ease infinite; animation: Gradient 15s ease infinite; } .fire { background-image: linear-gradient(-45deg, #ff0000, #efefef, #ff0000, #efefef); } @-webkit-keyframes Gradient { 0% { background-position: 0% 50% } 50% { background-position: 100% 50% } 100% { background-position: 0% 50% } } @-moz-keyframes Gradient { 0% { background-position: 0% 50% } 50% { background-position: 100% 50% } 100% { background-position: 0% 50% } } @keyframes Gradient { 0% { background-position: 0% 50% } 50% { background-position: 100% 50% } 100% { background-position: 0% 50% } } 
 <button onclick="setDefault()" tyle="button">Default</buttopn> <button onclick="clickHandler()" tyle="button">Fire</buttopn> 

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

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