-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
2 lines (2 loc) · 1.96 KB
/
index.js
1
2
"use strict";var React=require("react"),PropTypes=require("prop-types"),createReactClass=require("create-react-class"),ReactDOM=require("react-dom"),util=require("dom-find"),StickyDiv=createReactClass({displayName:"StickyDiv",propTypes:{offsetTop:PropTypes.number,zIndex:PropTypes.number,className:PropTypes.string},getDefaultProps:function(){return{offsetTop:0,className:"",zIndex:9999}},getInitialState:function(){return{fix:!1,width:null}},handleResize:function(){this.checkWidth(),this.checkPositions()},onScroll:function(){this.checkWidth(),this.checkPositions()},checkPositions:function(){util.findPosRelativeToViewport(ReactDOM.findDOMNode(this))[1]<=this.props.offsetTop?this.setState({fix:!0}):this.setState({fix:!1})},checkWidth:function(){var e=null;e=this.refs.duplicate?this.refs.duplicate.getBoundingClientRect().width:this.refs.original.getBoundingClientRect().width,this.state.width!==e&&this.setState({width:e})},componentDidMount:function(){window.addEventListener("scroll",this.onScroll,!1),window.addEventListener("resize",this.handleResize),this.checkWidth()},componentWillUnmount:function(){window.removeEventListener("scroll",this.onScroll,!1),window.removeEventListener("resize",this.handleResize)},render:function(){var e;return this.state.fix?(e={display:"block",position:"fixed",width:this.state.width?this.state.width+"px":null,top:this.props.offsetTop},React.createElement("div",{style:{zIndex:this.props.zIndex,position:"relative",width:"100%"}},React.createElement("div",{ref:"duplicate",key:"duplicate",style:{visibility:"hidden"}},this.props.children),React.createElement("div",{ref:"original",key:"original",className:this.props.className,style:e},this.props.children))):(e={display:"block",position:"relative"},React.createElement("div",{style:{zIndex:this.props.zIndex,position:"relative",width:"100%"}},React.createElement("div",{ref:"original",key:"original",style:e},this.props.children)))}});module.exports=StickyDiv;
//# sourceMappingURL=dist/react-stickydiv.min.js.map