JS实现的幻灯片切换显示效果


Posted in Javascript onSeptember 07, 2016

本文实例讲述了JS实现的幻灯片切换显示效果。分享给大家供大家参考,具体如下:

html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>纯JS幻灯版</title>
<style type="text/css">
*{ margin:0; padding:0;}
#banner {width:1000px;text-align:left; background:#fff; margin:0 auto;}
#banner img{ display:block; float:left;}
.mainbox{ overflow:hidden; position:relative;}
.flashbox{ overflow:hidden; position:relative;}
.imagebox{ text-align:right;position:relative;z-index:999;}
.bitdiv{display:inline-block;width:18px;height:18px;margin:0 10px 10px 0px;cursor:pointer;float:right;}
.defimg{ background:url(styles/images/ppt_icon2.png);}
.curimg{background:url(styles/images/ppt_icon1.png);}
</style>
<script type="text/javascript" src="styles/js/ppt_ban.js"></script>
</head>
<body>
<div id="banner">
  <script>
   var box =new PPTBox();
   box.width = 1000; //宽度
   box.height = 226;//高度
   box.autoplayer = 3;//自动播放间隔时间
   //box.add({"url":"图片地址","title":"悬浮标题","href":"链接地址"})
   box.add({"url":"styles/images/fck_04.jpg","href":"###","title":"悬浮提示标题1"});
   box.add({"url":"styles/images/fck_04.jpg","href":"###","title":"悬浮提示标题1"});
   box.add({"url":"styles/images/fck_04.jpg","href":"###","title":"悬浮提示标题1"});
   box.add({"url":"styles/images/fck_04.jpg","href":"###","title":"悬浮提示标题1"});
   box.show();
  </script>
</div>
</body>
</html>

js:

