jquery滾動到頂部


    <script>
        $.fn.scrollTo = function (options) {

            var defaults = {

                toT: 0, //滾動目標位置 

                durTime: 500, //過渡動畫時間 

                delay: 30, //定時器時間 

                callback: null //回調函數 

            };

            var opts = $.extend(defaults, options),

                timer = null,

                _this = this,

                curTop = _this.scrollTop(), //滾動條當前的位置 

                subTop = opts.toT - curTop, //滾動條目標位置和當前位置的差值 

                index = 0,

                dur = Math.round(opts.durTime / opts.delay),

                smoothScroll = function (t) {

                    index++;

                    var per = Math.round(subTop / dur);

                    if (index >= dur) {

                        _this.scrollTop(t);

                        window.clearInterval(timer);

                        if (opts.callback && typeof opts.callback == 'function') {

                            opts.callback();

                        }

                        return;

                    } else {

                        _this.scrollTop(curTop + index * per);

                    }

                };

            timer = window.setInterval(function () {

                smoothScroll(opts.toT);

            }, opts.delay);

            return _this;

        };


        $("body").scrollTo({ toT: 0 });
    </script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM