gpt4 book ai didi

jquery - 在动画暂停时使用 jQuery 更改动画延迟在 Safari 上不起作用,但在其他任何地方都可以

转载 作者:技术小花猫 更新时间:2023-10-29 11:12:24 25 4
gpt4 key购买 nike

我在 CSS 中设置了一个关键帧动画。将其附加到 DOM 元素并将其设置为暂停。使用 javascript (jQuery),我将动画延迟从 0s 更改为 100s,从而在滚动时实现漂亮的动画效果。

这适用于所有浏览器,但不适用于 Safari(版本 11.1.1 (13605.2.8))。

$(document).ready(function() {
fluider([
{
selector: '.manualAnim',
start: 100,
end: 500
},

{
selector: '.manualAnim2',
start: 500,
end: 1000
},

{
selector: '.manualAnim3',
start: 0,
end: 1500
}

])
})


function fluider(o) {
for(var i = 0; i < o.length; i++) {
$(o[i].selector).css('animation-play-state','paused');
$(o[i].selector).css('animation-duration','100s');
}
$(window).scroll(function() {
var h = $(window).scrollTop();
for(var i = 0; i < o.length; i++) {

$(o[i].selector).css('animation-delay',-clamp(0,100,((h-o[i].start)/o[i].end * 100)) + 's');
}
});

}

function clamp(from, to, val) {
if(val >= from) {
if(val <= to) {
return val;
}
else {
return to;
}
}
else {
return from;
}
}
   body {
height: 1000vh;
}
.manualAnim {
position: fixed;
display: block;
width: 100px;
height: 100px;
background-color: red;
animation: 100s anim paused both;
animation-delay: 0s;
}

.manualAnim2 {
position: fixed;
display: block;
left: 120px;
width: 100px;
height: 100px;
background-color: red;
animation: 100s anim paused both;
animation-delay: 0s;
}

.manualAnim3 {
position: fixed;
display: block;
left: 240px;
width: 100px;
height: 100px;
background-color: red;
animation: 100s anim paused both;
animation-delay: 0s;
}

@keyframes anim{
0% {
background-color: red;
transform: scale(1);
}
30% {
background-color: green;
transform: scale(1.5);
}
60% {
background-color: blue;
transform: scale(0.5);
}
100% {
background-color: yellow;
transform: scale(1);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="manualAnim"></div>
<div class="manualAnim2"></div>
<div class="manualAnim3"></div>

我现在用 Google 搜索了 几个小时 天,但我不知道可能是什么问题。有什么想法吗?

最佳答案

经过大量实验后,这里有一个具有变通办法的版本,可以在 Safari 11.1.2 和 Chrome 68(希望其他浏览器也一样)中提供顺畅的预期行为。

看起来潜在的问题是当动画属性为暂停的动画更改时元素不会重绘,正如问题所述。该解决方案通过在每一帧重新添加必要的与动画相关的 CSS(具有正确的延迟)来解决这个问题。这通常会导致闪烁(因为 Safari 试图在移除动画时恢复为非动画样式),因此该解决方案在每次修改动画时都手动应用当前动画样式。

$(document).ready(function() {
fluider([{
selector: '.manualAnim',
start: 100,
end: 500
},

{
selector: '.manualAnim2',
start: 500,
end: 1000
},

{
selector: '.manualAnim3',
start: 0,
end: 1500
}

])
})

function getAnimatedProperties(animName) {
// Get an array of all property names that
// are modified by the animation ${animName}
let properties = {};
let sheets = document.styleSheets;
let propertyRegex = /([a-z\-]+):/ig;
for (let sheet of sheets) {
let rules = sheet.rules || sheet.cssRules;
for (let r of rules) {
if (r.name === animName) {
let rText = r.cssText;
let match = propertyRegex.exec(rText);
while (match) {
properties[match[1]] = true;
match = propertyRegex.exec(rText);
}
}
}
}
return Object.keys(properties);
}

function fluider(o) {
const animationName = "anim";
const preservedProperties = getAnimatedProperties(animationName);
$(window).scroll(function() {
var h = $(window).scrollTop();
for (var i = 0; i < o.length; i++) {
let el = document.querySelector(o[i].selector);
let pct = 100 * (parseInt(h) - o[i].start) / o[i].end;
let delay = -Math.max(Math.min(pct, 100), 0) + 's';
let s = window.getComputedStyle(el);
// without setting these properties and overwriting .style,
// the animation will flicker
let preservedStyles = preservedProperties.map(p => `${p}: ${s[p]};`).join("");
el.style = `${preservedStyles} animation-delay: ${delay}; animation-duration: 100s; animation-play-state: paused;`;
// without scheduling this *using setTimeout*,
// the animation will not rerender
window.setTimeout(() => {
el.style.animationName = animationName;
}, 0);
}
});
}
body {
height: 1000vh;
}

.manualAnim {
position: fixed;
display: block;
width: 100px;
height: 100px;
background-color: red;
}

.manualAnim2 {
position: fixed;
display: block;
left: 120px;
width: 100px;
height: 100px;
background-color: red;
}

.manualAnim3 {
position: fixed;
display: block;
left: 240px;
width: 100px;
height: 100px;
background-color: red;
}

@keyframes anim {
0% {
background-color: red;
transform: scale(1);
}
30% {
background-color: green;
transform: scale(1.5);
}
60% {
background-color: blue;
transform: scale(0.5);
}
100% {
background-color: yellow;
transform: scale(1);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="manualAnim"></div>
<div class="manualAnim2"></div>
<div class="manualAnim3"></div>

关于jquery - 在动画暂停时使用 jQuery 更改动画延迟在 Safari 上不起作用,但在其他任何地方都可以,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51484408/

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