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 all commits
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
97 changes: 79 additions & 18 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,37 @@ Object.defineProperty(exports, "__esModule", {
value: true
});

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; /**
* 在页面滚动的时候,监听滚动事件,在快要到达底部指定距离的时候,执行相应函数
* 如果传入 totalPages, 则会在鼠标滚动时
*
* <ReactScrollPagination
fetchFunc = {targetFuc}
totalPages = {totalPages}
/>
*/

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var jQuery = require('jquery'); /**
* 在页面滚动的时候,监听滚动事件,在快要到达底部指定距离的时候,执行相应函数
* 如果传入 totalPages, 则会在鼠标滚动时
*
* <ReactScrollPagination
fetchFunc = {targetFuc}
totalPages = {totalPages}
/>
*/
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]),
excludeTopMargin: _react.PropTypes.oneOfType([_react.PropTypes.number, // The height value which should be excluded from scrollTop calculation
_react.PropTypes.string]),
excludeElement: _react.PropTypes.string, // The element selector which should be excluded from calculation
excludeHeight: _react.PropTypes.oneOfType([_react.PropTypes.number, // the height value which should be excluded from calculation
_react.PropTypes.string]),
Expand All @@ -36,23 +43,28 @@ 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,
excludeTopMargin: null,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@malikid
Thanks so much for your pr, one question, is the prop excludeTopMargin works same as the prop excludeHeight, if so, maybe you can comment hte excludeHeight as deprecated.

excludeHeight: null,
triggerAt: null,
showTime: null,
defaultShowTime: 2000,
defaultTrigger: 30,
defaultExcludeTopMargin: 0,
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 @@ -68,7 +80,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 @@ -88,6 +104,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 All @@ -100,6 +117,24 @@ var ReactScrollPagination = _react2.default.createClass({
return showTime;
},

getExcludeTopMargin: function getExcludeTopMargin() {
// 获取需要减去的高度
var excludeTopMargin = this.isolate.defaultExcludeTopMargin;

if (this.props.excludeTopMargin) {
var propsExcludeTopMargin = parseInt(this.props.excludeTopMargin);
if (isNaN(propsExcludeTopMargin)) {
console.error('WARNING: Failed to convert the props "excludeTopMargin" with value: "' + this.props.excludeTopMargin + '" to Number, please verify. Will take "' + this.isolate.defaultExcludeTopMargin + '" by default.');
} else {
excludeTopMargin = propsExcludeTopMargin;
}
}

this.isolate.excludeTopMargin = excludeTopMargin;

return excludeTopMargin;
},

getExcludeHeight: function getExcludeHeight() {
// 获取需要减去的高度
var excludeHeight = this.isolate.defaultExcludeHeight;
Expand Down Expand Up @@ -142,7 +177,7 @@ var ReactScrollPagination = _react2.default.createClass({
},

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

/*
* 当totalPages第一次有值时,表明List是初次加载,此时计算页面的高度,并将其作为单页的高度
Expand All @@ -155,8 +190,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 - this.isolate.excludeTopMargin;

if (this.isolate.onePageHeight !== null) {
var currentPage = Math.ceil(scrollTop / this.isolate.onePageHeight) || 1;
Expand All @@ -166,10 +201,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 @@ -182,26 +217,52 @@ var ReactScrollPagination = _react2.default.createClass({

validateAndSetPropValues: function validateAndSetPropValues() {
this.isolate.triggerAt = this.getTriggerAt();
this.isolate.excludeTopMargin = this.getExcludeTopMargin();
this.isolate.excludeHeight = this.getExcludeHeight();
this.isolate.showTime = this.getShowTime();
},

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) {
function extend() {
return _extend.apply(this, arguments);
}

extend.toString = function () {
return _extend.toString();
};

return extend;
}(function () {
for (var i = 1; i < arguments.length; i++) {
for (var key in arguments[i]) {
if (arguments[i].hasOwnProperty(key)) {
if (_typeof(arguments[0][key]) === 'object' && _typeof(arguments[i][key]) === 'object') {
extend(arguments[0][key], arguments[i][key]);
} else {
arguments[0][key] = arguments[i][key];
}
}
}
}
return arguments[0];
}),

render: function render() {
// if no totalPages presented, will only do the fetchings
if (typeof this.props.totalPages === 'undefined') {
return null;
}

var acutalPageContentDivStyle = jQuery.extend({}, this.props.innerDivStyle || this.pageContentStyle);
var acutalPageContentDivStyle = this.extend({}, this.props.innerDivStyle || this.pageContentStyle);

// always set the opacity for inner div, so they are able to make the transition
if (!this.state.showPageStatus) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"babel-preset-react": "^6.5.0",
"eslint": "^2.8.0",
"eslint-plugin-flow-vars": "^0.3.0",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"jest-cli": "*",
"jscs": "^3.0.3",
Expand Down
Loading