function PPTBox()
{
  this.uid = PPTBoxHelper.getId();
  PPTBoxHelper.instance[this.uid] = this;
  this._$ = function(id){return document.getElementById(id);};
  this.width = 400;//宽度
  this.height = 300;//高度
  this.picWidth = 15;//小图宽度
  this.picHeight = 12;//小图高度
  this.autoplayer = 4;//自动播放间隔(秒)
  this.target = "_blank";
  this._box = [];
  this._curIndex = 0;
}
PPTBox.prototype =
{
  _createMainBox : function (){
    var flashBoxWidth = this.width * this._box.length;
    var html="<div id='"+this.uid+"_mainbox' class='mainbox' style='width:"+(this.width)+"px;height:"+(this.height)+"px;'>";
    html += "<div id='"+this.uid+"_flashbox' class='flashbox' style='width:"+flashBoxWidth+"px;height:"+(this.height)+"px;'></div>";
    html += "<div id='"+this.uid+"_imagebox' class='imagebox' style='width:"+this.width+"px;height:"+(this.picHeight)+"px;top:-"+(this.picHeight+20)+"px;'></div>";
    html += "</div>";
    document.write(html);
  },
  _init : function (){
    var picstyle= "";
    var eventstr = "PPTBoxHelper.instance['"+this.uid+"']";
    var imageHTML="";
    var flashbox = "";
    for(var i=0;i<this._box.length;i++){
      var parame = this._box[i];
      flashbox += this.flashHTML(parame.url,this.width,this.height,i);
      imageHTML ="<div class='bitdiv "+((i==0)?"curimg":"defimg")+"' title ="+parame.title+" src='bit01.gif' "+picstyle+" onclick = \""+eventstr+".clickPic("+i+")\" onmouseover=\""+eventstr+".mouseoverPic("+i+")\"></div>" + imageHTML;
    }
    this._$(this.uid+"_flashbox").innerHTML = flashbox;
    this._$(this.uid+"_imagebox").innerHTML = imageHTML;
  },
  _play : function(){
    clearInterval(this._autoplay);
    var idx = this._curIndex+1;
    if(idx>=this._box.length){idx=0;}
    this.changeIndex(idx);
    var eventstr = "PPTBoxHelper.instance['"+this.uid+"']._play()";
    this._autoplay = setInterval(eventstr,this.autoplayer*1000);
  },
  flashHTML : function(url,width,height,idx) {
    var isFlash = url.substring(url.lastIndexOf('.')+1).toLowerCase()=="swf";
    var html = "";
    if(isFlash){
      html = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' "
      + "codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='"+width+"' height='"+height+"'>"
      + "<param name=\"movie\" value=\""+url+"\" />"
      + "<param name='quality' value='high' />"
      + "<param name='wmode' value='transparent'>"
      + "<embed src='"+url+"' quality='high' wmode='opaque' pluginspage='http://www.macromedia.com/go/getflashplayer'"
      +" type='application/x-shockwave-flash' width="+width+" height='"+height+"'></embed>"
      +" </object>";
    } else {
      var eventstr = "PPTBoxHelper.instance['"+this.uid+"']";
      var style = "";
      if(this._box[idx].href){
        style = "cursor:pointer"
      }
      html="<img src='"+url+"' style='width:"+width+"px;height:"+height+"px;"+style+"' onclick = \""+eventstr+".clickPic("+idx+")\"/>";
    }
    return html;
  },
  changeIndex : function(idx){
    var parame = this._box[idx];
    moveElement(this.uid+"_flashbox",-(idx*this.width),1);
    var imgs = this._$(this.uid+"_imagebox").getElementsByTagName("div");
    imgs[this._box.length-1-this._curIndex].className = "bitdiv defimg";
    imgs[this._box.length-1-idx].className = "bitdiv curimg";
    this._curIndex = idx;
  },
  mouseoverPic:function(idx){
    this.changeIndex(idx);
    if(this.autoplayer>0){
      clearInterval(this._autoplay);
      var eventstr = "PPTBoxHelper.instance['"+this.uid+"']._play()";
      this._autoplay = setInterval(eventstr,this.autoplayer*1000);
    }
  },
  clickPic:function(idx){
    var parame = this._box[idx];
    if(parame.href&¶me.href!=""){
      window.open(parame.href,this.target);
    }
  },
  add:function (imgParam){
    this._box[this._box.length] = imgParam;
  },
  show : function () {
    this._createMainBox();
    this._init();
    if(this.autoplayer>0){
      var eventstr = "PPTBoxHelper.instance['"+this.uid+"']._play()";
      this._autoplay = setInterval(eventstr,this.autoplayer*1000);
    }
  }
}
var PPTBoxHelper =
{
  count: 0,
  instance: {},
  getId: function() { return '_ppt_box-' + (this.count++); }
};
function moveElement(elementID,final_x,interval) {
 if (!document.getElementById) return false;
 if (!document.getElementById(elementID)) return false;
 var elem = document.getElementById(elementID);
 if (elem.movement) {
  clearTimeout(elem.movement);
 }
 if (!elem.style.left) {
  elem.style.left = "0px";
 }
 var xpos = parseInt(elem.style.left);
 if (xpos == final_x ) {
    return true;
 }
 if (xpos < final_x) {
  var dist = Math.ceil((final_x - xpos)/5);
  xpos = xpos + dist;
 }
 if (xpos > final_x) {
  var dist = Math.ceil((xpos - final_x)/5);
  xpos = xpos - dist;
 }
 elem.style.left = xpos + "px";
 var repeat = "moveElement('"+elementID+"',"+final_x+","+interval+")";
 elem.movement = setTimeout(repeat,interval);
}

效果图如下:

JS实现的幻灯片切换显示效果

希望本文所述对大家JavaScript程序设计有所帮助。

