gpt4 book ai didi

css - 将 CSS transition-timing-function 转换为滑动

转载 作者:太空宇宙 更新时间:2023-11-04 07:13:09 24 4
gpt4 key购买 nike

我正在使用自定义计时函数 ( http://cubic-bezier.com/#1,0,1,1 ) 在 div 中淡入淡出(使用 CSS 转换)。计时功能基本上是“缓入”的更极端版本。

div {
opacity: 0;
transition: opacity 1s;

.in {
opacity: 1;
transition-timing-function: cubic-bezier(1, 0, 1, 1);
}

除此之外,我希望能够通过在屏幕上滑动来使 div 淡入淡出。我正在使用以下 Jquery 根据用户滑动的距离设置不透明度:

documentWidth = $(document).width();

$(document).on('touchmove', function(e) {

// How much of the animation is completed (in %)
completion = e.changedTouches[0].pageX / documentWidth;

$('div').css('opacity', completion);
})

不不不,这是线性的!有没有聪明的数学家可以弄清楚如何重新陈述最后一行来表示我的计时功能?

因此,例如,如果 completion 为 25%,则不透明度应在 2% 左右。在 50% 时,它应该在 11% 左右,在 75% 时,它应该在 31% 左右。

最佳答案

首先找到一条与您的 cubic-bezier 曲线近似的曲线。有了给定的点和一些online tools可以用这个方程画一条曲线:

y = 464085.7 + (0.0174619 - 464085.7)/(1 + (x/22.88957)^4.174069)

在您的情况下,x 代表您的completion 变量,y 代表生成的不透明度。

那么你的代码就变成了

let completion = e.changedTouches[0].pageX / documentWidth;
let exponential = Math.pow((completion / 22.88957), 4.174069);
let opacity = 464085.7 + (0.0174619 - 464085.7)/(1 + exponential);

$('div').css('opacity', opacity);

(当然你可能会找到最适合你需要的更好的等式)

关于css - 将 CSS transition-timing-function 转换为滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50983658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com