Javascript 如何在滚动到页面中的某个位置后动态呈现元素?
我想我的“emailForm”组件呈现在我的导航栏后,用户滚动到“洗衣区”的开始,有人知道怎么做吗 我尝试了Javascript 如何在滚动到页面中的某个位置后动态呈现元素?,javascript,reactjs,scroll,dom-events,Javascript,Reactjs,Scroll,Dom Events,我想我的“emailForm”组件呈现在我的导航栏后,用户滚动到“洗衣区”的开始,有人知道怎么做吗 我尝试了{window.scrollY>500px?电子邮件表单:'} 但是它不起作用,我对react还不是很熟悉,所以也许你可以帮我找到实现它的正确方法 容器: 从“React”导入React,{Component}; //组成部分 从“../components/styledComponents/Section”导入节; 从“./组件/EmailForm”导入EmailForm 从“./c
{window.scrollY>500px?电子邮件表单:'}
但是它不起作用,我对react还不是很熟悉,所以也许你可以帮我找到实现它的正确方法
容器:
从“React”导入React,{Component};
//组成部分
从“../components/styledComponents/Section”导入节;
从“./组件/EmailForm”导入EmailForm
从“./containers/Navbar”导入导航栏;
导出默认类扩展组件{
render(){
const{iconStyle}=这个;
返回(
{/*头段*/}
标题
佩雷斯·莫伊:
{/*洗衣房组*/}
这是一场暴风雨,利用了秋天。
完成无休止的拉维里之旅……为临时雇员提供免费服务
{/* */}
);
}
}
您好,请检查此ScrollExample和ScrollChild组件。我在ScrollExample组件中使用了ScrollChild。当您向下滚动到200时,屏幕上将显示ScrollChild组件
滚动示例组件
import * as React from "react";
import ScrollChild from "./ScrollChild";
export default class ScrollExample extends React.Component {
constructor(props) {
super(props);
this.state = {
isChildActive: false
}
}
componentDidMount() {
window.addEventListener('scroll', this.handleScroll);
}
componentWillUnmount() {
window.removeEventListener('scroll', this.handleScroll);
}
handleScroll = (event) => {
let scrollTop = window.scrollY;
if (scrollTop > 200)
this.setState({isChildActive: true});
else
this.setState({isChildActive: false});
};
render() {
return (
<div style={{height: 2000}}>
Scroll down to show the EmailForm
{
this.state.isChildActive ? <ScrollChild/> : null
}
</div>
);
}
}
import React, {useEffect, useState} from 'react';
function ScrollChild(props) {
return (
<div style={{backgroundColor: '#1569C7', color: '#ffffff', width: 500, marginTop: 300, padding: 20}}>
THIS IS A DUMMY TEXT
<h1>How to dynamically render an element after scrolling to a certain position in a page?</h1>
I would like my "emailForm" component to render on my Navbar after user scrolled to beginning of
"laundry section", does someone know how to do that?
I tried window.scrollY > 500px ? EmailForm : "" But it doesn't work, I am not very familiar yet with
react so maybe you could help me figure out the right method to achieve it?
I would like my "emailForm" component to render on my Navbar after user scrolled to beginning of
"laundry section", does someone know how to do that?
I tried window.scrollY > 500px ? EmailForm : "" But it doesn't work, I am not very familiar yet with
react so maybe you could help me figure out the right method to achieve it?
I would like my "emailForm" component to render on my Navbar after user scrolled to beginning of
"laundry section", does someone know how to do that?
I tried window.scrollY > 500px ? EmailForm : "" But it doesn't work, I am not very familiar yet with
react so maybe you could help me figure out the right method to achieve it?
I would like my "emailForm" component to render on my Navbar after user scrolled to beginning of
"laundry section", does someone know how to do that?
I tried window.scrollY > 500px ? EmailForm : "" But it doesn't work, I am not very familiar yet with
react so maybe you could help me figure out the right method to achieve it?
</div>
);
}
export default ScrollChild;
import*as React from“React”;
从“/ScrollChild”导入ScrollChild;
导出默认类ScrollExample扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
Ischildative:错误
}
}
componentDidMount(){
window.addEventListener('scroll',this.handleScroll);
}
组件将卸载(){
window.removeEventListener('scroll',this.handleScroll);
}
handleScroll=(事件)=>{
让scrollTop=window.scrollY;
如果(滚动顶部>200)
this.setState({ischildative:true});
其他的
this.setState({ischildative:false});
};
render(){
返回(
向下滚动以显示电子邮件表单
{
this.state.ischildative?:null
}
);
}
}
滚动子组件
import * as React from "react";
import ScrollChild from "./ScrollChild";
export default class ScrollExample extends React.Component {
constructor(props) {
super(props);
this.state = {
isChildActive: false
}
}
componentDidMount() {
window.addEventListener('scroll', this.handleScroll);
}
componentWillUnmount() {
window.removeEventListener('scroll', this.handleScroll);
}
handleScroll = (event) => {
let scrollTop = window.scrollY;
if (scrollTop > 200)
this.setState({isChildActive: true});
else
this.setState({isChildActive: false});
};
render() {
return (
<div style={{height: 2000}}>
Scroll down to show the EmailForm
{
this.state.isChildActive ? <ScrollChild/> : null
}
</div>
);
}
}
import React, {useEffect, useState} from 'react';
function ScrollChild(props) {
return (
<div style={{backgroundColor: '#1569C7', color: '#ffffff', width: 500, marginTop: 300, padding: 20}}>
THIS IS A DUMMY TEXT
<h1>How to dynamically render an element after scrolling to a certain position in a page?</h1>
I would like my "emailForm" component to render on my Navbar after user scrolled to beginning of
"laundry section", does someone know how to do that?
I tried window.scrollY > 500px ? EmailForm : "" But it doesn't work, I am not very familiar yet with
react so maybe you could help me figure out the right method to achieve it?
I would like my "emailForm" component to render on my Navbar after user scrolled to beginning of
"laundry section", does someone know how to do that?
I tried window.scrollY > 500px ? EmailForm : "" But it doesn't work, I am not very familiar yet with
react so maybe you could help me figure out the right method to achieve it?
I would like my "emailForm" component to render on my Navbar after user scrolled to beginning of
"laundry section", does someone know how to do that?
I tried window.scrollY > 500px ? EmailForm : "" But it doesn't work, I am not very familiar yet with
react so maybe you could help me figure out the right method to achieve it?
I would like my "emailForm" component to render on my Navbar after user scrolled to beginning of
"laundry section", does someone know how to do that?
I tried window.scrollY > 500px ? EmailForm : "" But it doesn't work, I am not very familiar yet with
react so maybe you could help me figure out the right method to achieve it?
</div>
);
}
export default ScrollChild;
import React,{useffect,useState}来自“React”;
函数滚动子(道具){
返回(
这是一个虚拟文本
如何在滚动到页面中的某个位置后动态呈现元素?
我想我的“emailForm”组件呈现在我的导航栏后,用户滚动到的开始
“洗衣房”,有人知道怎么做吗?
我试过window.scrollY>500px?EmailForm:“”但不起作用,我还不太熟悉它
那么你能帮我找出正确的方法来实现它吗?
我想我的“emailForm”组件呈现在我的导航栏后,用户滚动到的开始
“洗衣房”,有人知道怎么做吗?
我试过window.scrollY>500px?EmailForm:“”但不起作用,我还不太熟悉它
那么你能帮我找出正确的方法来实现它吗?
我想我的“emailForm”组件呈现在我的导航栏后,用户滚动到的开始
“洗衣房”,有人知道怎么做吗?
我试过window.scrollY>500px?EmailForm:“”但不起作用,我还不太熟悉它
那么你能帮我找出正确的方法来实现它吗?
我想我的“emailForm”组件呈现在我的导航栏后,用户滚动到的开始
“洗衣房”,有人知道怎么做吗?
我试过window.scrollY>500px?EmailForm:“”但不起作用,我还不太熟悉它
那么你能帮我找出正确的方法来实现它吗?
);
}
导出默认滚动子对象;
我现在没有示例代码,但您可以这样做:
滚动时,您可以检查元素是否可见(带有位置和滚动位置)。如果是,您可以在每次状态更改时执行jQuery动画,它将调用组件的渲染。但是在渲染中,不会有任何更改,因为渲染不依赖于此.state.scrollTop 要解决此问题,您需要做两件事: 1.当用户滚动超过阈值(500px)时更新状态 2.在
render()
方法中使用此新状态值重新渲染组件
我创建了一个示例,当this.state.showEmailForm
更改时,我会更改表单的不透明度。您可以在下面的示例中看到和看到:
类应用程序扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
showmailform:false,
};
}
componentDidMount=()=>{
window.addEventListener(“滚动”,this.onScroll);
};
组件将卸载=()=>{
removeEventListener(“滚动”,this.onScroll);
};
onScroll=()=>{
这是我的国家({
showEmailForm:window.scrollY>500,
});
};
render(){
返回(
{" "}
Lorem ipsum dolor sit amet,为精英献身
艾库利斯·泰勒斯是福西布斯。佩伦茨克坐在阿梅特·托托的位子上
马蒂斯:不含酒精、调味品、维他命、流苏
医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院医学院
福西布斯·布兰迪特。弗林蒂利亚·内克坐在埃利芬德。纳拉姆
埃尼姆·奥古斯(enim augue),设施是一个交通工具id,是一位智者
这是一个很好的地方,一个康格舞团
nibh laoreet id.在viverra ligula的Concertetur中,有一个mattis dolor blandit
阿里先生