您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

32 行
1.3 KiB

  1. var ReactDOM = require('react-dom');
  2. var scroll = require('scroll-to');
  3. function calculateScrollOffset(element, offset, alignment) {
  4. var body = document.body,
  5. html = document.documentElement;
  6. var elementRect = element.getBoundingClientRect();
  7. var clientHeight = html.clientHeight;
  8. var documentHeight = Math.max( body.scrollHeight, body.offsetHeight,
  9. html.clientHeight, html.scrollHeight, html.offsetHeight );
  10. offset = offset || 0; // additional offset to top
  11. var scrollPosition;
  12. switch(alignment) {
  13. case 'top': scrollPosition = elementRect.top; break;
  14. case 'middle': scrollPosition = elementRect.bottom - clientHeight / 2 - elementRect.height / 2; break;
  15. case 'bottom': scrollPosition = elementRect.bottom - clientHeight; break;
  16. default: scrollPosition = elementRect.bottom - clientHeight / 2 - elementRect.height / 2; break; //defaul to middle
  17. }
  18. var maxScrollPosition = documentHeight - clientHeight;
  19. return Math.min(scrollPosition + offset + window.pageYOffset,
  20. maxScrollPosition);
  21. }
  22. module.exports = function (ref, options) {
  23. options = options || {
  24. offset: 0,
  25. align: 'middle'
  26. };
  27. var element = ReactDOM.findDOMNode(ref);
  28. if (element === null) return 0;
  29. return scroll(0, calculateScrollOffset(element, options.offset, options.align), options);
  30. };