Jquery函数没有显示,怎么来了



我正试图让这个Jquery count up函数工作,但它没有显示出来,有人能告诉我我做错了什么吗?

我正在使用"mhuggins"创建的函数(https://github.com/mhuggins/jquery-countTo)所以我自己没有成功,只是想让它发挥作用。

countup.js

    (function($) {
    $.fn.countTo = function(options) {
        // merge the default plugin settings with the custom options
        options = $.extend({}, $.fn.countTo.defaults, options || {});
        // how many times to update the value, and how much to increment the value on each update
        var loops = Math.ceil(options.speed / options.refreshInterval),
            increment = (options.to - options.from) / loops;
        return $(this).each(function() {
            var _this = this,
                loopCount = 0,
                value = options.from,
                interval = setInterval(updateTimer, options.refreshInterval);
            function updateTimer() {
                value += increment;
                loopCount++;
                $(_this).html(value.toFixed(options.decimals));
                if (typeof(options.onUpdate) == 'function') {
                    options.onUpdate.call(_this, value);
                }
                if (loopCount >= loops) {
                    clearInterval(interval);
                    value = options.to;
                    if (typeof(options.onComplete) == 'function') {
                        options.onComplete.call(_this, value);
                    }
                }
            }
        });
    };
    $.fn.countTo.defaults = {
        from: 0,  // the number the element should start at
        to: 100,  // the number the element should end at
        speed: 1000,  // how long it should take to count between the target numbers
        refreshInterval: 100,  // how often the element should be updated
        decimals: 0,  // the number of decimal places to show
        onUpdate: null,  // callback method for every time the element is updated,
        onComplete: null,  // callback method for when the element finishes updating
    };
})(jQuery);

然后我尝试将它与这个html页面一起使用

test.html

<html>
<head>
<script type="text/javascript" src="scripts/jquery-1.6.2.js"></script>
<script type="text/javascript" src="scripts/countup.js"></script>
<script type="text/javascript"><!--
    $('.timer').countTo({from: 0, to: 500});
//--></script>
</head>
<body>
<span class="timer"></span>
</body>
</html>

当我打开页面时,什么也没发生。这个剧本是为其他人写的,所以很明显是我做错了什么。我对此很陌生,所以这并不奇怪。

谢谢!

看起来您缺少document.ready(function() { // timer code here });

执行脚本时没有<span class="timer">。将其添加为DOMready处理程序:
<script type="text/javascript"><!--
    jQuery(function($) {
        $('.timer').countTo({from: 0, to: 500});
    });
//--></script>
</head>

或者在正文末尾执行:

<body>
    <span class="timer"></span>
    <script type="text/javascript"><!--
        $('.timer').countTo({from: 0, to: 500});
    //--></script>
</body>
$(document).ready(function(){
    $('.timer').countTo({from: 0, to: 500});
});

相关内容

最新更新