【问题标题】:How can I fade a navbar with a scroll down with React?如何使用 React 向下滚动淡化导航栏?
【发布时间】:2018-03-24 23:46:09
【问题描述】:

使用 jQuery 和 React 来解决动画问题是不是真的是个坏主意?使用带有状态的 css 类会更好吗?

我有这个带有导航栏菜单的模板,但我想通过向下滚动栏来执行 jquery 淡入淡出,这是我的模板有问题或者我该如何添加这样的效果

我有一个名为landing.js 的主模板

import React from 'react';

import NavbarBoots from './Vitae-Plantilla/NavbarBoots';
import NavbarLanding from './Landing-Plantilla/NavbarLanding';
import CabeceraLanding from './Landing-Plantilla/CabeceraLanding';
import CuerpoLanding from './Landing-Plantilla/CuerpoLanding';
import PieLanding from './Landing-Plantilla/PieLanding';

export default class Landing extends React.Component {

    componentDidMount () {
        var scrollTop = $(window).scrollTop();
        $(window).scroll(function(){
            if(scrollTop > 100) {
                $('#.main_h').fadeOut();
            } else {
                $('#.main_h').fadeIn();
            }

        });
    }
    render() {

        return (
          <div className="container-landing">
                <header id=".main_h"className=".main_h">
                        <NavbarBoots/>
                    <div className="cabecera-landing">
                        <CabeceraLanding title="Cabecera" subtitle="el pais del nunca jamás."/>
                    </div>
                </header> 
                    <div className="body-landing-">
                        <div className="cuerpo-landing">
                            <CuerpoLanding title="Acerca de mi."/>
                        </div>
                        <div className="pie-landing">
                            <PieLanding title="pie"/>
                        </div>

                    </div>
          </div>
        ); 
    }; // render
} // Landing

这些是我页面的样式,但我怎样才能让导航栏也向下。

.container-landing {
  .main_h {
    background: url(http://www.vqronline.org/sites/vqr.virginia.edu/files/story-images/petrusich_honis_opener_cssp_aur-mw_oct2015_1.jpg) center no-repeat;

      position: fixed;
      top: 0px;
      max-height: 70px;
      z-index: 999;
      width: 100%;
      padding-top: 17px;
      background: none;
      overflow: hidden;
      -webkit-transition: all 0.3s;
      transition: all 0.3s;
      opacity: 0;
      top: -100px;
      padding-bottom: 6px;
      font-family: "Montserrat", sans-serif;
    }
    .main_h .sticky{
      background-color: rgba(255, 255, 255, 0.93);
      opacity: 1;
      top: 0px;
      border-bottom: 1px solid gainsboro;
    }
    @media only screen and (max-width: 766px) {
      .main_h {
        padding-top: 25px;
      }
    }

【问题讨论】:

  • 一般来说,我会说在可能的情况下使用 css 动画优于 jQuery 或任何 javascript 派生动画方法。原因是 css 本身实现的动画可能会受益于浏览器优化 gpu 的操作。通常,使用 javascript 完成的动画将涉及对 DOM 的频繁更改,这将导致频繁的页面重绘等,从性能的角度来看,以及动画的潜在流动性,这是一件坏事。这是我从自己的个人研究中收集到的。

标签: javascript jquery css reactjs


【解决方案1】:

有一种“反应友好”的方式来做到这一点(即,不用 jquery 操作 DOM 元素):

componentDidMount () {      
   window.onscroll =()=>{
       this.setState({currentScrollHeight: window.scrollY})
  }
}

唯一的问题是它会在每次滚动高度改变时重新渲染,即使只有 1 个像素。如果这太贵了,您可以将值四舍五入到最接近的值,例如 50:

componentDidMount () {      
   window.onscroll =()=>{
    const newScrollHeight = Math.ceil(window.scrollY / 50) *50;
    if (this.state.currentScrollHeight != newScrollHeight){
        this.setState({currentScrollHeight: newScrollHeight})
    }
  }
}

然后在渲染中,类似:

render(){
   const opacity = Math.min(100 / this.state.currentScrollHeight  , 1)

    return <div style={{opacity}} id='element-you-want-to-fade'> </div>
}

您可以试验这些值(更改 100,也许给它一个起始值),让它按照您想要的方式淡化。

或者,对于纯 CSS 库解决方案,您可能需要查看 http://scrollmagic.io/

除了将 jquery 与 react 混合之外,您所拥有的问题在于,fadeIn 和 fadeOut 从字面上完全显示/隐藏了一个元素,因此该元素将始终完全显示或隐藏(换句话说,它不会缓慢淡出)。

【讨论】:

  • 嗨 DZack 我在声明常量不透明度时遇到问题告诉我 TypeError: this.state 为 null,我该如何修复它
  • 你应该在组件的构造函数中声明你的状态:constructor(props){ this.state = { opacity: 0 } }
  • 使用状态来处理滚动 css 听起来是个坏主意
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-24
  • 1970-01-01
相关资源
最近更新 更多