JavaScript html 图片滑动切换效果,幻灯片式切换,新闻展示,滚动新闻

新闻展示,滚动新闻

程序说明

原理就是通过不断设置滑动对象的left(水平切换)和top(垂直切换)来实现图片切换的动态效果。

首先需要一个容器,程序会自动设置容器overflow为hidden,如果不是相对或绝对定位会同时设置position为relative,
滑动对象会设置为绝对定位:


Js代码   收藏代码
  1. var p = CurrentStyle(this._container).position;  
  2. p == "relative" || p == "absolute" || (this._container.style.position = "relative");  
  3. this._container.style.overflow = "hidden";  
  4. this._slider.style.position = "absolute";  


如果没有设置Change切换参数属性,会自动根据滑动对象获取:

Js代码   收藏代码
  1. this.Change = this.options.Change ? this.options.Change :  
  2.     this._slider[bVertical ? "offsetHeight" : "offsetWidth"] / this._count;  


执行Run方法就会开始进入切换,其中有一个可选参数用来重新设置要切换的图片索引:

Js代码   收藏代码
  1. index == undefined && (index = this.Index);  
  2. index < 0 && (index = this._count - 1) || index >= this._count && (index = 0);  



之后就到设置使用tween缓动时需要的参数了,
包括_target(目标值)、_t(时间)、_b(初始值)和_c(变化量):

Js代码   收藏代码
  1. this._target = -Math.abs(this.Change) * (this.Index = index);  
  2. this._t = 0;  
  3. this._b = parseInt(CurrentStyle(this._slider)[this.options.Vertical ? "top" : "left"]);  
  4. this._c = this._target - this._b;  
还有Duration(持续时间)是自定义属性。

参数设置好后就执行Move程序开始移动了。
里面很简单,首先判断_c是否有值(等于0表示不需要移动)和_t是否到达Duration,
未满足条件就继续移动,否则直接移动到目标值并进行下一次切换:

Js代码   收藏代码
  1. if (this._c && this._t < this.Duration) {  
  2.     this.MoveTo(Math.round(this.Tween(this._t++, this._b, this._c, this.Duration)));  
  3.     this._timer = setTimeout(Bind(thisthis.Move), this.Time);  
  4. }else{  
  5.     this.MoveTo(this._target);  
  6.     this.Auto && (this._timer = setTimeout(Bind(thisthis.Next), this.Pause));  
  7. }  



使用说明

实例化需要3个参数,分别是容器对象,滑动对象和切换数量,之后可以直接执行Run方法运行:

Js代码   收藏代码
  1. new SlideTrans("idContainer""idSlider", 3).Run();  



还有以下可选属性:
Vertical: true,//是否垂直方向(方向不能改)
Auto:  true,//是否自动
Change:  0,//改变量
Duration: 50,//滑动持续时间
Time:  10,//滑动延时
Pause:  2000,//停顿时间(Auto为true时有效)
onStart: function(){},//开始转换时执行
onFinish: function(){},//完成转换时执行
Tween:  Tween.Quart.easeOut//tween算子

其中Vertical初始化后就不能修改,Tween算子可参照这里的缓动效果选择(实例中选了其中3个)。

还有提供了以下方法:
Next: 切换下一个
Previous: 切换上一个
Stop: 停止自动切换
还有上面说到的Run


完整程序代码:

