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 相关文章推荐
JQuery 学习笔记 选择器之三
Jul 23 Javascript
一个简单的javascript类定义例子
Sep 12 Javascript
基于jQuery实现下拉收缩(展开与折叠)特效
Dec 25 Javascript
有效提高JavaScript执行效率的几点知识
Jan 31 Javascript
EasyUI中datagrid在ie下reload失败解决方案
Mar 09 Javascript
js验证真实姓名与身份证号是否匹配
Oct 13 Javascript
js自定义瀑布流布局插件
May 16 Javascript
vue Element-ui input 远程搜索与修改建议显示模版的示例代码
Oct 19 Javascript
使用Vue中 v-for循环列表控制按钮隐藏显示功能
Apr 23 Javascript
微信小程序request请求封装,验签代码实例
Dec 04 Javascript
React组件设计模式之组合组件应用实例分析
Apr 29 Javascript
基于Vue2实现移动端图片上传、压缩、拖拽排序、拖拽删除功能
Jan 05 Vue.js
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
咖啡与水的关系
2021/03/03 冲泡冲煮
PHP数组对比函数,存在交集则返回真,否则返回假
2011/02/03 PHP
php 目录与文件处理-郑阿奇(续)
2011/07/04 PHP
Yii框架ACF(accessController)简单权限控制操作示例
2019/04/26 PHP
一次失败的jQuery优化尝试小结
2011/02/06 Javascript
原生javaScript做得动态表格(注释写的很清楚)
2013/12/29 Javascript
jQuery中delegate和on的用法与区别详细解析
2014/01/26 Javascript
jquery常用操作小结
2014/07/21 Javascript
jQuery选择器之基本选择器与层次选择器
2015/03/03 Javascript
JS实现购物车特效
2017/02/02 Javascript
解决koa2 ctx.render is not a function报错问题
2018/08/07 Javascript
vue中,在本地缓存中读写数据的方法
2018/09/21 Javascript
js for终止循环 跳出多层循环
2018/10/04 Javascript
解决JQuery的ajax函数执行失败alert函数弹框一闪而过问题
2019/04/10 jQuery
webpack4 SplitChunks实现代码分隔详解
2019/05/23 Javascript
vue.js中导出Excel表格的案例分析
2019/06/11 Javascript
[01:00] DOTA2英雄背景故事第五期之重力引力法则谜团
2020/07/16 DOTA
Python素数检测的方法
2015/05/11 Python
Python3编程实现获取阿里云ECS实例及监控的方法
2017/08/18 Python
Pandas探索之高性能函数eval和query解析
2017/10/28 Python
python版大富翁源代码分享
2018/11/19 Python
Numpy 中的矩阵求逆实例
2019/08/26 Python
pytorch点乘与叉乘示例讲解
2019/12/27 Python
Python3.9新特性详解
2020/10/10 Python
html5基础教程常用技巧整理
2013/08/20 HTML / CSS
美国在线珠宝商店:SZUL
2017/02/11 全球购物
日本小田急百货官网:Odakyu
2018/07/19 全球购物
新加坡一家在线男士皮具品牌:Faire Leather Co.
2019/12/01 全球购物
main 主函数执行完毕后,是否可能会再执行一段代码,给出说明
2012/12/05 面试题
大学新生军训感言
2014/02/25 职场文书
银行会计主管岗位职责
2014/10/01 职场文书
培训通知
2015/04/17 职场文书
2015年教务主任工作总结
2015/07/22 职场文书
2015年三好一满意工作总结
2015/07/24 职场文书
2016年学校招生广告语
2016/01/28 职场文书
Ruby使用Mysql2连接操作MySQL
2022/04/19 Ruby