获取JavaScript-未捕获的TypeError:问题

时间:2018-12-03 11:32:53

标签: javascript jquery

我将numscroller.js用于计数器,但遇到两个JavaScript错误:

  

未捕获的TypeError:无法读取HTMLHeadingElement上未定义的属性“ top”。 -40号线。

有人可以用下面的代码帮助我吗?它显示错误在第40行。任何帮助将不胜感激。预先谢谢你。

工作网址-http://bluecrystaluae.com/counter/counter.html

Screenshot Attached

jQuery.noConflict();

(function($) {
  $(window).on("load", function() {
    $(document).scrollzipInit();
    $(document).rollerInit();
  });

  $(window).on("load scroll resize", function() {
    $('.numscroller').scrollzip({
      showFunction: function() {
        numberRoller($(this).attr('data-slno'));
      },
      wholeVisible: false,
    });
  });

  $.fn.scrollzipInit = function() {
    $('body').prepend("<div style='position:fixed;top:0px;left:0px;width:0;height:0;' id='scrollzipPoint'></div>");
  };

  $.fn.rollerInit = function() {
    var i = 0;
    $('.numscroller').each(function() {
      i++;
      $(this).attr('data-slno', i);
      $(this).addClass("roller-title-number-" + i);
    });
  };

  $.fn.scrollzip = function(options) {
    var settings = $.extend({
      showFunction: null,
      hideFunction: null,
      showShift: 0,
      wholeVisible: false,
      hideShift: 0,
    }, options);
    return this.each(function(i, obj) {
      $(this).addClass('scrollzip');
      if ($.isFunction(settings.showFunction)) {
        if (!$(this).hasClass('isShown') &&
          ($(window).outerHeight() + $('#scrollzipPoint').offset().top - settings.showShift) > ($(this).offset().top + ((settings.wholeVisible) ? $(this).outerHeight() : 0)) &&
          ($('#scrollzipPoint').offset().top + ((settings.wholeVisible) ? $(this).outerHeight() : 0)) < ($(this).outerHeight() + $(this).offset().top - settings.showShift)
        ) {
          $(this).addClass('isShown');
          settings.showFunction.call(this);
        }
      }

      if ($.isFunction(settings.hideFunction)) {
        if (
          $(this).hasClass('isShown') &&
          (($(window).outerHeight() + $('#scrollzipPoint').offset().top - settings.hideShift) < ($(this).offset().top + ((settings.wholeVisible) ? $(this).outerHeight() : 0)) ||
            ($('#scrollzipPoint').offset().top + ((settings.wholeVisible) ? $(this).outerHeight() : 0)) > ($(this).outerHeight() + $(this).offset().top - settings.hideShift))
        ) {
          $(this).removeClass('isShown');
          settings.hideFunction.call(this);
        }
      }
      return this;
    });
  };

  function numberRoller(slno) {
    var min = $('.roller-title-number-' + slno).attr('data-min');
    var max = $('.roller-title-number-' + slno).attr('data-max');
    var timediff = $('.roller-title-number-' + slno).attr('data-delay');
    var increment = $('.roller-title-number-' + slno).attr('data-increment');
    var numdiff = max - min;
    var timeout = (timediff * 1000) / numdiff;
    //if(numinc<10){
    //increment=Math.floor((timediff*1000)/10);
    //}//alert(increment);
    numberRoll(slno, min, max, increment, timeout);
  }

  function numberRoll(slno, min, max, increment, timeout) { //alert(slno+"="+min+"="+max+"="+increment+"="+timeout);
    if (min <= max) {
      $('.roller-title-number-' + slno).html(min);
      min = parseInt(min) + parseInt(increment);
      setTimeout(function() {
        numberRoll(eval(slno), eval(min), eval(max), eval(increment), eval(timeout))
      }, timeout);
    } else {
      $('.roller-title-number-' + slno).html(max);
    }
  }
})(jQuery);

1 个答案:

答案 0 :(得分:0)

我认为您忘记了在插件函数中返回jquery元素

$.fn.scrollzipInit = function() {
    return $('body').prepend("<div style='position:fixed;top:0px;left:0px;width:0;height:0;' id='scrollzipPoint'></div>");
};

$.fn.rollerInit = function() {
    var i = 0;
    return $('.numscroller').each(function() {
      i++;
      $(this).attr('data-slno', i);
      $(this).addClass("roller-title-number-" + i);
    });
 };