You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 3 години
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # react-scroll-to-component
  2. Smooth srolls to react component via reference
  3. ## Install
  4. #### With npm
  5. ```sh
  6. npm install react-scroll-to-component --save
  7. ```
  8. #### With Yarn
  9. ```sh
  10. yarn add react-scroll-to-component
  11. ```
  12. ## `scrollToComponent(ref, <options>)`
  13. ##### Valid options:
  14. >
  15. ###### offset : *number*
  16. > Add an offset to the final position. if > 0 then page is moved to the bottom otherwise the page is moved to the top.
  17. ###### align : *string*
  18. > Alignment of the element. Can be one of `'top'`, `'middle'` or `'bottom'`. Defaulting to `'middle'`.
  19. ###### ease : *string*
  20. > Easing function defaulting to "out-circ" (view [ease](https://github.com/component/ease) for more)
  21. ###### duration : *number*
  22. > Animation duration defaulting to `1000`
  23. ## Example
  24. ```js
  25. import scrollToComponent from 'react-scroll-to-component';
  26. // without options
  27. scrollToComponent(this.refs.name);
  28. //with options
  29. scrollToComponent(this.refs.name, {
  30. offset: 1000,
  31. align: 'top',
  32. duration: 1500
  33. });
  34. ```
  35. ## [Demo](https://flyingant.github.io/react-scroll-to-component/)
  36. ```js
  37. import React, { Component } from 'react';
  38. import './App.css';
  39. import scrollToComponent from 'react-scroll-to-component';
  40. class App extends React.Component {
  41. componentDidMount() {
  42. scrollToComponent(this.Blue, { offset: 0, align: 'middle', duration: 500, ease:'inCirc'});
  43. }
  44. render() {
  45. return (
  46. <div className='main'>
  47. <div className='button_group'>
  48. <button onClick={() => scrollToComponent(this.Violet, { offset: 0, align: 'top', duration: 1500})}>Go To Violet</button>
  49. <button onClick={() => scrollToComponent(this.Indigo, { offset: 0, align: 'bottom', duration: 500, ease:'inExpo'})}>Go To Indigo</button>
  50. <button onClick={() => scrollToComponent(this.Blue, { offset: -200, align: 'middle', duration: 1500, ease:'inCirc'})}>Go To Blue</button>
  51. <button onClick={() => scrollToComponent(this.Green, { offset: 0, align: 'middle', duration: 500, ease:'inExpo'})}>Go To Green</button>
  52. <button onClick={() => scrollToComponent(this.Yellow, { offset: 0, align: 'top', duration: 1500, ease:'inCirc'})}>Go To Yellow</button>
  53. <button onClick={() => scrollToComponent(this.Orange, { offset: 0, align: 'top', duration: 500, ease:'inCirc'})}>Go To Orange</button>
  54. <button onClick={() => scrollToComponent(this.Red, { offset: 0, align: 'top', duration: 500})}>Go To Red</button>
  55. </div>
  56. <section className='violet' ref={(section) => { this.Violet = section; }}>Violet</section>
  57. <section className='indigo' ref={(section) => { this.Indigo = section; }}>Indigo</section>
  58. <section className='blue' ref={(section) => { this.Blue = section; }}>Blue</section>
  59. <section className='green' ref={(section) => { this.Green = section; }}>Green</section>
  60. <section className='yellow' ref={(section) => { this.Yellow = section; }}>Yellow</section>
  61. <section className='orange' ref={(section) => { this.Orange = section; }}>Orange</section>
  62. <section className='red' ref={(section) => { this.Red = section; }}>Red</section>
  63. <b>Inspired By <a href="https://hopechen1028.github.io/hopechen.me/" target="_blank">Hope</a> with Love and Peace</b>
  64. </div>
  65. )
  66. }
  67. }
  68. export default App;
  69. ```
  70. # License
  71. MIT