原生js實(shí)現(xiàn)頁(yè)面滾動(dòng)動(dòng)畫(huà)
本文實(shí)例為大家分享了js實(shí)現(xiàn)頁(yè)面滾動(dòng)動(dòng)畫(huà)的具體代碼,供大家參考,具體內(nèi)容如下

需求:
1 頁(yè)面滾動(dòng)到對(duì)應(yīng)板塊,左側(cè)對(duì)應(yīng)的索引高亮
2 點(diǎn)擊左側(cè)的索引,滾動(dòng)到對(duì)應(yīng)的板塊
代碼如下,直接拷貝到html文件就可以使用
<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
? ? <title>Document</title>
? ? <style>?
? ? ? ? html, body, ul {
? ? ? ? ? ? margin: 0;
? ? ? ? ? ? padding: 0;
? ? ? ? }
? ? ? ? .section li {
? ? ? ? ? ? height: 400px;
? ? ? ? }
? ? ? ? li {
? ? ? ? ? ? list-style: none;
? ? ? ? }
? ? ? ? .indexs {
? ? ? ? ? ? width: 40px;
? ? ? ? ? ? background-color: #000000;
? ? ? ? ? ? color: #ffffff;
? ? ? ? ? ? position: fixed;
? ? ? ? ? ? left: 20px;
? ? ? ? ? ? bottom: 50px;
? ? ? ? ? ? display: none;
? ? ? ? }
? ? ? ? .indexs.active {
? ? ? ? ? ? display: block;
? ? ? ? }
? ? ? ? .indexs li {
? ? ? ? ? ? padding: 10px;
? ? ? ? ? ? text-align: center;
? ? ? ? ? ? user-select: none;
? ? ? ? }
? ? ? ? .indexs li.addcolor {
? ? ? ? ? ? background-color: red;
? ? ? ? }
? ? ? ? header {
? ? ? ? ? ? height: 1000px;
? ? ? ? ? ? background-color: purple;
? ? ? ? }
? ? ? ? footer {
? ? ? ? ? ? height: 200px;
? ? ? ? ? ? background-color: rgb(15, 218, 15);
? ? ? ? }
? ? </style>
</head>
<body>
? ? <header></header>
? ? <ul class="section">
? ? ? ? <li style="background-color: palegreen;">1</li>
? ? ? ? <li style="background-color: rgb(15, 218, 15);">2</li>
? ? ? ? <li style="background-color: rgb(24, 203, 209);">3</li>
? ? ? ? <li style="background-color: rgb(167, 152, 251);">4</li>
? ? ? ? <li style="background-color: rgb(251, 152, 165);">5</li>
? ? ? ? <li style="background-color: palegreen;">6</li>
? ? ? ? <li style="background-color: rgb(15, 218, 15);">7</li>
? ? ? ? <li style="background-color: rgb(24, 203, 209);">8</li>
? ? ? ? <li style="background-color: rgb(167, 152, 251);">9</li>
? ? ? ? <li style="background-color: rgb(251, 152, 165);">10</li>
? ? </ul>
? ? <ul class="indexs">
? ? ? ? <li>1</li>
? ? ? ? <li>2</li>
? ? ? ? <li>3</li>
? ? ? ? <li>4</li>
? ? ? ? <li>5</li>
? ? ? ? <li>6</li>
? ? ? ? <li>7</li>
? ? ? ? <li>8</li>
? ? ? ? <li>9</li>
? ? ? ? <li>10</li>
? ? </ul>
? ? <footer></footer>
? ? <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
? ? <script type="text/javascript">
? ? ? ? let scrollTop = 0
? ? ? ? let section = document.querySelector('.section')
? ? ? ? let sectionLi = document.querySelectorAll('.section li')
? ? ? ? let indexs = document.querySelector('.indexs')
? ? ? ? let indexsLi = document.querySelectorAll('.indexs li')
? ? ? ? let ofseTop = 0
? ? ? ??
? ? ? ? let requestId = null
? ? ? ??
? ? ? ? let recordScrollTop = 0
? ? ? ? // 頁(yè)面滾動(dòng)方向
? ? ? ? let scrollDirection = null
? ? ? ? let diffX = 0
? ? ? ? let diffY = 0
? ? ? ? let scrollAction = {x: 'undefined', y: 'undefined'}
? ? ? ? let lastI = 0
? ? ? ? let direction = null
? ? ? ? window.onscroll = myScroll
? ? ? ??
? ? ? ? function myScroll(e) {
? ? ? ? ? ? scrollTop = document.body.scrollTop || document.documentElement.scrollTop
? ? ? ? ? ? // 當(dāng)header滾出去時(shí)出現(xiàn)左側(cè)導(dǎo)航
? ? ? ? ? ? if (section.offsetTop -scrollTop <= 0) {
? ? ? ? ? ? ? ? indexs.classList.add('active')
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? indexs.classList.remove('active')
? ? ? ? ? ? }
? ? ? ? ? ? // 滾到sectionLi對(duì)應(yīng)的li時(shí)候 給indexs對(duì)應(yīng)的li紅色高亮
? ? ? ? ? ? for (let i = 0; i < sectionLi.length; i++) {
? ? ? ? ? ? ? ? if(sectionLi[i].offsetTop - scrollTop <= 30) {?
? ? ? ? ? ? ? ? ? ? for (let j = 0; j < indexsLi.length; j++) {
? ? ? ? ? ? ? ? ? ? ? ? indexsLi[j].classList.remove('addcolor')
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? indexsLi[i].classList.add('addcolor')
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ??
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? // 點(diǎn)擊indexs對(duì)應(yīng)的li 頁(yè)面滾回到section對(duì)應(yīng)的li
? ? ? ? for (let i = 0; i < indexsLi.length; i++) {
? ? ? ? ? ? indexsLi[i].addEventListener('click', function (e) {
? ? ? ? ? ? ? ? for (let j = 0; j < indexsLi.length; j++) {
? ? ? ? ? ? ? ? ? ? indexsLi[j].classList.remove('active')
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? // i就是被點(diǎn)擊的那個(gè)li的索引值
? ? ? ? ? ? ? ? indexsLi[i].classList.add('active')
? ? ? ? ? ? ? ? // 記錄下被點(diǎn)擊的li的offsetTop
? ? ? ? ? ? ? ? ofseTop = sectionLi[i].offsetTop
? ? ? ? ? ? ? ? // 第一種方法 jquery 動(dòng)畫(huà) 這里不用jquery 用requestAnimationFrame
? ? ? ? ? ? ? ? // $('html,body').animate({?
? ? ? ? ? ? ? ? // ? ? scrollTop: ofseTop
? ? ? ? ? ? ? ? // }, 300)
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? // 第二種方法 requestAnimationFrame
? ? ? ? ? ? ? ? if(i - lastI > 0) {
? ? ? ? ? ? ? ? ? ? direction = 'down'
? ? ? ? ? ? ? ? } else if(i - lastI < 0){
? ? ? ? ? ? ? ? ? ? direction = 'up'
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? // 更新lastI
? ? ? ? ? ? ? ? lastI = i
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? // 記錄點(diǎn)擊li的時(shí)候 當(dāng)前頁(yè)面卷出去了多少
? ? ? ? ? ? ? ? recordScrollTop = document.body.scrollTop || document.documentElement.scrollTop
? ? ? ? ? ? ? ? requestId = requestAnimationFrame(scrollAnimate)
? ? ? ? ? ? })
? ? ? ? }
? ? ? ??
? ? ? ??
? ? ? ? function scrollAnimate(){
? ? ? ? ? ? // 滾動(dòng)條向下滾動(dòng) 頁(yè)面向上翻 當(dāng)前頁(yè)面卷出去更多
? ? ? ? ? ? if (direction && direction === 'down') {
? ? ? ? ? ? ? ? recordScrollTop += 30
? ? ? ? ? ? ? ? if (recordScrollTop >= ofseTop) {
? ? ? ? ? ? ? ? ? ? direction = null
? ? ? ? ? ? ? ? ? ? return cancelAnimationFrame(requestId)
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? // 滾動(dòng)條向上滾動(dòng) 頁(yè)面向下翻 當(dāng)前頁(yè)面卷出去更少
? ? ? ? ? ? if (direction && direction === 'up') {
? ? ? ? ? ? ? ? recordScrollTop -= 30
? ? ? ? ? ? ? ? if (recordScrollTop <= ofseTop) {
? ? ? ? ? ? ? ? ? ? direction = null
? ? ? ? ? ? ? ? ? ? return cancelAnimationFrame(requestId)
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? ? ? document.body.scrollTop = recordScrollTop
? ? ? ? ? ? document.documentElement.scrollTop = recordScrollTop
? ? ? ? ? ? requestId = requestAnimationFrame(scrollAnimate)
? ? ? ? ? ??
? ? ? ? }
? ? ? ? // 人為鼠標(biāo)滾動(dòng)判斷頁(yè)面滾動(dòng)方向 這個(gè)函數(shù)在這個(gè)案例用不到
? ? ? ? function scrollFunc() {
? ? ? ? ? ? if (typeof scrollAction.x == 'undefined') {
? ? ? ? ? ? ? ? scrollAction.x = window.pageXOffset
? ? ? ? ? ? ? ? scrollAction.y = window.pageYOffset
? ? ? ? ? ? }
? ? ? ? ? ? diffX = scrollAction.x - window.pageXOffset
? ? ? ? ? ? diffY = scrollAction.y - window.pageYOffset
? ? ? ? ? ? if (diffX < 0) {
? ? ? ? ? ? ? ? // Scroll right
? ? ? ? ? ? ? ? scrollDirection = 'right'
? ? ? ? ? ? } else if (diffX > 0) {
? ? ? ? ? ? ? ? // Scroll left
? ? ? ? ? ? ? ? scrollDirection = 'left'
? ? ? ? ? ? } else if (diffY < 0) {
? ? ? ? ? ? ? ? // Scroll down
? ? ? ? ? ? ? ? scrollDirection = 'down'
? ? ? ? ? ? } else if (diffY > 0) {
? ? ? ? ? ? ? ? // Scroll up
? ? ? ? ? ? ? ? scrollDirection = 'up'
? ? ? ? ? ? }?
? ? ? ? ? ? scrollAction.x = window.pageXOffset
? ? ? ? ? ? scrollAction.y = window.pageYOffset
? ? ? ? }
? ? </script>
</body>
</html>以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 十個(gè)利用JavaScript實(shí)現(xiàn)的愛(ài)心動(dòng)畫(huà)特效
- js實(shí)現(xiàn)帶翻轉(zhuǎn)動(dòng)畫(huà)圖片時(shí)鐘
- 關(guān)于JavaScript實(shí)現(xiàn)動(dòng)畫(huà)時(shí)動(dòng)畫(huà)抖動(dòng)的原因與解決方法
- JavaScript實(shí)現(xiàn)瀑布動(dòng)畫(huà)
- JS使用window.requestAnimationFrame()實(shí)現(xiàn)逐幀動(dòng)畫(huà)
- JavaScript實(shí)現(xiàn)扯網(wǎng)動(dòng)畫(huà)效果的示例代碼
- 如何利用JS實(shí)現(xiàn)時(shí)間軸動(dòng)畫(huà)效果
- JS實(shí)現(xiàn)添加緩動(dòng)畫(huà)的方法
- JavaScript+CSS實(shí)現(xiàn)唯美蝴蝶動(dòng)畫(huà)
相關(guān)文章
javascript foreach中如何獲取數(shù)組下標(biāo)/index
這篇文章主要介紹了javascript foreach中如何獲取數(shù)組下標(biāo)/index問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
JavaScript和CSS通過(guò)expression實(shí)現(xiàn)Table居中顯示
如何將表格居中的顯示使用一個(gè)叫expression的函數(shù),多數(shù)的瀏覽器都支持這個(gè)函數(shù),感興趣的朋友可以看一下具體的實(shí)現(xiàn)哈2013-06-06
ExtJs使用自定義插件動(dòng)態(tài)保存表頭配置(隱藏或顯示)
這篇文章主要介紹了ExtJs使用自定義插件動(dòng)態(tài)保存表頭配置(隱藏或顯示) ,需要的朋友可以參考下2018-09-09
使用js操作css實(shí)現(xiàn)js改變背景圖片示例
有個(gè)朋友在weibo上問(wèn)我可不可以用JS和CSS讓頁(yè)面每次刷新隨機(jī)產(chǎn)生一張背景圖,當(dāng)然是可以的。具體的方法看下面的實(shí)現(xiàn)代碼吧2014-03-03
JS實(shí)現(xiàn)的車(chē)標(biāo)圖片提示效果代碼
這篇文章主要介紹了JS實(shí)現(xiàn)的車(chē)標(biāo)圖片提示效果代碼,涉及JavaScript鼠標(biāo)事件觸發(fā)頁(yè)面元素遍歷修改的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Javascript寫(xiě)了一個(gè)清除“l(fā)ogo1_.exe”的殺毒工具(可掃描目錄)
Javascript寫(xiě)了一個(gè)清除“l(fā)ogo1_.exe”的殺毒工具(可掃描目錄)...2007-02-02
JavaScript可否多線程? 深入理解JavaScript定時(shí)機(jī)制
JavaScript的setTimeout與setInterval是兩個(gè)很容易欺騙別人感情的方法,因?yàn)槲覀冮_(kāi)始常常以為調(diào)用了就會(huì)按既定的方式執(zhí)行, 我想不少人都深有同感2012-05-05

