前端 平滑滾動到底部/頂部

頁面滾動時,添加平滑特效css

1.在頁面入口處,添加css

1 html {
2     scroll-behavior: smooth;
3 }

添加全局css以後,直接使用window.scroll(0,0)就能夠平滑滾動到頂部了。html

注:兼容性不好,僅支持火狐、chrome高級版本chrome

2.指定滾動操做,使用平滑效果

平滑滾動到某塊元素的底部:scrollIntoView瀏覽器

1     let anchorElement = document.getElementById("softwareHeader-container");
2     let scrollIntoViewOptions: ScrollIntoViewOptions = {
3         block: 'end',
4         behavior: "smooth"
5     }
6     if (anchorElement) {
7         anchorElement.scrollIntoView(scrollIntoViewOptions)
8     }

或者平滑滾動到指定位置:ScrollToOptionsscrollTo函數

1   let scrollOptions: = {
2     left: 0,
3     top: 1000,
4     behavior:'smooth'
5   }
6 
7   window.scrollTo(scrollOptions);ScrollToOptions

兼容性:大部分支持,獵豹不支持。動畫

3.添加JS滾動代碼

滾動到頂部:spa

 1     returnTop = () => {
 2         //記錄當前執行動畫的標識
 3         let animationStepNumber;
 4         function exeucteAnimationByStep() {
 5             //當前頁面的滾動高度
 6             let currentScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
 7             if (currentScrollTop >= 4) {
 8                 animationStepNumber = window.requestAnimationFrame(exeucteAnimationByStep);
 9                 let scrollLocationChanging = currentScrollTop / 9;
10                 scrollLocationChanging = scrollLocationChanging > 1 ? scrollLocationChanging : 1;
11                 let newScrollTop = currentScrollTop - scrollLocationChanging;
12                 window.scrollTo(0, newScrollTop);
13             } else {
14                 window.cancelAnimationFrame(animationStepNumber);
15                 window.scrollTo(0, 0);
16             }
17         }
18         animationStepNumber = window.requestAnimationFrame(exeucteAnimationByStep);
19     }

滾動到底部:code

 1   scrollToPageBottom = () => {
 2     let animationStepNumber;
 3     function exeucteAnimationByStep() {
 4       let currentScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
 5       let totalHeight = (document.documentElement.scrollHeight || document.body.scrollHeight) - window.innerHeight;
 6       //剩餘的高度
 7       let scrollingHeight = totalHeight - currentScrollTop;
 8       if (scrollingHeight >= 4) {
 9         animationStepNumber = window.requestAnimationFrame(exeucteAnimationByStep);
10         //滾動變量
11         let scrollChangedHeight = scrollingHeight / 9;
12         scrollChangedHeight = scrollChangedHeight > 1 ? scrollChangedHeight : 1;
13         window.scrollTo(0, currentScrollTop + scrollChangedHeight);
14       } else {
15         window.cancelAnimationFrame(animationStepNumber);
16         window.scrollTo(0, totalHeight);
17       }
18     }
19     animationStepNumber = window.requestAnimationFrame(exeucteAnimationByStep);
20   };

原理:htm

requestAnimationFrame,告訴瀏覽器重繪時執行動畫,參數是具體執行方法,返回參數是nubmer(標識)
注:若是須要連續執行動畫,則須要在回調函數中,先添加一個待執行動畫回調requestAnimationFrame。(如上案例)
cancelAnimationFrame,取消待執行的動畫。
注:執行完全部動畫後,必定要註銷上一個動畫回調(若是有的話),不然會影響頁面滾動(由於回調函數中的動畫委託還在待處理呢)。

兼容性:平滑效果,支持全部瀏覽器。blog

缺陷:滾動過程當中,不能操做手動滾動頁面。這個缺陷,也有理論上的解法,能夠添加滾動事件監聽,若是滾動方向與平滑動畫效果方向相反,則取消平滑動畫的處理(調cancelAnimationFrame便可)

特別提示:添加純js平滑滾動方案,須要將第1個方案css全局的設置刪除了,不然滾動會很緩慢。

相關文章
相關標籤/搜索