新闻展示,滚动新闻

程序说明

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

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

  1. Android仿今日头条图片滑动退出效果

    资源下载(2C币) 逛CSDN的时候,看到几篇写仿今日头条图片滑动退出效果的文章,闲着无聊便想着也给自己项目加上,实现的思路有很多种,本着就近原则选了一篇与自己思路相近的文章结合自己的实践总结一下. ...

  2. flash特效原理:图片滑动放大效果(2)

    flash特效原理:图片滑动放大效果(1) http://blog.csdn.net/hero82748274/archive/2009/10/22/4715312.aspx 最近看了一些关于动态注册 ...

  3. flash特效原理 图片滑动放大效果 2

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! flas ...

  4. Javascript实现图片轮播效果。

    用js做一个简单的图片轮播效果. 思路如下:用JavaScript来控制轮播的图片的样式(margin-left).用计时器来控制图片的自动播放.鼠标点击控制图片的翻页. 效果图如下.具有以下功能:1 ...

  5. html5滚动文字切换效果代码,js+div实现文字滚动和图片切换效果代码

    本文实例讲述了js+div实现文字滚动和图片切换效果代码.分享给大家供大家参考.具体如下: 这里演示js+div文字滚动和图片切换代码,为了演示方便,去掉了图片调用,用数字代替了,用时候再加上就可以了 ...

  6. 【CSON原创】 图片滑动展开效果发布

    功能说明: 鼠标移动到不同图片上,该图片滑动展开,其它图片折叠. 支持IE 6 7 8 firefox chrome 效果预览:   实现原理: 当鼠标移动到某张图片,该图片以及该图片前的图片以相同速 ...

  7. 公众号滑动图代码_公众号怎么制作图片滑动的效果?怎么做可以上下滑动的长图?...

    微信公众号图片怎么制作呢?图片太多又该怎么排版呢?接下来就和小编一起看看,怎么使用壹伴助手这款公众号编辑器,来实现公众号图片的排版吧~ 怎么使用公众号制作多图滑动的效果 在公众号文章中,如果插入的图片 ...

  8. flash特效原理 图片滑动放大效果

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 这几天, ...

  9. flash特效原理:图片滑动放大效果

    这几天,都在无所事事,唯一寄托就是在这里记录一下自己研究出来的东西.趁现在有点时间,就把最近看过的一种常用的flash特效记录一下,flash 做特效不是为做特效而做,在make thing move ...

最新文章

  1. 宝塔Linux/Windows面板如何添加网站?附图文教程
  2. chakra linux安装教程,Chakra Linux 安装指南(二):Chakra Linux 安装
  3. 通过/proc/PID/status查看进程内存占用情况
  4. 服务性服务–服务到服务的通话
  5. 类加载器ClassLoader
  6. opencv 任意角度旋转图像
  7. 用fuser或者lsof解决无法umount问题(device is busy)
  8. 计算机软件乘除,基于单片机的智能计算机程序 可以实现加减乘除运算
  9. 老杨和驳客都在胡言乱语——千条道理不如一个事实
  10. docker容器启动几分钟之后自动退出
  11. C语言栈的push函数,关于栈的有关问题。为什么Push函数top始终是NULL
  12. 你所不了解的“三消”游戏
  13. 计算机主机的组成部分,计算机主机的组成是有哪些
  14. Word2016中调节页眉内容一部分左对齐,一部分右对齐
  15. <<和>>运算符的用法
  16. WeChatExtension:一款mac微信必备插件!
  17. VvalidationError:Invalid options object.Ignore Plugin has been initialized using an options object
  18. python 中的“_,“有什么用途
  19. 主从博弈论文中关于均衡解证明的写作总结
  20. golang与面向接口编程

热门文章

  1. Flink中的CEP(二)
  2. 文件服务器映驱动,IIS虚拟目录实现与文件服务器网络驱动器映射共享
  3. Unity-Transform实例-小飞机
  4. 离散数学简单复习知识点汇总
  5. 拼多多有哪些API?
  6. CAD中如何把一个DWG文件里的块插入到另一个DWG中
  7. 基于Springboot健身会员俱乐部管理系统设计
  8. h5使用js的点击复制功能,兼容安卓和ios,亲测有效
  9. C语言入门学习——如何在程序中使用空格
  10. 差异表达基因热图怎么看_获得差异表达基因后-基因功能注释