项目作者: kwzm

项目描述 :
An anchor react component use query string without hash. 一个使用 query string来实现 hash 功能的 react 组件。
高级语言: JavaScript
项目地址: git://github.com/kwzm/react-anchor-without-hash.git


English | 简体中文

react-anchor-without-hash

GitHub package.json version
Build Status
Coverage Status
npm bundle size
Commitizen friendly
A anchor react component use url search string without hash.
This component solves the problem that hash anchors cannot be used when using the hash router.
This is a good solution for projects that need to use hash routing compatible with older browsers (IE9) but want to use anchor points

Online Demo

https://kwzm.github.io/react-anchor-without-hash/

codesandbox

https://codesandbox.io/embed/react-anchor-without-hash-2xq2h

Install

  1. $ npm i react-anchor-without-hash

Usage

scrollIntoView(default)

The effect is the same as scrollIntoView).
When url search includes ‘_to=section1’, the section1 will scroll into view area.

  1. import Anchor from 'react-anchor-without-hash'
  2. // ......
  3. <Anchor name="section1">
  4. <div className="section section1">
  5. <h2>This is section1</h2>
  6. <div>There are some text...</div>
  7. </div>
  8. </Anchor>
  9. <Anchor name="section2">
  10. <div className="section section2">
  11. <h2>This is section2</h2>
  12. <div>There are some text...</div>
  13. </div>
  14. </Anchor>

scrollTop

The effect is the same as scrollTop).
When url search includes ‘_to=section1’, the section1 will scroll into view area with 50px top margin.

note:
1.Because offsetTop is used internally to get the height of the scroll, you need to make sure have a positioned containing element.
2.Interval can allow negative values

  1. import Anchor from 'react-anchor-without-hash'
  2. // ......
  3. const anchorProps = {
  4. type: 'scrollTop',
  5. container: '#container',
  6. interval: 50
  7. }
  8. <div style={{position: 'relative'}}>
  9. <Anchor name="section1" {...anchorProps}>
  10. <div className="section section1">
  11. <h2>This is section1</h2>
  12. <div>There are some text...</div>
  13. </div>
  14. </Anchor>
  15. <Anchor name="section2" {...anchorProps}>
  16. <div className="section section2">
  17. <h2>This is section2</h2>
  18. <div>There are some text...</div>
  19. </div>
  20. </Anchor>
  21. </div>

Options

common options

type: string

Specifies the mechanism for internal execution.

  • scrollIntoView: use element.scrollIntoView api
  • scrollTop: use element.scrollTop api

    anchorKey: string

    Url search key for the anchor, default is ‘_to’.

    scrollIntoView options

    scrollIntoViewOption: boolean | object

    Options passed when scrollIntoView is used.

    scrollTop options

    container: string

    Specifies which element performs scrollTop, if not, scrollTop is set by default using one of the following options:
  • document.body.scrollTop
  • document.documentElement.scrollTop
  • window.pageYOffset

    note: This option is the parameter for the document.querySelector, so choose the type it supports.

    interval: number

    Specifies the distance from the top, which defaults to 0.
    The actual scrollTop equals:

    1. document.querySelector(container).scrollTop = document.getElementById(anchor).offsetTop + interval

    License

    MIT