文字或背景颜色渐变的效果。

 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>JavaScript 无缝上下左右滚动加定高定宽停顿效果(兼容ie/ff)</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. var $ = function (id) {
  10.     return "string" == typeof id ? document.getElementById(id) : id;
  11. };
  12. var Class = {
  13.   create: function() {
  14.     return function() {
  15.       this.initialize.apply(this, arguments);
  16.     }
  17.   }
  18. }
  19. Object.extend = function(destination, source) {
  20.     for (var property in source) {
  21.         destination[property] = source[property];
  22.     }
  23.     return destination;
  24. }
  25. function addEventHandler(oTarget, sEventType, fnHandler) {
  26.     if (oTarget.addEventListener) {
  27.         oTarget.addEventListener(sEventType, fnHandler, false);
  28.     } else if (oTarget.attachEvent) {
  29.         oTarget.attachEvent("on" + sEventType, fnHandler);
  30.     } else {
  31.         oTarget["on" + sEventType] = fnHandler;
  32.     }
  33. };
  34. var Scroller = Class.create();
  35. Scroller.prototype = {
  36.   initialize: function(idScroller, idScrollMid, options) {
  37.     var oThis = thisoScroller = $(idScroller), oScrollMid = $(idScrollMid);
  38.     
  39.     this.SetOptions(options);
  40.     thisthis.Side = this.options.Side || ["up"];//方向
  41.     this.scroller = oScroller;            //对象
  42.     thisthis.speed = this.options.Speed;    //速度
  43.     this.timer = null;                    //时间
  44.     this.pauseHeight = 0;                //定高
  45.     this.pauseWidth = 0;                //定宽
  46.     this.pause = 0;                        //定高(宽)
  47.     this.side = 0;                        //参数
  48.     
  49.     //用于上下滚动
  50.     this.heightScroller = parseInt(oScroller.style.height) || oScroller.offsetHeight;
  51.     this.heightList = oScrollMid.offsetHeight;
  52.     
  53.     //用于左右滚动
  54.     this.widthScroller = parseInt(oScroller.style.width) || oScroller.offsetWidth;
  55.     this.widthList = oScrollMid.offsetWidth;
  56.     
  57.     //js取不到css设置的height和width
  58.     
  59.     oScroller.style.overflow = "hidden";
  60.     oScrollMid.appendChild(oScrollMid.cloneNode(true));
  61.     oScrollMid.appendChild(oScrollMid.cloneNode(true));
  62.     
  63.     addEventHandler(oScroller, "mouseover", function() { oThis.Stop(); });
  64.     addEventHandler(oScroller, "mouseout", function() { oThis.Start(); });
  65.     
  66.     this.Start();
  67.   },
  68.   //设置默认属性
  69.   SetOptions: function(options) {
  70.     this.options = {//默认值
  71.       Step:            1,//每次变化的px量
  72.       Speed:        20,//速度(越大越慢)
  73.       Side:            ["up"],//滚动方向:"up"是上,"down"是下,"left"是左,"right"是右
  74.       PauseHeight:    0,//隔多高停一次
  75.       PauseWidth:    0,//隔多宽停一次
  76.       //当上下和左右一起使用时必须设置PauseHeight和PauseWidth来设置转向位置
  77.       PauseStep:    1000//停顿时间(PauseHeight或PauseWidth大于0该参数才有效)
  78.     };
  79.     Object.extend(this.options, options || {});
  80.   }, 
  81.   //转向
  82.   Turn: function() {
  83.     //通过设置方向数组的排列来转向
  84.     this.Side.push(this.Side.shift().toLowerCase());
  85.   },
  86.   //上下滚动
  87.   ScrollUpDown: function() {
  88.     thisthis.pause = this.pauseHeight;
  89.     thisthis.scroller.scrollTop = this.GetScroll(this.scroller.scrollTop, this.heightScroller, this.heightList, this.options.PauseHeight);
  90.     thisthis.pauseHeight = this.pause;
  91.     
  92.     var oThis = this;
  93.     this.timer = window.setTimeout(function(){ oThis.Start(); }, this.speed);
  94.   },
  95.   //左右滚动
  96.   ScrollLeftRight: function() {
  97.     thisthis.pause = this.pauseWidth;
  98.     //注意:scrollLeft超过1400会自动变回1400 注意长度
  99.     thisthis.scroller.scrollLeft = this.GetScroll(this.scroller.scrollLeft, this.widthScroller, this.widthList, this.options.PauseWidth);
  100.     thisthis.pauseWidth = this.pause;
  101.     
  102.     var oThis = this;
  103.     this.timer = window.setTimeout(function(){ oThis.Start(); }, this.speed);
  104.   },
  105.   //获取设置滚动数据
  106.   GetScroll: function(iScroll, iScroller, iList, iPause) {
  107.     var iStep = this.options.Step * this.side;
  108.     
  109.     if(this.side > 0){
  110.         if(iScroll >= (iList * 2 - iScroller)){ iScroll -iList; }
  111.     } else {
  112.         if(iScroll <= 0){ iScroll += iList; }
  113.     }
  114.     
  115.     thisthis.speed = this.options.Speed;
  116.     if(iPause > 0){
  117.         if(Math.abs(this.pause) >= iPause){
  118.             thisthis.speed = this.options.PauseStep; this.pause = iStep = 0; this.Turn();
  119.         } else {
  120.             this.pause += iStep;
  121.         }
  122.     }
  123.     
  124.     return (iScroll + iStep);
  125.   },
  126.   //开始
  127.   Start: function() {    
  128.     //方向设置
  129.     switch (this.Side[0].toLowerCase()) {
  130.         case "right" :
  131.             if(this.widthList < this.widthScroller) return;
  132.             this.side = -1;
  133.             this.ScrollLeftRight();
  134.             break;
  135.         case "left" :
  136.             if(this.widthList < this.widthScroller) return;
  137.             this.side = 1;
  138.             this.ScrollLeftRight();
  139.             break;
  140.         case "down" :
  141.             if(this.heightList < this.heightScroller) return;
  142.             this.side = -1;
  143.             this.ScrollUpDown();
  144.             break;
  145.         case "up" :
  146.         default :
  147.             if(this.heightList < this.heightScroller) return;
  148.             this.side = 1;
  149.             this.ScrollUpDown();
  150.     }
  151.   },
  152.   //停止
  153.   Stop: function() {
  154.     clearTimeout(this.timer);
  155.   }
  156. };
  157. window.onload = function(){
  158.     new Scroller("idScroller", "idScrollMid",{ Side:["up","left"], PauseHeight:50, PauseWidth:400 });
  159. }
  160. </script>
  161. <style>
  162. .Scroller {}{line-height:50px; border:1px solid #000000; padding:0px 10px; height:50px; width:400px;}
  163. .Scroller *{}{margin:0px; padding:0px;}
  164. .ScrollMid {}{float:left;}
  165. .ScrollMid ul{}{width:800px;float:left;}
  166. .ScrollMid li{}{list-style:none; float:left; width:390px; padding-left:10px;}
  167. </style>
  168. <div id="idScroller" class="Scroller" style="width:400px; height:50px;">
  169.   <div style="width:1600px">
  170.     <div id="idScrollMid" class="ScrollMid">
  171.       <ul>
  172.         <li>111111111111</li>
  173.         <li>2222222222222</li>
  174.         <li>333333333333333</li>
  175.         <li>4444444444444</li>
  176.       </ul>
  177.     </div>
  178.   </div>
  179. </div>
  180. <div id="test"></div>
  181. </body>
  182. </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值