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 相关文章推荐
js技巧--转义符&quot;\&quot;的妙用
Jan 09 Javascript
jQuery使用toggleClass方法动态添加删除Class样式的方法
Mar 26 Javascript
在Python中使用glob模块查找文件路径的方法
Jun 17 Javascript
jQuery使用经验小技巧(推荐)
May 31 Javascript
Bootstrap文件上传组件之bootstrap fileinput
Nov 25 Javascript
js css自定义分页效果
Feb 24 Javascript
兼容浏览器的js事件绑定函数(详解)
May 09 Javascript
vue使用stompjs实现mqtt消息推送通知
Jun 22 Javascript
vue中$set的使用(结合在实际应用中遇到的坑)
Jul 10 Javascript
在js代码拼接dom对象到页面上的模板总结
Oct 21 Javascript
Vue简单实现原理详解
May 07 Javascript
一文秒懂JavaScript构造函数、实例、原型对象以及原型链
Aug 25 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+javascript液晶时钟
2006/10/09 PHP
让你同时上传 1000 个文件 (一)
2006/10/09 PHP
php引用地址改变变量值的问题
2012/03/23 PHP
PHP实现图片不变型裁剪及图片按比例裁剪的方法
2016/01/14 PHP
PHP实践教程之过滤、验证、转义与密码详解
2017/07/24 PHP
&amp;lt;script defer&amp;gt; defer 是什么意思
2009/05/10 Javascript
深入理解JavaScript系列(28):设计模式之工厂模式详解
2015/03/03 Javascript
JavaScript更改字符串的大小写
2015/05/07 Javascript
jquery.mousewheel实现整屏翻屏效果
2015/08/30 Javascript
javascript Slip.js实现整屏滑动的手机网页
2015/11/25 Javascript
Vue项目中quill-editor带样式编辑器的使用方法
2017/08/08 Javascript
p5.js入门教程和基本形状绘制
2018/03/15 Javascript
webstorm中vue语法的支持详解
2018/05/09 Javascript
详解Axios 如何取消已发送的请求
2018/10/20 Javascript
Vue插槽原理与用法详解
2019/03/05 Javascript
vue webpack build资源相对路径的问题及解决方法
2020/06/04 Javascript
vue 监听窗口变化对页面部分元素重新渲染操作
2020/07/28 Javascript
[03:02]2020完美世界城市挑战赛(秋季赛)总决赛回顾
2021/03/11 DOTA
Python+Opencv识别两张相似图片
2020/03/23 Python
Python数据分析之如何利用pandas查询数据示例代码
2017/09/01 Python
将TensorFlow的模型网络导出为单个文件的方法
2018/04/23 Python
Python控制键盘鼠标pynput的详细用法
2019/01/28 Python
python中嵌套函数的实操步骤
2019/02/27 Python
python中metaclass原理与用法详解
2019/06/25 Python
opencv 实现特定颜色线条提取与定位操作
2020/06/02 Python
keras中的History对象用法
2020/06/19 Python
Pandas中两个dataframe的交集和差集的示例代码
2020/12/13 Python
pytorch 把图片数据转化成tensor的操作
2021/03/04 Python
HTML5新增的标签和属性归纳总结
2018/05/02 HTML / CSS
bareMinerals官网:矿物质化妆品和护肤品
2018/02/04 全球购物
C#面试常见问题
2013/02/25 面试题
上班离岗检讨书
2014/01/27 职场文书
事业单位财务人员岗位职责
2015/04/14 职场文书
护士旷工检讨书
2015/08/15 职场文书
在Docker容器中部署SQL Server
2022/04/11 Servers
Python何绘制带有背景色块的折线图
2022/04/23 Python