Js代码   收藏代码
  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 图片滑动切换效果</title>  
  6. <script type="text/javascript">  
  7. var $ = function (id) {  
  8.     return "string" == typeof id ? document.getElementById(id) : id;  
  9. };  
  10.   
  11. var Extend = function(destination, source) {  
  12.     for (var property in source) {  
  13.         destination[property] = source[property];  
  14.     }  
  15.     return destination;  
  16. }  
  17.   
  18. var CurrentStyle = function(element){  
  19.     return element.currentStyle || document.defaultView.getComputedStyle(element, null);  
  20. }  
  21.   
  22. var Bind = function(object, fun) {  
  23.     var args = Array.prototype.slice.call(arguments).slice(2);  
  24.     return function() {  
  25.         return fun.apply(object, args.concat(Array.prototype.slice.call(arguments)));  
  26.     }  
  27. }  
  28.   
  29. var Tween = {  
  30.     Quart: {  
  31.         easeOut: function(t,b,c,d){  
  32.             return -c * ((t=t/d-1)*t*t*t - 1) + b;  
  33.         }  
  34.     },  
  35.     Back: {  
  36.         easeOut: function(t,b,c,d,s){  
  37.             if (s == undefined) s = 1.70158;  
  38.             return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;  
  39.         }  
  40.     },  
  41.     Bounce: {  
  42.         easeOut: function(t,b,c,d){  
  43.             if ((t/=d) < (1/2.75)) {  
  44.                 return c*(7.5625*t*t) + b;  
  45.             } else if (t < (2/2.75)) {  
  46.                 return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;  
  47.             } else if (t < (2.5/2.75)) {  
  48.                 return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;  
  49.             } else {  
  50.                 return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;  
  51.             }  
  52.         }  
  53.     }  
  54. }  
  55.   
  56.   
  57. //容器对象,滑动对象,切换数量  
  58. var SlideTrans = function(container, slider, count, options) {  
  59.     this._slider = $(slider);  
  60.     this._container = $(container);//容器对象  
  61.     this._timer = null;//定时器  
  62.     this._count = Math.abs(count);//切换数量  
  63.     this._target = 0;//目标值  
  64.     this._t = this._b = this._c = 0;//tween参数  
  65.       
  66.     this.Index = 0;//当前索引  
  67.       
  68.     this.SetOptions(options);  
  69.       
  70.     this.Auto = !!this.options.Auto;  
  71.     this.Duration = Math.abs(this.options.Duration);  
  72.     this.Time = Math.abs(this.options.Time);  
  73.     this.Pause = Math.abs(this.options.Pause);  
  74.     this.Tween = this.options.Tween;  
  75.     this.onStart = this.options.onStart;  
  76.     this.onFinish = this.options.onFinish;  
  77.       
  78.     var bVertical = !!this.options.Vertical;  
  79.     this._css = bVertical ? "top" : "left";//方向  
  80.       
  81.     //样式设置  
  82.     var p = CurrentStyle(this._container).position;  
  83.     p == "relative" || p == "absolute" || (this._container.style.position = "relative");  
  84.     this._container.style.overflow = "hidden";  
  85.     this._slider.style.position = "absolute";  
  86.       
  87.     this.Change = this.options.Change ? this.options.Change :  
  88.         this._slider[bVertical ? "offsetHeight" : "offsetWidth"] / this._count;  
  89. };  
  90. SlideTrans.prototype = {  
  91.   //设置默认属性  
  92.   SetOptions: function(options) {  
  93.     this.options = {//默认值  
  94.         Vertical:   true,//是否垂直方向(方向不能改)  
  95.         Auto:       true,//是否自动  
  96.         Change:     0,//改变量  
  97.         Duration:   50,//滑动持续时间  
  98.         Time:       10,//滑动延时  
  99.         Pause:      2000,//停顿时间(Auto为true时有效)  
  100.         onStart:    function(){},//开始转换时执行  
  101.         onFinish:   function(){},//完成转换时执行  
  102.         Tween:      Tween.Quart.easeOut//tween算子  
  103.     };  
  104.     Extend(this.options, options || {});  
  105.   },  
  106.   //开始切换  
  107.   Run: function(index) {  
  108.     //修正index  
  109.     index == undefined && (index = this.Index);  
  110.     index < 0 && (index = this._count - 1) || index >= this._count && (index = 0);  
  111.     //设置参数  
  112.     this._target = -Math.abs(this.Change) * (this.Index = index);  
  113.     this._t = 0;  
  114.     this._b = parseInt(CurrentStyle(this._slider)[this.options.Vertical ? "top" : "left"]);  
  115.     this._c = this._target - this._b;  
  116.       
  117.     this.onStart();  
  118.     this.Move();  
  119.   },  
  120.   //移动  
  121.   Move: function() {  
  122.     clearTimeout(this._timer);  
  123.     //未到达目标继续移动否则进行下一次滑动  
  124.     if (this._c && this._t < this.Duration) {  
  125.         this.MoveTo(Math.round(this.Tween(this._t++, this._b, this._c, this.Duration)));  
  126.         this._timer = setTimeout(Bind(thisthis.Move), this.Time);  
  127.     }else{  
  128.         this.MoveTo(this._target);  
  129.         this.Auto && (this._timer = setTimeout(Bind(thisthis.Next), this.Pause));  
  130.     }  
  131.   },  
  132.   //移动到  
  133.   MoveTo: function(i) {  
  134.     this._slider.style[this._css] = i + "px";  
  135.   },  
  136.   //下一个  
  137.   Next: function() {  
  138.     this.Run(++this.Index);  
  139.   },  
  140.   //上一个  
  141.   Previous: function() {  
  142.     this.Run(--this.Index);  
  143.   },  
  144.   //停止  
  145.   Stop: function() {  
  146.     clearTimeout(this._timer); this.MoveTo(this._target);  
  147.   }  
  148. };  
  149. </script>  
  150. </head>  
  151. <body>  
  152. <style type="text/css">   
  153. .container, .container img{width:280px; height:200px;}  
  154. .container{border:1px solid #333;}  
  155. .container img{border:0;}  
  156. </style>  
  157. <div class="container" id="idContainer">  
  158.     <table id="idSlider" border="0" cellpadding="0" cellspacing="0">  
  159.         <tr>  
  160.             <td><a href="http://www.cnblogs.com/cloudgamer/archive/2009/01/06/Tween.html"><img src="http://images.cnblogs.com/cnblogs_com/cloudgamer/143727/r_rt_1.jpg"/></a></td>  
  161.         </tr>  
  162.         <tr>  
  163.             <td><a href="http://www.cnblogs.com/cloudgamer/archive/2008/07/21/ImgCropper.html"><img src="http://images.cnblogs.com/cnblogs_com/cloudgamer/143727/r_rt_5.jpg"/></a></td>  
  164.         </tr>  
  165.         <tr>  
  166.             <td><a href="http://www.cnblogs.com/cloudgamer/archive/2008/11/17/Drag.html"><img src="http://images.cnblogs.com/cnblogs_com/cloudgamer/143727/r_rt_3.jpg"/></a></td>  
  167.         </tr>  
  168.     </table>  
  169. </div>  
  170. <br />  
  171. <br />  
  172. <style type="text/css">  
  173. .num{ position:absolute; right:5px; bottom:5px;}  
  174. .num li{  
  175.     float: left;  
  176.     list-style:none;  
  177.     color: #fff;  
  178.     text-align: center;  
  179.     line-height: 16px;  
  180.     width: 16px;  
  181.     height: 16px;  
  182.     font-family: Arial;  
  183.     font-size: 12px;  
  184.     cursor: pointer;  
  185.     margin: 1px;  
  186.     border: 1px solid #707070;  
  187.     background-color: #060a0b;  
  188. }  
  189. .num li.on{  
  190.     line-height: 18px;  
  191.     width: 18px;  
  192.     height: 18px;  
  193.     font-size: 14px;  
  194.     border: 0;  
  195.     background-color: #ce0609;  
  196.     font-weight: bold;  
  197. }  
  198. </style>  
  199. <div class="container" id="idContainer2">  
  200.     <table id="idSlider2" border="0" cellpadding="0" cellspacing="0">  
  201.         <tr>  
  202.             <td><a href="http://www.cnblogs.com/cloudgamer/archive/2009/03/11/color.html"><img src="http://images.cnblogs.com/cnblogs_com/cloudgamer/143727/r_4.jpg"/></a></td>  
  203.             <td><a href="http://www.cnblogs.com/cloudgamer/archive/2008/12/24/Slider.html"><img src="http://images.cnblogs.com/cnblogs_com/cloudgamer/143727/r_3.jpg"/></a></td>  
  204.             <td><a href="http://www.cnblogs.com/cloudgamer/archive/2008/12/03/Resize.html"><img src="http://images.cnblogs.com/cnblogs_com/cloudgamer/143727/r_rt_2.jpg"/></a></td>  
  205.         </tr>  
  206.     </table>  
  207.     <ul class="num" id="idNum">  
  208.     </ul>  
  209. </div>  
  210. <br />  
  211. <div>  
  212.     <input id="idAuto" type="button" value="停止" />  
  213.     <input id="idPre" type="button" value="&lt;&lt;" />  
  214.     <input id="idNext" type="button" value="&gt;&gt;" />  
  215.     <select id="idTween">  
  216.         <option value="0">默认缓动</option>  
  217.         <option value="1">方式1</option>  
  218.         <option value="2">方式2</option>  
  219.     </select>  
  220. </div>  
  221. <script>  
  222.   
  223. new SlideTrans("idContainer""idSlider", 3).Run();  
  224.   
  225. ///////////////////////////////////////////////////////////  
  226.   
  227. var forEach = function(array, callback, thisObject){  
  228.     if(array.forEach){  
  229.         array.forEach(callback, thisObject);  
  230.     }else{  
  231.         for (var i = 0, len = array.length; i < len; i++) { callback.call(thisObject, array[i], i, array); }  
  232.     }  
  233. }  
  234.   
  235. var st = new SlideTrans("idContainer2""idSlider2", 3, { Vertical: false });  
  236.   
  237. var nums = [];  
  238. //插入数字  
  239. for(var i = 0, n = st._count - 1; i <= n;){  
  240.     (nums[i] = $("idNum").appendChild(document.createElement("li"))).innerHTML = ++i;  
  241. }  
  242.   
  243. forEach(nums, function(o, i){  
  244.     o.onmouseover = function(){ o.className = "on"; st.Auto = false; st.Run(i); }  
  245.     o.onmouseout = function(){ o.className = ""; st.Auto = true; st.Run(); }  
  246. })  
  247.   
  248. //设置按钮样式  
  249. st.onStart = function(){  
  250.     forEach(nums, function(o, i){ o.className = st.Index == i ? "on" : ""; })  
  251. }  
  252.   
  253. $("idAuto").onclick = function(){  
  254.     if(st.Auto){  
  255.         st.Auto = false; st.Stop(); this.value = "自动";  
  256.     }else{  
  257.         st.Auto = true; st.Run(); this.value = "停止";  
  258.     }  
  259. }  
  260. $("idNext").onclick = function(){ st.Next(); }  
  261. $("idPre").onclick = function(){ st.Previous(); }  
  262.   
  263. $("idTween").onchange = function(){  
  264.     switch (parseInt(this.value)){  
  265.         case 2 :  
  266.             st.Tween = Tween.Bounce.easeOut; break;  
  267.         case 1 :  
  268.             st.Tween = Tween.Back.easeOut; break;  
  269.         default :  
  270.             st.Tween = Tween.Quart.easeOut;  
  271.     }  
  272. }  
  273.   
  274.   
  275. st.Run();  
  276. </script>  
  277. </body>  
  278. </html> 

猜你喜欢

转载自blog.csdn.net/baolibin528/article/details/38684631