javascript - 如何在反应组件中使用javascript
问题描述
我正在开发一个 React 组件,我发现了一个特定的 javascript 代码来修改我的 html 页面的内容。问题是我不知道如何将此脚本合并到我的组件中。我知道我可以用“import”语句调用它,但脚本可以使用“window.onload”函数,该函数只会被调用一次,但我的组件被安装了几次,脚本将不再工作
组件 -
import React, { Component } from 'react';
import textRotation from '../../scripts/textRotation';
import './Main.scss';
class Main extends Component {
constructor(props) {
super(props);
this.title = props.title;
this.imgSrc = props.imgSrc;
}
render() {
return (
<div className="Main">
<div className="main-content">
<div className="presentation-caption">
<span>Hello, I'm Jordan, a junior front-end developer, who does a lot of things.</span>
</div>
<div className="description-caption">
<span>Prepare you to encounter various types of projects... but it's ok, just explore !</span>
</div>
<div className="button-container">
<a href="#works"><button>Scroll down</button></a>
</div>
</div>
<div className="side-content">
<span
className="txt-rotate"
data-period="1100"
data-rotate='[ "Web development", "Video editing", "Motion design", "Graphism", "Creativity" ]'></span><span>.</span>
</div>
<div className="side-text-portfolio">
<span>ポ<br />ー<br />ト<br />フ<br />ォ<br />リ<br />オ</span>
</div>
</div>
);
}
}
export default Main;
剧本 -
var TxtRotate = function (el, toRotate, period) {
this.toRotate = toRotate;
this.el = el;
this.loopNum = 0;
this.period = parseInt(period, 10) || 2000;
this.txt = '';
this.tick();
this.isDeleting = false;
};
TxtRotate.prototype.tick = function () {
var i = this.loopNum % this.toRotate.length;
var fullTxt = this.toRotate[i];
if (this.isDeleting) {
this.txt = fullTxt.substring(0, this.txt.length - 1);
} else {
this.txt = fullTxt.substring(0, this.txt.length + 1);
}
this.el.innerHTML = '<span class="wrap">' + this.txt + '</span>';
var that = this;
var delta = 300 - Math.random() * 100;
if (this.isDeleting) { delta /= 2; }
if (!this.isDeleting && this.txt === fullTxt) {
delta = this.period;
this.isDeleting = true;
} else if (this.isDeleting && this.txt === '') {
this.isDeleting = false;
this.loopNum++;
delta = 500;
}
setTimeout(function () {
that.tick();
}, delta);
};
window.onload = function () {
var elements = document.getElementsByClassName('txt-rotate');
for (var i = 0; i < elements.length; i++) {
var toRotate = elements[i].getAttribute('data-rotate');
var period = elements[i].getAttribute('data-period');
if (toRotate) {
new TxtRotate(elements[i], JSON.parse(toRotate), period);
}
}
// INJECT CSS
var css = document.createElement("style");
css.type = "text/css";
css.innerHTML = ".txt-rotate > .wrap { border-right: 0.08em solid #725070 }";
document.body.appendChild(css);
};
如果您有任何将脚本添加到我的组件中的解决方案,或者正确地重新加载它......在此先感谢
解决方案
将脚本更改为:
var TxtRotate = function (el, toRotate, period) {
this.toRotate = toRotate;
this.el = el;
this.loopNum = 0;
this.period = parseInt(period, 10) || 2000;
this.txt = '';
this.tick();
this.isDeleting = false;
};
TxtRotate.prototype.tick = function () {
var i = this.loopNum % this.toRotate.length;
var fullTxt = this.toRotate[i];
if (this.isDeleting) {
this.txt = fullTxt.substring(0, this.txt.length - 1);
} else {
this.txt = fullTxt.substring(0, this.txt.length + 1);
}
this.el.innerHTML = '<span class="wrap">' + this.txt + '</span>';
var that = this;
var delta = 300 - Math.random() * 100;
if (this.isDeleting) { delta /= 2; }
if (!this.isDeleting && this.txt === fullTxt) {
delta = this.period;
this.isDeleting = true;
} else if (this.isDeleting && this.txt === '') {
this.isDeleting = false;
this.loopNum++;
delta = 500;
}
setTimeout(function () {
that.tick();
}, delta);
};
loadCall = function () {
var elements = document.getElementsByClassName('txt-rotate');
for (var i = 0; i < elements.length; i++) {
var toRotate = elements[i].getAttribute('data-rotate');
var period = elements[i].getAttribute('data-period');
if (toRotate) {
new TxtRotate(elements[i], JSON.parse(toRotate), period);
}
}
// INJECT CSS
var css = document.createElement("style");
css.type = "text/css";
css.innerHTML = ".txt-rotate > .wrap { border-right: 0.08em solid #725070 }";
document.body.appendChild(css);
};
const loadMyScript = () => window.addEventListener('load', () => loadCall());
export default loadMyScript;
然后从组件中的函数导入loadMyScript
和调用。loadMyScript()
componentDidMount()
推荐阅读
- querydsl - 我可以使用 queryDSL 添加条件字段吗?(如mybatis)
- swift - 如何使用 ARKit 和 RealityKit 检测 2D 图像?
- node.js - 我得到一个 TypeError: mongoose.model is not a function。我如何解决它?
- ejabberd - ejabberdctl mod_http_bind 模块不可用
- c# - double value 使用 Dapper DynamicParameters 保存到 MS SQL 后删除比例值
- html - 在子组件中使用 *ngIf 动态更改类
- sql - 如何在 Amazon Athena 中查询 json?
- python - 如何从文本文件中保存特定数据并使用 python 保存结果数据
- android - Android当活动失去焦点并被通知带回来时,声音将不再播放
- node.js - 对 node_modules 中的文件进行更改是否会重新启动 nodemon?