【问题标题】:Add year counter to flipclock.js将年份计数器添加到 Flipclock.js
【发布时间】:2016-03-25 22:58:19
【问题描述】:

我想为翻转时钟添加一个年份计数器,但“天”似乎是开箱即用支持的最大单位。

如何修改以添加年份作为显示计数器之一?

【问题讨论】:

  • 查看源代码。你可以像dailycounter之类的一年写一张脸:github.com/objectivehtml/FlipClock/blob/master/src/flipclock/js/…
  • 谢谢。我查看了源代码,但作为一个不熟悉 JavaScript 的人,我不确定如何修改代码。我希望有人可以提供一个代码示例,让我朝着正确的方向开始。

标签: javascript jquery flipclock


【解决方案1】:

对于任何感兴趣的人,我在flipclock.js 中添加了以下内容:

/**
 * Gets number of years
 */     

getYears: function() {
    return Math.floor(this.time / 60 / 60 / 24 / 7 / 52);
},

还有这个:

        /**
     * Gets a digitized yearly counter
     *
     * @return  object  Returns a digitized object
     */

    getYearCounter: function(includeSeconds) {
        var digits = [
            this.getYears(),
            this.getDays(true),
            this.getHours(true),
            this.getMinutes(true)
        ];

        if(includeSeconds) {
            digits.push(this.getSeconds(true));
        }

        return this.digitize(digits);
    },

还有这个:

    (function($) {

    /**
     * Yearly Counter Clock Face
     *
     * This class will generate a yearly counter for FlipClock.js. A
     * yearly counter will track years, days, hours, minutes, and seconds. If
     * the number of available digits is exceeded in the count, a new
     * digit will be created.
     *
     * @param  object  The parent FlipClock.Factory object
     * @param  object  An object of properties to override the default
     */

    FlipClock.YearlyCounterFace = FlipClock.Face.extend({

        showSeconds: true,

        /**
         * Constructor
         *
         * @param  object  The parent FlipClock.Factory object
         * @param  object  An object of properties to override the default
         */

        constructor: function(factory, options) {
            this.base(factory, options);
        },

        /**
         * Build the clock face
         */

        build: function(time) {
            var t = this;
            var children = this.factory.$el.find('ul');
            var offset = 0;

            time = time ? time : this.factory.time.getYearCounter(this.showSeconds);

            if(time.length > children.length) {
                $.each(time, function(i, digit) {
                    t.createList(digit);
                });
            }

            if(this.showSeconds) {
                $(this.createDivider('Seconds')).insertBefore(this.lists[this.lists.length - 2].$el);
            }
            else
            {
                offset = 2;
            }

            $(this.createDivider('Minutes')).insertBefore(this.lists[this.lists.length - 4 + offset].$el);
            $(this.createDivider('Hours')).insertBefore(this.lists[this.lists.length - 6 + offset].$el);
            $(this.createDivider('Days')).insertBefore(this.lists[this.lists.length - 8 + offset].$el);
            $(this.createDivider('Years', true)).insertBefore(this.lists[0].$el);

            this.base();
        },

        /**
         * Flip the clock face
         */

        flip: function(time, doNotAddPlayClass) {
            if(!time) {
                time = this.factory.time.getYearCounter(this.showSeconds);
            }

            this.autoIncrement();

            this.base(time, doNotAddPlayClass);
        }

    });

}(jQuery));

然后输出:

<div class="clock" style="margin:2em;"></div>

        <script type="text/javascript">
            var clock;

            $(document).ready(function() {

                // Grab the current date
                var currentDate = new Date();

                // Set some date in the future. In this case, it's always Jan 1
                var futureDate  = new Date(currentDate.getFullYear() + 22, 0, 1);

                // Calculate the difference in seconds between the future and current date
                var diff = futureDate.getTime() / 1000 - currentDate.getTime() / 1000;

                // Instantiate a coutdown FlipClock
                clock = $('.clock').FlipClock(diff, {
                    clockFace: 'YearlyCounter',
                    countdown: true
                });
            });
        </script>   

【讨论】:

    猜你喜欢
    • 2022-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 2020-11-11
    • 2012-04-02
    • 2017-09-03
    • 1970-01-01
    相关资源
    最近更新 更多