3 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個贊
您必須進(jìn)行一些編輯。
您正在使用鏈接標(biāo)簽來加載 javascript。您需要更改為script標(biāo)記。
<link href="//cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css"rel="stylesheet">
<link href="//cdnjs.cloudflare.com/ajax/libs/gsap/1.19.1/TweenMax.min.js">
clock.js 取決于渲染的 dom 元素。在渲染 DOM 之前,您不應(yīng)讓腳本運(yùn)行。使用componentDidMount生命周期方法加載腳本。
如果它是一個自執(zhí)行腳本,您可以使用動態(tài)導(dǎo)入。在這種情況下,您可能需要在所有全局變量前加上window.. 例如window.TweenMax,window.Power1,window.Back
componentDidMount(){
import("./js/clock.js");
}
或者
您可以更改腳本以導(dǎo)出默認(rèn)函數(shù)并在 componentDidMount
export default () => {
//All the clock.js code
makeAnimation();
}
并用作
import makeAnimation from "./js/clock";
class Loading extends Component {
componentDidMount(){
makeAnimation();
}
使用這種方法的演示
或者
純 DOM 操作(如果是外部腳本)。
componentDidMount(){
fetch("url").then(res => res.text()).then(text => {
const script = document.createElement("script");
script.innerHTML = text;
document.head.appendChild(script);
}
您可以運(yùn)行下面的堆棧片段以查看動畫效果。
class Loading extends React.Component {
componentDidMount(){
fetch("https://codepen.io/nithinthampi/pen/JjjOQVv.js").then(res => res.text()).then(text => {
const script = document.createElement("script");
script.innerHTML = text;
document.head.appendChild(script);
}
);
}
render() {
return (
<div>
<div className="container">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 600 600">
<title>clock coffee cup</title>
<defs>
<clipPath id="cupMask">
<path className="cupMask" d="M215.65,214.41c0,19.85,37.76,35.94,84.35,35.94s84.35-16.09,84.35-35.94H506V399H145V214.41h70.65Z" fill="#ff0d0d"/>
</clipPath>
<clipPath id="handleMask">
<path className="handleMask" fill="#4BFF00" d="M475,305c-23.7-2.4-104.6,3.9-104.6,3.9s12.1-11.9,13.9-46.2c0,0,2.3-39.9,0-48.3
c9.9,0,90.6,0,90.6,0V305z"/>
</clipPath>
</defs>
<g className="cupGroup">
<ellipse className="ripple" cx="300" cy="214.41" rx="84.35" ry="35.94" fill="rgba(0,0,0,0)" strokeLinecap="round" strokeMiterlimit="10" strokeWidth="6"/>
<ellipse className="ripple" cx="300" cy="214.41" rx="84.35" ry="35.94" fill="rgba(0,0,0,0)" strokeLinecap="round" strokeMiterlimit="10" strokeWidth="6"/>
<g clipPath="url(#cupMask)">
<path id="base" d="M216,214v48.7
c0,46.4,37.8,84.4,84.2,84.4h-0.3c46.4,0,84.1-38,84.1-84.4V214" fill="none" strokeLinecap="round" strokeMiterlimit="10" strokeWidth="14"/>
</g>
<g clipPath="url(#handleMask)">
<path opacity="1" id="handle" d="M384.5,228.7c15.9,0,27.8,13.6,27.8,31.5s-14.9,30.5-30.8,30.5" fill="none" strokeLinecap="round" strokeMiterlimit="10" strokeWidth="14"/>
</g>
<ellipse id="rim" cx="300" cy="214.41" rx="84.35" ry="35.94" fill="rgba(0,0,0,0)" strokeLinecap="round" strokeMiterlimit="10" strokeWidth="14"/>
</g>
<g className="clockGroup" opacity="1">
<line id="bighand" fill="none" strokeWidth="14" strokeLinecap="round" strokeMiterlimit="10" x1="300" y1="263" x2="300" y2="189"/>
<line id="littlehand" fill="none" strokeWidth="14" strokeLinecap="round" strokeMiterlimit="10" x1="300" y1="263" x2="300" y2="221"/>
</g>
<line id="table" x1="235" y1="376" x2="365" y2="376" fill="none" strokeLinecap="round" strokeMiterlimit="10" strokeWidth="14"/>
</svg>
</div>
</div>
);
}
}
ReactDOM.render(<Loading />, document.getElementById("root"));
body {
background-color:#FFF9ED;
overflow: hidden;
}
body,
html {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
.container{
position:absolute;
width:600px;
}
svg{
visibility:hidden;
}
line, ellipse, path{
stroke:#574227;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css"rel="stylesheet">
<script src="//cdnjs.cloudflare.com/ajax/libs/gsap/1.19.1/TweenMax.min.js"></script>
<div id="root"></div>
請注意,您使用的所有插件都不是開源的。
https://codepen.io/GreenSock/full/OPqpRJ/

TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個贊
這是因?yàn)槟_本clock.js在Loading組件掛載到 dom之前執(zhí)行。container = select('.container')并且cupGroup = select('.cupGroup')將獲得空。所以注意正在頁面上呈現(xiàn)。
你應(yīng)該在EXCUTE腳本clock.js的生命周期componentDidMount。
時鐘.js
export funtion initClock() {
// write the script in clock.js here
}
加載.jsx
import {initClock} from '.js/clock.js'
class Loading extends Component {
render() {}
componentDidMount() {
initClock();
}
}

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個贊
您可以將樣式導(dǎo)入到頁面,例如:
import clockCSS from 'styles/App.module.css';
在頁面中,您可以將樣式類指定為:
className={clockCSS.container}
在 style.css 文件中,您必須更新定義樣式類的方式:
:local(.container){
position:absolute;
width:600px;
}
添加回答
舉報