【发布时间】: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