[英]Achieve animated seesaw -ish effect with linear-gradient (pure CSS)
I have an element with one diagonal side achieved by adjusting linear-gradient and height - in two different states. 我有一个通过调整线性梯度和高度(在两种不同状态下)而实现的对角线一侧的元素。 Now I try to toggle between these states and have a smooth transition of the red triangle, so that it would look like a seesaw :-) The problem is, that from one state to another it changes the direction and is jumpy, I did not find a way to animate it fluently .. Is there a way to to what I want using pure CSS eg using transitions?
现在,我尝试在这些状态之间切换,并使红色三角形平滑过渡,从而使它看起来像跷跷板:-)问题是,从一种状态切换到另一种状态会改变方向并且跳动,我没有找到一种方法来流畅地制作动画。是否可以使用纯CSS(例如使用过渡)来实现我想要的方式?
let btn = document.getElementsByTagName('button')[0]; let stage = document.getElementById('stage'); btn.addEventListener('click', function() { stage.classList.toggle('fixie'); });
body, ul { padding: 0; margin: 0; } #stage { width: 100%; height: 14em; background: pink; padding: 0; border: 1px solid blue; } #stage::before { display: block; position: relative; width: 100%; height: 100%; /*as high as #stage*/ opacity: 0.4; content: ''; z-index: 1; background: linear-gradient(to left bottom, red 50%, pink 50%); /*transition: height 4s;*/ /*transition: linear-gradient 4s 8s;*/ } #stage.fixie::before { height: 30%; background: linear-gradient(to right bottom, red 50%, pink 50%); }
<div id="stage"></div> <button>animate gradient</button>
As you can't animate linear-gradient
, here is a workaround using transform
由于您无法设置
linear-gradient
动画,因此这是使用transform
一种解决方法
In this sample I used skew
. 在此示例中,我使用了
skew
。 As the degree of skew will differ based on the width/height, and as long as its ratio is kept, this will be fully responsive, else you'll need a small script. 由于偏斜的程度会根据宽度/高度而有所不同,并且只要保持其比例,就可以完全响应,否则您将需要一个小的脚本。
(function(){ let btn = document.getElementsByTagName('button')[0]; let stage = document.getElementById('stage'); btn.addEventListener('click', function() { stage.classList.toggle('fixie'); }); })();
body, ul { padding: 0; margin: 0; } #stage { position: relative; overflow: hidden; width: 90vw; height: calc(90vw * 0.2677); /* 0.2677, aspect ratio that match skew degree */ background: pink; padding: 0; border: 1px solid blue; } .navi { width: 100%; min-height: 4em; height: auto; background: green; z-index: 2; position: relative; } #stage::before { content: ''; position: absolute; width: 100%; height: 100%; /*as high as #stage*/ bottom: 100%; opacity: 0.4; z-index: 1; background: red; transform: skewY(15deg); transform-origin: left bottom; transition: transform 2s; } #stage.fixie::before { transform: skewY(-15deg) translateY(100%); } .navi ul { list-style: none; display: flex; background: lightblue; justify-content: flex-end; } .navi ul li { display: flex; align-items: center; justify-content: center; min-width: 4em; width: auto; height: 2em; margin: 1px; background: yellow; }
<div id="stage"></div> <button> animate </button>
Side note: 边注:
One can use any fixed value instead of vw
, as long as the #stage
's ratio is kept. 只要保持
#stage
的比率,就可以使用任何固定值代替vw
。 If to change ratio, you'll either need a script, since CSS calc
can't do math with sqrt
and sin
/ cos
etc. to get the angle, or using media query's, and have angle's and ratio's manually set for different screens. 如果要更改比例,您将需要一个脚本,因为
CSS calc
无法使用sqrt
和sin
/ cos
等进行数学运算来获取角度,也无法使用媒体查询来对角度进行设置,并且无法针对不同的屏幕手动设置角度和比例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.