基于Jquery开发 自己的幻灯片组件效果

 代码如下 复制代码

<!DOCTYPE HTML>
<html> 
<head>
<meta charset="utf-8">
<title>使用Jquery开发适合自己的幻灯片组件</title>
<meta name="description" content="使用Jquery开发适合自己的幻灯片组件,该组件支持2种形式,一种是模仿的淘宝首页幻灯,ie下使用vml实现圆角;另一种是可以使用css任意的自定义样式。" />
<meta name="keywords" content="nolure,幻灯片,jquery" />
<script src="/js/jquery/1.7/jquery.min.js"></script>
<link rel="stylesheet" href="/p/css/base.css"/>
</head>
<body>
<style type="text/css">
h1{font:26px/3 'microsoft yahei','simhei';color:#000;text-indent:2em;text-shadow:1px 1px 2px #ccc}
h3{margin-bottom:10px} 
.box{padding:20px 55px} 
.p_slide{border:1px solid #e4e4e4;overflow:hidden;position:relative;height:200px}
.p_slide .slide_c{height:100%;overflow:hidden}
.p_slide .slide_c li{height:100%}
.p_slide .slide_c li img{width:100%;height:100%}
.p_slide .slide_t li{cursor:pointer;float:left}
.p_slide .type1{position:absolute;bottom:5px;right:5px}
.p_slide .type1 li{width:20px;height:20px;line-height:20px;overflow:hidden;border-radius:20px;background:#fff;color:#DE7D4B;text-align:center;opacity:0.7;margin-left:3px}
.p_slide .type1 .current{background:#f60;color:#fff;font-weight:800;opacity:1}
.p_slide .type1 .ie{border:none;background:none;position:relative}

#slide2 .slide_c{height:164px}
#slide2 .type2{position:absolute;bottom:1px;left:1px;line-height:20px}
#slide2 .type2 li{background:#eee;color:#333;padding:7px 10px;margin-right:1px}
#slide2 .type2 .current{color:#c00;background:#ddd}
</style>
<h1>使用Jquery开发适合自己的幻灯片组件</h1>
<div class="box">
 <h3>type1:默认样式</h3>
 <div class="p_slide" id="slide1">
  <ul class="slide_c">
   <li style="background:#eee">1</li>
   <li style="background:#ccc">2</li>
   <li style="background:#eee">3</li>
   <li style="background:#ccc">4</li>
  </ul>
  <ul class="slide_t"></ul>
 </div>
</div>

<div class="box">
 <h3>type2:可任意自定义样式</h3>
 <div class="p_slide" id="slide2">
  <ul class="slide_c">
   <li style="background:#eee">1</li>
   <li style="background:#ccc">2</li>
   <li style="background:#eee">3</li>
   <li style="background:#ccc">4</li>
  </ul>
  <ul class="slide_t">
   <li>1</li>   <li>2</li>   <li>3</li>   <li>4</li>
  </ul>
 </div>
</div>
<script>
/*
 * 幻灯片组件
 * @id:容器ID
 * @timer:切换间隔时间
 * @opt:{type:幻灯片类型,默认1}
 */
var p_slide = function(id,timer,opt){
 if(!id){return;}
 this.box = $("#"+id);
 this.C = this.box.children(".slide_c");
 this.c = this.C.children("li");
 this.len = this.c.length;
 this.M = this.box.children(".slide_t");
 this.timer = timer || 4000;
 this.ie = /msie/.test(navigator.userAgent.toLowerCase());
 this.m = null;
 this.A = null;
 this.vml = [];
 this.nowIndex = 0;
 opt = opt || {};
 this.type = opt.type || 1;//幻灯片类型
 this.init();
}
p_slide.prototype = {
 init : function(){
  var T = this;
  if(this.type==1){
   var html = "";
   //ie下用vml创建圆形
   if(this.ie){
    document.namespaces.add("v", "urn:schemas-microsoft-com:vml"); //创建vml命名空间
    document.createStyleSheet().addRule("v\:oval", "behavior:url(#default#VML);display:inline-block;");
    for (var i = 0; i < this.len; i++) {
     this.vml[i] = document.createElement('<v:oval fillcolor="#fff" strokecolor="#fff" style="position:absolute;left:0;top:0;width:19px;height:19px"></v:oval>');
     this.vml[i].innerHTML = i+1;
    }
   }
   for(var i=1;i<=this.len;i++){    
    if (this.ie){
     html += '<li class="ie">';
    }else{
     html += '<li>';
    }
    html += i;
    html += '</li>';
   }   
   this.M.html(html).addClass("type1");
  }else if(this.type==2){
   this.M.addClass("type2");
  }
  this.m = this.M.children("li");
  
  //插入VML
  if (this.type==1&&this.ie) {
   for (var i = 0; i < this.len; i++) {
    this.m.eq(i).append(this.vml[i]);
   }
  } 
  
  this.bindEvent();
 },
 bindEvent : function(){
  var T = this;
  this.m.mouseover(function(){
   var m = $(this);
   window.clearInterval(T.A);
   T.change(m.index());
  })
  this.box.hover(function(){
   window.clearInterval(T.A);
  },function(){
   T.start();
  }).mouseout();
  T.change(T.nowIndex);
 },
 change : function(index){
  var T = this;
  index = (index>=this.len)?0:index;
  this.nowIndex = index ;
  var t = this.m.eq(index);
  t.addClass("current").siblings().removeClass("current");  
  if (this.type==1&&this.ie) {
   var m,v = this.vml[index];
   for(var i=0;i<this.len;i++){
    if(i==index){continue;}
    m = this.vml[i];
    m.fillcolor = '#fff';
    m.strokecolor = '#fff';
   }
   v.fillcolor = '#f60';
   v.strokecolor = '#f60';
  } 
  
  function animate(){
   var st = T.C.scrollTop(),
    St = T.c.outerHeight()*index,
    s = (St-st)/8,
    n = 15,
    A,
    Nt = st;
   A = setInterval(B,n);
   function B(){
    if(Math.abs(St-Nt)<=Math.abs(s)){
     clearInterval(A);
     T.C.scrollTop(St);
     return;
    }
    Nt += s;
    T.C.scrollTop(Nt);
   }
  }
  animate();
  
  this.nowIndex++;
 },
 start : function(){
  var T = this;
  this.A = window.setInterval(function(){
   T.change(T.nowIndex);
  },T.timer)
 }
}

new p_slide('slide1');
new p_slide('slide2',null,{type:2});
</script>
</body>
</html>

时间: 2024-07-30 02:31:28

基于Jquery开发 自己的幻灯片组件效果的相关文章

基于Jquery实现焦点图淡出淡入效果_jquery

本文实例讲述了基于Jquery实现焦点图淡出淡入效果代码.分享给大家供大家参考.具体如下: 这个容器用了百分比宽度,图片始终保持居中处理,定宽或者自适应宽度都是可以的. 兼容到IE6+以上浏览器,有淡出淡入速度和切换间隔两个参数可以改. 运行效果截图如下: 具体代码如下: Html代码如下: <!doctype html> <html lang="zh-CN"> <head> <meta charset="utf-8">

基于jquery的15款幻灯片插件_jquery

1,Gallerific Galleriffic 是使用 Mike Alsup 的 jQuery Cycle plugin 创建的图片展示效果,包括缩略图.图片标题和描述等详细功能,是一个非常不错的高质量画廊相册展示工具. 2,ZoomImage zoomimage ,以一种独具魅力的方式展示图片效果.缩略图链接在本页弹出大图,并且可以可以随意拖拽该悬浮图片. 3,EasySlider Easy Slider 可以实现图片或其他任意内容,在水平或垂直方向上的滑动效果,你还可以通过 CSS 自定义

基于jquery 完美的弹出层效果实例

一,基于jquery弹出层实例 先看实例  代码如下 复制代码 <!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> &l

基于jQuery实现多层次的手风琴效果附源码_jquery

基于jQuery多层次的手风琴是一款经过美化的多级多层次手风琴特效代码.效果图如下:   在线预览    源码下载 html代码: <aside class="accordion"> <h>News</h> <div class="opened-for-codepen"> <h>News Item #</h> <div class="opened-for-codepen"

基于jQuery滑动杆实现购买日期选择效果_jquery

这是一款基于jQuery的滑动杆购买日期选择插件,它的外观仿的是阿里云的服务器购买日期选择界面.这款jQuery插件非常适合在一些虚拟产品购买页面上使用,它可以帮助你的用户快速选择产品的购买日期,十分方便.效果图如下: 在线预览    源码下载 html代码: <center> <div class="slider-date" id="slider-date-"> <!--底层--> <ul class="slid

基于JQuery模拟的FLASH导航动画效果实现代码

这个分上下两个部分,上面是大分类,下面是二级分类,动画是一个向上,一个向下,动画利用JQuery中的animate可以做到,鼠标的移上和移出就是一个hover的事件进行触发,基本涉及的就主要是这两个函数了,其他的一些小地方就在实际的过程中进行细节调整就可以了. 于是就有了下面基于JQuery的核心脚本(这个代码有点长,其实没什么特别的地方,和我们平常写的基本一样,HTML和CSS请到DEMO页中查看,因有使用图片就不贴出来了) 核心代码如下  代码如下 复制代码 $("#nav>li>

基于jQuery实现简单的折叠菜单效果_jquery

本文实例讲述了JQuery实现简单的折叠菜单效果代码.分享给大家供大家参考.具体如下: 运行效果截图如下: Html代码如下: <div class="box"> <p>菜单一</p> <ul> <li><a>1111</a></li> <li><a>1111</a></li> <li><a>1111</a>

基于JQuery的数字改变的动画效果--可用来做计数器_jquery

因为要求是动态的,我就想到了应该是位置的变化,想到之前用过的JQuery,把里边的效果全试了试,最后选用了animate自定义,代码如下: 复制代码 代码如下: <html> <head> <title>testAnimate</title> <script type="text/javascript"> function changeNum(n) { //n设为想要改成的数字 $(function () { $("

两种方法基于jQuery实现IE浏览器兼容placeholder效果

 placeholder是HTML5<input>的属性之一,在不同的浏览器( 支持HTML5的现代浏览器 )中会有略微不同的显示效果: 在Chrome( v31.0.1650.63 m).Firefox( v21.0 ).360安全( v6.3 极速模式 )中,输入栏获得焦点后,提示文字并不消失,如图( Chrome ): 获得焦点前: 获得焦点时: 偏偏IE11要搞点特殊: 获得焦点前: 获得焦点时: 也就是说获得焦点时提示的文字会消失. 非现代浏览器( 例如 IE6-IE9 )是不支持p