项目作者: ederssouza

项目描述 :
ScrollSpy in pure JavaScript
高级语言: JavaScript
项目地址: git://github.com/ederssouza/vanillajs-scrollspy.git
创建时间: 2017-08-20T00:06:54Z
项目社区:https://github.com/ederssouza/vanillajs-scrollspy

开源协议:MIT License

下载


VanillaJS ScrollSpy

GitHub npm npm npm

ScrollSpy in pure JavaScript.

Installation

  1. $ npm install vanillajs-scrollspy --save

How to use

  • menu: HTML selector (#id, .class, …)
  • speed (optional): scroll speed, default value 2000
  • easing (optional): scroll type 'easeOutSine', 'easeInOutSine' or 'easeInOutQuint', default value 'easeOutSine'

ES6

  1. import VanillaScrollspy from 'vanillajs-scrollspy'
  2. const menu = document.querySelector('#navbar')
  3. const scrollspy = VanillaScrollspy({ menu })
  4. scrollspy.init()

UMD in Browser

  1. <script src="https://cdn.jsdelivr.net/npm/vanillajs-scrollspy@3.0.7/dist/vanillajs-scrollspy.min.js"></script>
  2. <!-- or -->
  3. <script src="./dist/vanillajs-scrollspy.min.js"></script>

After that the library will be available to the Global as VanillaScrollspy. Follow an example:

  1. const menu = document.querySelector('#navbar')
  2. const scrollspy = VanillaScrollspy({ menu })
  3. scrollspy.init()

Examples

Basic template

Available in public/index.html.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>VanillaJS ScrollSpy</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. }
  12. html, body {
  13. height: 100%;
  14. }
  15. nav {
  16. position: fixed;
  17. left: 0;
  18. right: 0;
  19. top: 0;
  20. }
  21. nav a.active {
  22. font-weight: bold;
  23. }
  24. .page {
  25. height: 100%;
  26. padding: 80px 0;
  27. width: 100%;
  28. background-color: #ddd;
  29. }
  30. .page:nth-child(even) { background-color: #fff; }
  31. </style>
  32. </head>
  33. <body>
  34. <nav id="navbar">
  35. <a href="#home" title="Home" class="active">Home</a>
  36. <a href="#portfolio" title="Portfolio">Portfolio</a>
  37. <a href="#about" title="About">About</a>
  38. <a href="#contact" title="Contact">Contact</a>
  39. </nav>
  40. <section id="home" class="page">
  41. <h2 class="title">Home</h2>
  42. </section>
  43. <section id="portfolio" class="page">
  44. <h2 class="title">Portfolio</h2>
  45. </section>
  46. <section id="about" class="page">
  47. <h2 class="title">About</h2>
  48. </section>
  49. <section id="contact" class="page">
  50. <h2 class="title">Contact</h2>
  51. </section>
  52. <script src="https://cdn.jsdelivr.net/npm/vanillajs-scrollspy@3.0.7/dist/vanillajs-scrollspy.min.js"></script>
  53. <script>
  54. const menu = document.querySelector('#navbar')
  55. const scrollspy = VanillaScrollspy({ menu })
  56. scrollspy.init()
  57. </script>
  58. </body>
  59. </html>

Controlling the speed

Choose a number greater than or equal to 1.

  1. const menu = document.querySelector('#navbar')
  2. const scrollspy = VanillaScrollspy({ menu, speed: 1000 })
  3. scrollspy.init()

Changing scroll type

  1. const menu = document.querySelector('#navbar')
  2. const scrollspy = VanillaScrollspy({ menu, speed: 1000, easing: 'easeInOutQuint' })
  3. scrollspy.init()

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details