javascript實現 捲軸滾動 載入內容

來源:互聯網
上載者:User

說明:自從Bing搜尋實現  滾動式載入內容後,google,baidu都實現了類似的功能。

步驟 1:  開啟Visual Studio 2010  ,建立項目www.scrollpage.com.

步驟 2:添加一個HTML檔案,命名:ScrollLoadPage.html,項目引用了Jquery1.8.2架構

步驟 3:添加代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title>demo</title>    <script src="jquery-1.8.2.min.js" type="text/javascript"></script>    <style type="text/css">        div#Loadding { text-align: center; margin-top: 10px; display: none; font-weight: bold; color:Red; }        div.content { width: 100%; height: 900px; border-bottom: 1px solid gray;  font-weight: bold; color:Red;text-align: center;}    </style>    <script type="text/javascript">        if (!NeuF) var NeuF = {};        NeuF.ScrollPage = function (obj, options, callback) {            var _defaultOptions = { delay: 500, marginBottom: 100 }; //預設配置:延遲時間delay和捲軸距離底部距離marginBottom            options = $.extend(_defaultOptions, options);            this.isScrolling = false; //是否在滾動            this.oriPos = 0; //原始位置            this.curPos = 0; //當前位置            var me = this; //頂層            var $obj = (typeof obj == "string") ? $("#" + obj) : $(obj);            //綁定滾動事件            $obj.scroll(function (ev) {                me.curPos = $obj.scrollTop();                if ($(window).height() + $(window).scrollTop() >= $(document.body).height() - options.marginBottom) {                    if (me.isScrolling == true) return;                    me.isScrolling = true;                    setTimeout(function () { me.isScrolling = false; }, options.delay);   //重複觸發間隔毫秒                    if (typeof callback == "function") callback.call(null, me.curPos - me.oriPos);                };                me.oriPos = me.curPos;            });        };        $(function () {            window.scrollTo(0, 0); //每次F5重新整理把捲軸置頂            //marginBottom表示捲軸離底部的距離,0表示滾動到最底部才載入,可以根據需要修改              new NeuF.ScrollPage(window, { delay: 1000, marginBottom: 0 }, function (offset) {                if (offset > 0) {                    $("#Loadding").show(); //載入提示                    setTimeout(function () {                        //這裡就是非同步擷取內容的地方,這裡簡化成一句話,可以根據需要修改                        $("#divContainer").append($("<div class='content'>第“" + ($(".content").size() + 1) + "”頁內容</div>"));                        //內容擷取後,隱藏載入提示                        $("#Loadding").hide();                    }, 1000);                }            });        });    </script></head><body>    <div id="divContainer">        <div class="content">            這裡是內容,嘗試滾動,載入下一頁內容。如果是大螢幕顯示器,<br />請把CSS div.content 高度調高,以便看到滾動效果 </div>    </div>    <div id="Loadding">        正在載入,請稍候 ...</div></body></html>

步驟 4: 在瀏覽器中查看ScrollLoadPage.html。

下面給出了項目源碼   :

http://files.cnblogs.com/HCCZX/www.scrollpage.com.rar

 

 

 

相關文章

聯繫我們

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