【发布时间】:2020-04-18 09:32:35
【问题描述】:
我希望我的“emailForm”组件在用户滚动到“洗衣区”的开头后呈现在我的导航栏上,有人知道该怎么做吗?
我试过{window.scrollY > 500px ? EmailForm : "" }
但它不起作用,我对 react 还不是很熟悉,所以也许你能帮我找出正确的方法来实现它?
容器:
<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>
import React, {Component} from 'react';
// Components
import Section from '../components/styledComponents/section';
import EmailForm from "../components/emailForm"
import Navbar from './containers/navbar';
export default class Individuals extends Component {
render() {
const { iconStyle } = this;
return (
<NavBar/>
<main className="individuals">
{/*Header section*/}
<Section height="100vh" usedScreen="84px" className="your-laundromat-connected">
<div className="title">
<h1>
Title</span>.
</h1>
</div>
</div>
<div className="subscribe-container">
<div className="catchphrase-input">Prévenez-moi :</div>
<EmailForm/>
</div>
</div>
</Section>
{/*Laundry section*/}
<div className="container-laverie">
<h2>Votre temps est précieux. Utilisez-le autrement.</h2>
<p>Finie l'attente interminable à la laverie... grâce à l'application Lavigo vous reprenez le contrôle sur votre emploi du temps.</p>
{/* </div> */}
</div>
</main>
);
}
}
导航栏组件:
<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>
import React from 'react';
import INavbarState from '../containers/interfaces/Inavbar';
//Librairies
import $ from 'jquery'
import EmailForm from '../components/emailForm'
export default class Individuals extends React.PureComponent<{}, INavbarState | any> {
constructor(props: INavbarState | any) {
super(props);
this.state = {
showMenu: false,
scrollTop: true
};
}
componentDidMount() {
const that = this;
let $win = $(window);
$win.scroll(function () {
if ($win.scrollTop() == 0) {
if (!that.state.scrollTop) {
that.setState({ scrollTop: true })
}
} else {
if (that.state.scrollTop) {
that.setState({ scrollTop: false })
}
}
});
if(window.innerWidth <= 1024)
{
that.setState({showMenu:false})
// hide menubar
}else{
that.setState({showMenu:true})
// show menubar
}
window.onresize = function(event:any) {
if(window.innerWidth <= 1024)
{
that.setState({showMenu:false})
// hide menubar
}else{
that.setState({showMenu:true})
// show menubar
}
if (window.scrollY > 1000) {
}
};
ontoggle = () => {
if(window.innerWidth <= 1024)
{
this.setState({ showMenu: !this.state.showMenu })
// hide menubar
}else{
this.setState({showMenu:true})
// show menubar
}
}
}
render() {
return (
<nav className="nav">
<div className="d-flex align-items-center">
<div className="container-email">
<EmailForm/>
</div>
</div>
</nav>
);
};
}
【问题讨论】:
标签: javascript reactjs scroll dom-events