js实现消息滚动显示效果 - jarry - 我想去非洲
arguments对象的高级用法

js实现消息滚动显示效果

jarry posted @ 2009年8月12日 05:52 in javascript with tags javascript , 6414 阅读

最近的项目中需要实现一个滚动消息的功能,以前在网上也看到了些,但是都不太符合我的需求,于是就自己写了个,效果:

内存泄露的问题一直以来都不受到大家的重视,但是浏览器对代码的解析可能造成效率问题
2009-08-03 13:32:56
内存泄露的问题一直以来都不受到大家的重视,但是浏览器对代码的解析可能造成效率问题
2009-08-03 13:32:56
内存泄露的问题一直以来都不受到大家的重视,但是浏览器对代码的解析可能造成效率问题
2009-08-03 13:32:56
内存泄露的问题一直以来都不受到大家的重视,但是浏览器对代码的解析可能造成效率问题
2009-08-03 13:32:56
内存泄露的问题一直以来都不受到大家的重视,但是浏览器对代码的解析可能造成效率问题
2009-08-03 13:32:56
内存泄露的问题一直以来都不受到大家的重视,但是浏览器对代码的解析可能造成效率问题
2009-08-03 13:32:56
1 2 3 4 5 6

 

 

 

 

 

 

缓动类型:

下面来说说程序的实现思路,最初我设想的是通过改变div的left属性来实现位移的效果,但是在实际操作中发现有一些困难,首先是当消息条目一旦多起来,需要移动的元素就太多了,会造成性能问题,其次每个条目也可能大小不一致,这样还得再程序中修正大小。所以后来我换了一种思路,把滚动消息放在一个表格内,这样只需要移动这个表格就可以了,思路有了,结下了来实现就简单了,程序的构造器接受3个参数,container, num, options,分别是消息包装器,滚动条目,配置选项。

配置选项有一下一些参数

  1. var ScrollMsg = Class.create({
  2.     options : {
  3.                vertical : false,//是否垂直,需要在html代码中提供多行的表格
  4.                auto : true,//是否自动
  5.                duration : 1000,//滚动持续时间
  6.                fps : 100,//帧数
  7.                pauseTime : 1000,//停顿时间间隔
  8.                onStart : function(){},//滚动开始前执行
  9.                onFinish : function(){},//滚动结束后执行
  10.                fx :  function(t,b,c,d){//缓动算法
  11.                    return c*(t/=d)*t*t*t + b;
  12.                }
  13.     }
  14. });

 这些参数都是可选的,需要注意的是如果auto设置为了false,那么pauseTime将无效。

接下来是程序的主要函数,它接受一个可选的index参数用于指定移到哪一个位置,如果不指定则默认为当前的下一个位置

  1. moveto : function (index){
  2.              if(typeof index == "undefined" || index < 0){
  3.                  index = (this.currIndex < this.count-1 ? this.currIndex+1 : 0);
  4.              } else{
  5.                  index = (index >= this.count ? this.count-1 : index);
  6.              }
  7.              this.clear();
  8.              this.from = parseInt(this.containerStyle[this.pos]);
  9.              this.change = -index * this.distance - this.from;
  10.              this.currIndex = index;
  11.              this.ctime = new Date().getTime();
  12.              window.setTimeout(this.options.onStart.Bind(this), 10);
  13.              this.timer = window.setInterval((function (){
  14.                  this.step();
  15.              }).Bind(this), Math.round(1000/this.options.fps));
  16. }

移动开始的时候会首先校正index参数,然后清空当前的timer,防止当有多个interval的时候出现位移混乱,然后记录消息包装器的当前位置,这个数值是通过ScrollMs对象的containerStyle属性得到的,containerStyle属性指向消息包装器的ComputedStyle

  1. var currentStyle = function (element){
  2.     element = $(element);
  3.     return element.currentStyle || document.defaultView.getComputedStyle(element, null);
  4. };
  5. this.containerStyle = currentStyle(this.container);

这样当以后需要的时候就不需要重复取了,直接.属性名即可。

  1. this.change = -index * this.distance - this.from;

 是说移动对象相对于当前位置要改变的量,这个可以用当前index*平均位移量再减去当前位置得到。this.ctime = new Date().getTime();是记录移动的开始时间,有了这些基本参数就可以调用缓动函数了。

程序还有一个start和stop函数用于启动停止滚动。如果auto为false则调用start函数只能移动一次。

step函数是用来进行逐步移位的,它首先判断当前时间是否小于等于位移的终止时间,如果为true则位移一次,否则就清空timer,然后把移动对象的位置设置为最终值

  1. this.container.style[this.pos] = this.from + this.change;

