Skip to content

Fixed can't work if this component is used in sub container. #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed can't work if this component is used in sub container.
  • Loading branch information
malikid committed May 31, 2017
commit 35f852dc9800234c179ec0c4f2d1dc46379f7218
27 changes: 19 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ var jQuery = require('jquery');
var ReactScrollPagination = _react2.default.createClass({
displayName: 'ReactScrollPagination',


propTypes: {
fetchFunc: _react.PropTypes.func.isRequired,
totalPages: _react.PropTypes.number,
windowElement: _react.PropTypes.string, // The element selector which contains the list container and is responsible for scrolling
documentElement: _react.PropTypes.string, // The element selector which contains the list
paginationShowTime: _react.PropTypes.oneOfType([_react.PropTypes.number, // How long shall the pagination div shows
_react.PropTypes.string]),
excludeElement: _react.PropTypes.string, // The element selector which should be excluded from calculation
Expand All @@ -38,6 +41,7 @@ var ReactScrollPagination = _react2.default.createClass({
triggerAt: _react.PropTypes.oneOfType([_react.PropTypes.number, // The distance to trigger the fetchfunc
_react.PropTypes.string])
},

isolate: {
onePageHeight: null,
timeoutFuncHandler: null,
Expand All @@ -48,13 +52,15 @@ var ReactScrollPagination = _react2.default.createClass({
defaultTrigger: 30,
defaultExcludeHeight: 0
},

pageDivStle: {
position: 'fixed',
bottom: '15px',
left: 0,
right: 0,
textAlign: 'center'
},

pageContentStyle: {
display: 'inline-block',
background: 'rgba(6, 6, 6, 0.54)',
Expand All @@ -70,7 +76,11 @@ var ReactScrollPagination = _react2.default.createClass({
OTransition: 'opacity 0.8s',
transition: 'opacity 0.8s'
},

getInitialState: function getInitialState() {
this.windowElement = this.props.windowElement || window;
this.documentElement = this.props.documentElement || document;

return {
currentPage: 1,
totalPages: null,
Expand All @@ -90,6 +100,7 @@ var ReactScrollPagination = _react2.default.createClass({
_this.setState({ showPageStatus: false });
}, this.isolate.showTime);
},

getShowTime: function getShowTime() {
var showTime = this.isolate.defaultShowTime;
if (this.props.paginationShowTime) {
Expand Down Expand Up @@ -144,7 +155,7 @@ var ReactScrollPagination = _react2.default.createClass({
},

getOnePageHeight: function getOnePageHeight() {
var documentHeight = jQuery(document).height();
var documentHeight = jQuery(this.documentElement).height();

/*
* 当totalPages第一次有值时,表明List是初次加载,此时计算页面的高度,并将其作为单页的高度
Expand All @@ -157,8 +168,8 @@ var ReactScrollPagination = _react2.default.createClass({
handlePagePosition: function handlePagePosition() {
this.getOnePageHeight();

var windowHeight = jQuery(window).height();
var scrollTop = jQuery(window).scrollTop() + windowHeight - this.isolate.excludeHeight;
var windowHeight = jQuery(this.windowElement).height();
var scrollTop = jQuery(this.windowElement).scrollTop() + windowHeight - this.isolate.excludeHeight;

if (this.isolate.onePageHeight !== null) {
var currentPage = Math.ceil(scrollTop / this.isolate.onePageHeight) || 1;
Expand All @@ -168,10 +179,10 @@ var ReactScrollPagination = _react2.default.createClass({
},

scrollHandler: function scrollHandler() {
var documentHeight = jQuery(document).height();
var documentHeight = jQuery(this.documentElement).height();

var windowHeight = jQuery(window).height();
var scrollBottom = jQuery(window).scrollTop() + windowHeight;
var windowHeight = jQuery(this.windowElement).height();
var scrollBottom = jQuery(this.windowElement).scrollTop() + windowHeight;
var triggerBottom = scrollBottom + this.isolate.triggerAt;

// 当滚动条距离底部距离小于30像素的时候出发请求操作
Expand All @@ -189,12 +200,12 @@ var ReactScrollPagination = _react2.default.createClass({
},

componentWillUnmount: function componentWillUnmount() {
jQuery(window).unbind('scroll', this.scrollHandler);
jQuery(this.windowElement).unbind('scroll', this.scrollHandler);
},

componentDidMount: function componentDidMount() {
this.validateAndSetPropValues();
jQuery(window).scroll(this.scrollHandler);
jQuery(this.windowElement).scroll(this.scrollHandler);
},

extend: function (_extend) {
Expand Down
29 changes: 20 additions & 9 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import React, { PropTypes } from 'react'
const jQuery = require('jquery')

const ReactScrollPagination = React.createClass({

propTypes: {
fetchFunc: PropTypes.func.isRequired,
totalPages: PropTypes.number,
windowElement: PropTypes.string, // The element selector which contains the list container and is responsible for scrolling
documentElement: PropTypes.string, // The element selector which contains the list
paginationShowTime: PropTypes.oneOfType([
PropTypes.number, // How long shall the pagination div shows
PropTypes.string
Expand All @@ -31,6 +34,7 @@ const ReactScrollPagination = React.createClass({
PropTypes.string
]),
},

isolate: {
onePageHeight: null,
timeoutFuncHandler: null,
Expand All @@ -41,13 +45,15 @@ const ReactScrollPagination = React.createClass({
defaultTrigger: 30,
defaultExcludeHeight: 0
},

pageDivStle: {
position: 'fixed',
bottom: '15px',
left: 0,
right: 0,
textAlign: 'center'
},

pageContentStyle: {
display: 'inline-block',
background: 'rgba(6, 6, 6, 0.54)',
Expand All @@ -63,7 +69,11 @@ const ReactScrollPagination = React.createClass({
OTransition: 'opacity 0.8s',
transition: 'opacity 0.8s'
},

getInitialState: function () {
this.windowElement = this.props.windowElement || window
this.documentElement = this.props.documentElement || document

return {
currentPage: 1,
totalPages: null,
Expand All @@ -81,6 +91,7 @@ const ReactScrollPagination = React.createClass({
this.setState({showPageStatus: false})
}, this.isolate.showTime)
},

getShowTime: function () {
let showTime = this.isolate.defaultShowTime
if (this.props.paginationShowTime) {
Expand Down Expand Up @@ -126,7 +137,7 @@ const ReactScrollPagination = React.createClass({
let triggerAt = this.isolate.defaultTrigger

if (this.props.triggerAt) {
triggerAt= parseInt(this.props.triggerAt)
triggerAt = parseInt(this.props.triggerAt)

if (isNaN(triggerAt)) {
triggerAt = this.isolate.defaultTrigger
Expand All @@ -140,7 +151,7 @@ const ReactScrollPagination = React.createClass({
},

getOnePageHeight: function () {
const documentHeight = jQuery(document).height()
const documentHeight = jQuery(this.documentElement).height()

/*
* 当totalPages第一次有值时,表明List是初次加载,此时计算页面的高度,并将其作为单页的高度
Expand All @@ -153,8 +164,8 @@ const ReactScrollPagination = React.createClass({
handlePagePosition: function () {
this.getOnePageHeight()

let windowHeight = jQuery(window).height()
let scrollTop = jQuery(window).scrollTop() + windowHeight - this.isolate.excludeHeight
let windowHeight = jQuery(this.windowElement).height()
let scrollTop = jQuery(this.windowElement).scrollTop() + windowHeight - this.isolate.excludeHeight

if (this.isolate.onePageHeight !== null) {
let currentPage = Math.ceil(scrollTop / this.isolate.onePageHeight) || 1
Expand All @@ -164,10 +175,10 @@ const ReactScrollPagination = React.createClass({
},

scrollHandler: function () {
let documentHeight = jQuery(document).height()
let documentHeight = jQuery(this.documentElement).height()

let windowHeight = jQuery(window).height()
let scrollBottom = jQuery(window).scrollTop() + windowHeight
let windowHeight = jQuery(this.windowElement).height()
let scrollBottom = jQuery(this.windowElement).scrollTop() + windowHeight
let triggerBottom = scrollBottom + this.isolate.triggerAt

// 当滚动条距离底部距离小于30像素的时候出发请求操作
Expand All @@ -185,12 +196,12 @@ const ReactScrollPagination = React.createClass({
},

componentWillUnmount: function () {
jQuery(window).unbind('scroll', this.scrollHandler)
jQuery(this.windowElement).unbind('scroll', this.scrollHandler)
},

componentDidMount: function () {
this.validateAndSetPropValues()
jQuery(window).scroll(this.scrollHandler)
jQuery(this.windowElement).scroll(this.scrollHandler)
},

extend: function () {
Expand Down