[英]CSS keyframes working fine in dev environment but not on production build
我正在開發一個使用 node-sass 編譯 SCSS 的應用程序。 我的組件結構是這樣的:
./src
|-- components
| |-- Foo
| | |-- Foo.jsx
| | `-- Foo.scss
| |-- Bar
| |-- Bar.jsx
| `-- Bar.scss
[...]
然后我這樣稱呼我的 scss:
import React, {useState} from 'react';
import './Foo.scss';
[...]
當我運行開發構建時,我的所有轉換都正常,但是當我運行構建和部署時,它們會丟失。 所有其他 CSS styles 工作正常,只是我的轉換搞砸了。 它們仍然存在,但它們都發生在最后一幀。 因此,如果一個轉換應該在 200 毫秒內從 0 到 100 go,它仍然會發生,但它在最后一個可能的幀中從 0 到 100。
誰能指出我正確的方向? 這是我的 package.json:
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"node-sass": "^4.14.1",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-icons": "^3.10.0",
"react-map-gl": "^5.2.7",
"react-map-gl-geocoder": "^2.0.12",
"react-redux": "^7.2.0",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "^3.4.3",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
編輯:根據要求,這是我的過渡示例:
.fade-in {
animation: fade-in ease-in 0.25s,
slide-in ease-in 0.25s;
}
@keyframes fade-in {
0% {
opacity: 0%;
}
100% {
opacity: 100%;
}
}
@keyframes slide-in {
0%{
transform: translateY(50px);
}
100%{
transform: translateY(0px);
}
}
然后我根據 state 有條件地添加類。
簡短回答:.scss 不支持 % 表示法(根據 w3 文檔,它也不是官方表示法: https://www.w3.org/TR/css-color-3/#opacity )有些瀏覽器不支持也支持 % 符號。 將不透明度值從 % 表示法更改為介於 0 和 1 之間的數字表示法。
長答案:我有一個類似的問題。 在 create-react-app 環境中,當我在 cli 上使用 npm 啟動時,以下動畫正確運行,但當我運行 npm run build 和 serve -s build 時,它不起作用。 我的不透明度為 0 的元素會在最后一幀突然達到其最終不透明度(就像你的一樣。)這是我原來的 custom.css 文件:
/*keyframes.css*/
@keyframes slideInRightDark {
0% {
opacity: 0%;
transform: translate(100%);
}
100% {
opacity: 40%;
transform: translate(0%);
}
和反應組件文件:
/*Text.js*/
import "[...some_file_path]/keyframes.css"
...
return (<div style={{animationName: slideInRightDark, animationDuration... etc.}}></div>)
我通過查看 animate.css 庫 ( https://animate.style/ ) 並觀察 animate.css 關鍵幀是如何構建的來讓它工作的。 順便說一句,很棒的即時可用關鍵幀庫。 強烈推薦用於快速啟動您的 web 應用程序。 無論如何,這是 animate.css 文件中的內容:
/* CSS keyframes from animate.css */
@charset "UTF-8";
:root {
--animate-duration: 1s;
--animate-delay: 1s;
--animate-repeat: 1;
}
...
@-webkit-keyframes slideInLeft {
from {
-webkit-transform: translate3d(-100%, 0, 0);
transform: translate3d(-100%, 0, 0);
visibility: visible;
}
to {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
}
@keyframes slideInLeft {
from {
-webkit-transform: translate3d(-100%, 0, 0);
transform: translate3d(-100%, 0, 0);
visibility: visible;
}
to {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
}
在換入和換出 utf-charset 和:root 部分以及添加我自己的 @-webkit-keyframes 之后,我發現實際上是不透明度以百分比表示法使它在動畫持續時間后突然閃爍:
/*in keyframes.css*/
...
@keyframes slideInRightDark {
from {
opacity: 0;
transform: translate(100%, 0);
}
to {
opacity: 0.4;
transform: translate(0, 0);
}
}
我希望這有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.