这主要是考虑有可能上次位移没有达到最终值,这次位移的时间恰好又超出了终止时间从而造成位置偏差而进行的校正。

整个程序结构基本就是这样,通过修改还可以做成图片轮训展示的效果

源代码:

  1. Function.prototype.Bind = function (object){
  2.     var args = Array.prototype.slice.call(arguments, 1);
  3.         var fun = this;
  4.         return function() {
  5.             return fun.apply(object, args.concat(Array.prototype.slice.apply(arguments)));
  6.         }
  7. };
  8. Array.prototype.each = function(fun){
  9.     var b = true;
  10.     for (var i = 0, len = this.length; i < len; i++) { !!fun.call(this, this[i], i) ? b : (b = false); }
  11.     return b;
  12. };
  13. window.$ = function (id){
  14.     if(arguments.length > 1){
  15.          var els = [];
  16.          Array.prototype.each.call(arguments, function (a){els.push($(a));});
  17.          return els;
  18.     } else if("string" == typeof id){
  19.          return document.getElementById(id);
  20.     } else if(id.length && id.join){
  21.          return $.apply(null, id);
  22.     } else {
  23.          return id;
  24.     }
  25. };
  26. var Class = {
  27.     create: function (def) {
  28.         return function (){Extend(this, def||{}); def = null; this.initialize.apply(this, arguments); }
  29.     }
  30. };
  31. var Extend = function(destination, source) {
  32.     for (var property in source) { destination[property] = source[property]; }
  33.         return destination;
  34. };
  35. var currentStyle = function(element){
  36.     element = $(element);
  37.         return element.currentStyle || document.defaultView.getComputedStyle(element, null);
  38. };
  39. var ScrollMsg = Class.create({
  40.     options : {
  41.          vertical : false,
  42.          auto : true,
  43.          duration : 1000,
  44.          fps : 100,
  45.          pauseTime : 1000,
  46.          onStart : function(){},
  47.          onFinish : function(){},
  48.          fx : function (t,b,c,d){
  49.               return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
  50.          }
  51.     }
  52. });
  53. ScrollMsg.prototype = {
  54.     initialize : function (container, num, options){
  55.          this.setOptions(options);
  56.          this.container = $(container);
  57.          this.containerStyle = currentStyle(this.container);
  58.          this.count = num;
  59.          this.distance = (this.options.vertical ? this.container.scrollHeight : this.container.scrollWidth) / num;
  60.          this.currIndex = 0;
  61.          this.timer = null;
  62.          this.pauseTimer = null;
  63.          this.pos = this.options.vertical ? "top" : "left";
  64.          if(this.options.auto){
  65.               this.start();
  66.          }
  67.     },
  68.     setOptions : function (options){
  69.          Extend(this.options, options||{});
  70.     },
  71.     moveto : function (index){
  72.          if(typeof index == "undefined" || index < 0){
  73.                index = (this.currIndex < this.count-1 ? this.currIndex+1 : 0);
  74.          } else{
  75.                index = (index >= this.count ? this.count-1 : index);
  76.          }
  77.          this.clear();
  78.          this.from = parseInt(this.containerStyle[this.pos]);
  79.          this.change = -index * this.distance - this.from;
  80.          this.currIndex = index;
  81.          this.ctime = new Date().getTime();
  82.          window.setTimeout(this.options.onStart.Bind(this), 10);
  83.          this.timer = window.setInterval((function (){
  84.               this.step();
  85.          }).Bind(this), Math.round(1000/this.options.fps));
  86.     },
  87.     step : function (){
  88.          var time = new Date().getTime();
  89.               if(time <= this.ctime + this.options.duration){
  90.                    var t = time - this.ctime;
  91.                    this.container.style[this.pos] = this.compute(t);
  92.               }else{
  93.                    this.clear();
  94.                    this.container.style[this.pos] = this.from + this.change;
  95.                    window.setTimeout(this.options.onFinish.Bind(this), 10);
  96.                     if(this.pauseTimer && this.options.auto)this.start();
  97.               }
  98.     },
  99.     clear : function (){
  100.          window.clearInterval(this.timer);
  101.     },
  102.     compute : function (t){
  103.          return Math.round(this.options.fx(t, this.from, this.change, this.options.duration)) + "px";
  104.     },
  105.     stop : function (){
  106.          window.clearTimeout(this.pauseTimer);
  107.          this.pauseTimer = null;
  108.     },
  109.     start : function(){
  110.          window.clearTimeout(this.pauseTimer);
  111.          this.pauseTimer = window.setTimeout((function (){
  112.               this.moveto();
  113.          }).Bind(this), this.options.pauseTime);
  114.     }
  115. };
(程序可自由转载,但是请注明出处)

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter