js scrollTop到達指定位置!

來源:互聯網
上載者:User

標籤:返回頂部   style   blog   color   io   java   ar   art   div   

很早之前就想分享這篇心得, 幸之今天能在這裡完成, 好了, 話不多說, 進入正題 :

方法主要利用scrolltop值做運動, 用於到達使用者指定的位置(如返回頂部把參數target設定為0即可),處理了多種情況如 scrolltop > 目標值 向上運動 ,等4種情況  , 代碼及用法貼上, 

goTo = function(target){var scrollT = document.body.scrollTop|| document.documentElement.scrollTopif (scrollT >target) {var timer = setInterval(function(){var scrollT = document.body.scrollTop|| document.documentElement.scrollTopvar step = Math.floor(-scrollT/6);document.documentElement.scrollTop = document.body.scrollTop = step + scrollT;if(scrollT <= target){document.body.scrollTop = document.documentElement.scrollTop = target;clearTimeout(timer);}},20)}else if(scrollT == 0){var timer = setInterval(function(){var scrollT = document.body.scrollTop|| document.documentElement.scrollTopvar step = Math.floor(300/3*0.7);document.documentElement.scrollTop = document.body.scrollTop = step + scrollT;console.log(scrollT)if(scrollT >= target){document.body.scrollTop = document.documentElement.scrollTop = target;clearTimeout(timer);}},20)}else if(scrollT < target){var timer = setInterval(function(){var scrollT = document.body.scrollTop|| document.documentElement.scrollTopvar step = Math.floor(scrollT/6);document.documentElement.scrollTop = document.body.scrollTop = step + scrollT;if(scrollT >= target){document.body.scrollTop = document.documentElement.scrollTop = target;clearTimeout(timer);}},20)}else if(target == scrollT){return false;}}

  用法 function(target) / / 目前唯一target(目標距離number)  ,

on(goPs,‘click‘,function(){ goTo(2450) }); //運動到scrolltop值為2450地位置,下面也一樣, 運動到指定的位置 on(goJob,‘click‘,function(){ goTo(3373) })on(goTel,‘click‘,function(){ buffer.goTo(3373) })on(goMe,‘click‘,function(){ buffer.goTo(1539) })on(goHome,‘click‘,function(){ buffer.goTo(0) });on(scrollgoOne,‘click‘,function(){ buffer.goTo(2450) });on(scrollgoPc,‘click‘,function(){ buffer.goTo(2450) });on(scrollJob,‘click‘,function(){ buffer.goTo(3373) });on(scrollMe,‘click‘,function(){ buffer.goTo(1539) });on(goTop,‘click‘,function(){ buffer.goTo(0) })

 

js scrollTop到達指定位置!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.