【问题标题】:How to display navbar when scrolled with WayPoint使用 WayPoint 滚动时如何显示导航栏
【发布时间】:2020-07-18 17:05:32
【问题描述】:

所以当我滚动到实际导航栏下方时,我试图将我的导航栏固定在屏幕顶部。当我滚动过去时,我正在使用 WayPoints onEnter 和 onLeave 来触发。我遇到的问题是,当 styled 等于 false 时,我认为 {position:'relative'} 会使网站崩溃,我不确定为什么,或者解决方案是什么。

import React, { useEffect, useState } from 'react'
import Navbar from './Navbar'
import { Box, Grid, Card, CardMedia, CardActionArea, Typography, CardContent, CardActions, Button }             
from '@material-ui/core'
import Styles from './Styles'
import { Random } from 'react-animated-text'
import { projectSections } from './ListItems' 
import { Waypoint } from 'react-waypoint';

const Portfolio = () => {

    const [styled, setStyled] = useState(false)

    const style = () => (
        styled==false
        ? {
            position:'fixed',
            width:'100%',
            zIndex:99
        }
        : {position:'relative'}
    )

    const handleWaypointEnter = () => (
        setStyled(!styled) )

    const handleWaypointLeave = () => (
        setStyled(!styled)
        )

    return (
    <>
        <Waypoint
        onEnter={handleWaypointEnter}
        onLeave={handleWaypointLeave}
        >
        <div style={style()}>
            <Navbar/>
        </div>
        </Waypoint>

    {/* Ignore this for now
        <Box style={{position:'relative'}}>
            <Box component='div'>
                <Typography align='center' className={classes.portTitle}>
                    <Random 
                    className={classes.portTitle}
                    text="PORTFOLIO"
                    effect='pop'
                    effectChange={.8}
                    effectDuration={1.3}
                    />
                </Typography>
                <Grid container justify='center' alignItems='center' style={{padding:20}}>
                    {project()}
                </Grid>
            </Box>
        </Box>
    */}
    </>
)

}

【问题讨论】:

    标签: css reactjs scroll waypoint


    【解决方案1】:

    你有没有尝试在 style 函数中使用 return 语句。

    【讨论】:

    • 我做到了,不幸的是同样的问题仍然存在
    • 在样式函数的条件语句前添加'return'
    猜你喜欢
    • 2021-02-07
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多