GSAP小應用——JS單頁滾屏

來源:互聯網
上載者:User

標籤:

好吧,第一次寫,比想像中的花時間呢,總算是寫好了。也好久沒寫部落格了,就發上來看看吧!

說明:就是一個很普通的單屏滾動效果,一次滾一屏那樣子。

PS:有了這個,再加上TimelineMax,那不就~~   <( ̄︶ ̄)>

PS2:用的是touch事件,只有在手機瀏覽器或像chrome手機模擬器之類的瀏覽器中才能看到效果!

<!DOCTYPE html><html><head><title>js單屏滾動</title><meta charset=‘utf-8‘/><meta name="viewport" content="width=device-width,target-densitydpi=high-dpi,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/><style type="text/css">html,body{height: 100%; margin: 0; padding: 0;}.page{height: 100%; font-size: 30em; text-align: center; background-color: rgb(68, 164, 168);}</style><!-- 引入GSAP --><script type="text/javascript" src=‘http://cdnjs.cloudflare.com/ajax/libs/gsap/latest/plugins/CSSPlugin.min.js‘></script>    <script type="text/javascript" src=‘http://cdnjs.cloudflare.com/ajax/libs/gsap/latest/easing/EasePack.min.js‘></script>    <script type="text/javascript" src=‘http://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenLite.min.js‘></script><script type="text/javascript">//定義初始屏var currentPage = 1;//定義初始觸點縱座標var old_clientY= 0;var new_clientY= 0;//定義螢幕高度var stage = document.body || document.documentElement; var stageHeight = stage.clientHeight;//定義螢幕個數var pageNum = 4;//禁止螢幕滾動window.addEventListener(‘touchmove‘,function(ev){ev.preventDefault();});window.addEventListener(‘touchstart‘,function(ev){old_clientY = ev.changedTouches[0][‘clientY‘];});window.addEventListener(‘touchend‘,function(ev){new_clientY = ev.changedTouches[0][‘clientY‘];//滾動超過30個像素就不滾動if(Math.abs(new_clientY - old_clientY) < 30) return;if(new_clientY > old_clientY){//住下拉向上滾currentPage--;if(currentPage < 1) currentPage = 1;}else{//住上拉向下滾currentPage++;if(currentPage > pageNum) currentPage = pageNum;}TweenLite.to(‘body‘, 0.8, {scrollTop: (currentPage-1) * stageHeight, ease: Back.easeInOut});});</script></head><body><div class=‘page‘>1</div><div class=‘page‘>2</div><div class=‘page‘>3</div><div class=‘page‘>4</div></body></html>

  

GSAP小應用——JS單頁滾屏

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.