Js、Jquery對goTop功能的實現

來源:互聯網
上載者:User

標籤:home   document   play   效果   注意   pos   head   data-   http   

本文介紹用原生JS和Jquery分別實現的網頁goTopbutton功能,以及在這個過程中碰到的問題。


終於實現的效果類似:http://sc2.163.com/home(注意右下角的top)


代碼:

Jquery

<!DOCTYPE html><html><head lang="en">    <meta charset="UTF-8">    <title></title>    <style>        body{height: 2000px;}        #goTop{            display: none;            cursor:pointer;            background: url("common.png") no-repeat scroll 0 -460px;            position: fixed;            width: 60px;            height: 60px;            right: 20px;            bottom: 20px;            text-indent: -9999px;            z-index: 100;        }        #goTop:hover{            background-position: -100px -460px;        }    </style>    <script src="../jQuery/jquery-1.11.3.js"></script>    <script>            $(function(){                $(window).scroll(function(){                    var scrH=document.documentElement.scrollTop+document.body.scrollTop;                    if(scrH>200){                        $(‘#goTop‘).fadeIn(400);                    }else{                        $(‘#goTop‘).stop().fadeOut(400);                    }                });                $(‘#goTop‘).click(function(){                    $(‘html,body‘).animate({scrollTop:‘0px‘},200);                });            });    </script></head><body><a id="goTop" href="javascript:">返回頂層</a></body></html>

JS
<!DOCTYPE html><html><head lang="en">    <meta charset="UTF-8">    <title></title>    <style>        body{height: 2000px;}        #goTop{            display: none;            cursor:pointer;            background:url("common.png") no-repeat scroll 0 -460px;            position: fixed;            width: 60px;            height: 60px;            right: 20px;            bottom: 20px;            z-index: 100;            text-indent: -9999px;        }        #goTop:hover{            background-position: -100px -460px;        }    </style></head><body><a id="goTop" title="返回頂部" href="javascript:scroll(0,0)">返回頂層</a></body></html><script>    (function() {        function $(id){            return document.getElementById(id);        }        window.onscroll=function(){            var scrH=document.documentElement.scrollTop+document.body.scrollTop;            if(scrH>200){                $(‘goTop‘).style.display=‘block‘;            }else{                $(‘goTop‘).style.display=‘none‘;            }        };    }());</script>

代碼好像沒啥特別好解釋了。說說我碰到的問題吧。

①在IE低版本號碼下。不支援rgba屬性~

②原生js的動畫效果還不會實現,希望有人留言教下。


Js、Jquery對goTop功能的實現

聯繫我們

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