【问题标题】:using jquery numscroller in javascript在javascript中使用jquery numscroller
【发布时间】:2018-01-03 21:47:08
【问题描述】:

尝试动态使用numscroller,但不起作用。

静态使用(工作):

<span class='numscroller' data-min='1' data-max='1000' data-delay='5' data-increment='10'>1000</span>

动态使用(不起作用):

    $("#product-total-view").addClass('numscroller');
    $("#product-total-view").text(10000);
    $("#product-total-view").attr('data-delay',5);
    $("#product-total-view").attr('data-increment',10);
    $("#product-total-view").attr('data-min',1);
    $("#product-total-view").attr('data-max',10000);

有什么想法可以吗?

【问题讨论】:

  • “不起作用”到底是什么意思?你得到什么错误?它在做什么/不应该做什么?
  • 它不滚动数字,我只看到最终数字
  • 在列表末尾添加$(".numscroller").numscroller()
  • 我会尝试更新
  • 顺便说一句,$("#product-total-view").addClass('numscroller').text(10000).data({ delay: 5, increment: 10, min: 1, max: 10000}); 更简洁,速度也更快。

标签: javascript jquery html jquery-plugins


【解决方案1】:

在属性设置后添加numscroller类。

效果很好。
;)

// No CDN for "numscroller"... So look around line #94 (at the bottom) for OP code.

/**
* jQuery scroroller Plugin 1.0
*
* http://www.tinywall.net/
* 
* Developers: Arun David, Boobalan
* Copyright (c) 2014 
*/
(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);



$("#product-total-view").text(10000);
$("#product-total-view").attr('data-delay',5);
$("#product-total-view").attr('data-increment',10);
$("#product-total-view").attr('data-min',1);
$("#product-total-view").attr('data-max',10000);

// Add the class last!!!
$("#product-total-view").addClass('numscroller');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<span id="product-total-view"></span>

【讨论】:

  • 看起来不错,但仍然不适合我。我尝试添加到以下仪表板:github.com/puikinsh/gentelella,它也运行很多外部库。也许其中一个会覆盖它。我会尝试解决它并尽快更新
  • 看看#product-total-view是否也是动态生成的。在这种情况下,请使用$(document).find("#product-total-view)... ;) 我看不出哪里可以重叠...但是脚本运行时元素不存在的可能性很大。 ;)
  • 在设置属性之前最好的测试(我根本没有分析你的项目):if($("#product-total-view").length&gt;0){console.log("Element is there!")}else{console.log("Hey! I need an element to work!")} ;)
  • 元素就在那里!但我仍然看到没有动画的最终数字
  • 当我在your Github 的控制台中完全输入上面建议的控制台测试时...我得到Hey! I need an element to work! 那么...寻找错别字。 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
  • 2013-02-27
  • 1970-01-01
  • 2017-07-31
  • 2010-10-21
  • 2014-12-18
  • 2017-02-14
相关资源
最近更新 更多