做網站一定會有遇到需要做捲動一次滑鼠滾輪頁面就捲動一頁的效果,如果要這個效果可以參考 Pageable 這隻 javascript library。
使用方法就是讀入後:
https://unpkg.com/pageable@latest/dist/pageable.min.js
配置 html
<div id="container">
<div data-anchor="Page 1"></div>
<div data-anchor="Page 2"></div>
<div data-anchor="Page 3"></div>
<div data-anchor="Page 4"></div>
....
</div>
再去使用它就好了
new Pageable("#container");
可以參考文件帶入需要的參數客製化。
new Pageable("#container", { childSelector: "[data-anchor]" // CSS3 selector string for the pages anchors: [], // define the page anchors pips: true, // display the pips animation: 300, // the duration in ms of the scroll animation delay: 0, // the delay in ms before the scroll animation starts throttle: 50, // the interval in ms that the resize callback is fired orientation: "vertical", // or horizontal swipeThreshold: 50, // swipe / mouse drag distance (px) before firing the page change event freeScroll: false, // allow manual scrolling when dragging instead of automatically moving to next page navPrevEl: false, // define an element to use to scroll to the previous page (CSS3 selector string or Element reference) navNextEl: false, // define an element to use to scroll to the next page (CSS3 selector string or Element reference) infinite: false, // enable infinite scrolling (from 0.4.0) slideshow: { // enable slideshow that cycles through your pages automatically (from 0.4.0) interval: 3000, // time in ms between page change, delay: 0 // delay in ms after the interval has ended and before changing page }, events: { wheel: true, // enable / disable mousewheel scrolling mouse: true, // enable / disable mouse drag scrolling touch: true, // enable / disable touch / swipe scrolling keydown: true, // enable / disable keyboard navigation }, easing: function(currentTime, startPos, endPos, interval) { // the easing function used for the scroll animation return -endPos * (currentTime /= interval) * (currentTime - 2) + startPos; }, onInit: function() { // do something when the instance is ready }, onUpdate: function() { // do something when the instance updates }, onBeforeStart: function() { // do something before scrolling begins }, onStart: function() { // do something when scrolling begins }, onScroll: function() { // do something during scroll }, onFinish: function() { // do something when scrolling ends }, });