Javascript 相关文章推荐
获取Javscript执行函数名称的方法
Dec 22 Javascript
javascript中注册和移除事件的4种方式
Mar 20 Javascript
jQuery.event兼容各浏览器的event详细解析
Dec 18 Javascript
JQuery遍历json数组的3种方法
Nov 08 Javascript
轻量级网页遮罩层jQuery插件用法实例
Jul 31 Javascript
jQuery实现的手机发送验证码倒计时效果代码分享
Aug 24 Javascript
轻松学习jQuery插件EasyUI EasyUI创建树形菜单
Nov 30 Javascript
漂亮实用的页面loading(加载)封装代码
Feb 03 Javascript
微信小程序实现红包雨功能
Jul 11 Javascript
Vue.js中使用iView日期选择器并设置开始时间结束时间校验功能
Aug 12 Javascript
layui实现数据分页功能(ajax异步)
Jul 27 Javascript
微信小程序左滑删除实现代码实例
Sep 16 Javascript
javascript宿主对象之window.navigator详解
Sep 07 #Javascript
Angular 理解module和injector,即依赖注入
Sep 07 #Javascript
JS继承之借用构造函数继承和组合继承
Sep 07 #Javascript
Node.js读写文件之批量替换图片的实现方法
Sep 07 #Javascript
jQuery实现底部浮动窗口效果
Sep 07 #Javascript
聊一聊Vue.js过渡效果
Sep 07 #Javascript
BootStrap中的表单大全
Sep 07 #Javascript
You might like
PHP中shuffle数组值随便排序函数用法
2014/11/21 PHP
php读取本地json文件的实例
2018/03/07 PHP
php合并数组并保留键值的实现方法
2018/03/12 PHP
laravel5.6 框架操作数据 Eloquent ORM用法示例
2020/01/26 PHP
JSON 客户端和服务器端的格式转换
2009/08/27 Javascript
学习ExtJS table布局
2009/10/08 Javascript
javascript笔记 String类replace函数的一些事
2011/09/22 Javascript
JavaScript在XHTML中的用法详解
2013/04/11 Javascript
JS简单实现登陆验证附效果图
2013/11/19 Javascript
Javascript中引用示例介绍
2014/02/21 Javascript
javascript面向对象之定义成员方法实例分析
2015/01/13 Javascript
javascript实现带节日和农历的日历特效
2015/02/01 Javascript
jQuery leonaScroll 1.1 自定义滚动条插件(推荐)
2016/09/17 Javascript
微信小程序 input输入框控件详解及实例(多种示例)
2016/12/14 Javascript
bootstrap导航、选项卡实现代码
2016/12/28 Javascript
微信小程序出现wx.navigateTo页面不跳转问题的解决方法
2017/12/26 Javascript
vue非父子组件通信问题及解决方法
2018/06/11 Javascript
JavaScript创建对象方式总结【工厂模式、构造函数模式、原型模式等】
2018/12/19 Javascript
eslint+prettier统一代码风格的实现方法
2020/07/22 Javascript
Javascript如何实现扩充基本类型
2020/08/26 Javascript
[01:11]steam端dota2实名认证操作流程视频
2021/03/11 DOTA
windows系统中python使用rar命令压缩多个文件夹示例
2014/05/06 Python
Python 执行字符串表达式函数(eval exec execfile)
2014/08/11 Python
Python的Flask框架中SQLAlchemy使用时的乱码问题解决
2015/11/07 Python
Python Nose框架编写测试用例方法
2017/10/26 Python
人工智能最火编程语言 Python大战Java!
2017/11/13 Python
Django代码性能优化与Pycharm Profile使用详解
2018/08/26 Python
如何验证python安装成功
2020/07/06 Python
西班牙英格列斯百货英国官网:El Corte Inglés英国
2017/10/30 全球购物
欧洲最大的滑雪假期供应商之一:Sunweb Holidays
2018/01/06 全球购物
高三生物教学反思
2014/01/25 职场文书
四风问题个人自查剖析材料思想汇报
2014/09/21 职场文书
项目经理助理岗位职责
2015/04/13 职场文书
合作合同协议书
2016/03/21 职场文书
从贫穷到富有,是知识技能和学习力的差别
2019/08/20 职场文书
CSS3实现列表无限滚动/轮播效果
2021/06/23 HTML / CSS