前提: 最近由于项目的需要jquery "switchable图片切换"效果 所以趁着周末有空时间研究下 ,以前工作都依赖于kissy框架,所以也没有综合的写过类似的,如下图所示效果:

有左右按钮 和下面的数字按钮 点击左右按钮或者数字按钮切换到上一屏或者下一屏等。

HTML代码如下

<div class="wrapper">
<div class="focus" id="focus">
<ul>
<li>
<a href="#">
<img src="data:images/01.jpg"/>
</a>
</li>
<li>
<a href="#">
<img src="data:images/02.jpg"/>
</a>
</li>
<li>
<a href="#">
<img src="data:images/03.jpg"/>
</a>
</li>
<li>
<a href="#">
<img src="data:images/04.jpg"/>
</a>
</li>
</ul>
</div>
</div>

css代码如下:

<style>
*{margin:;padding:;}
body{font-size:12px;color:#222;font-family:Verdana,Arial,Helvetica,sans-serif;background:#f0f0f0;}
.clearfix:after{content: ".";display: block;height:;clear: both;visibility: hidden;}
.clearfix{zoom:;}
ul,li{list-style:none;}
img{border:;} .wrapper{width:800px;margin:0 auto;padding-bottom:50px;}
.focus{width:800px;height:280px;overflow:hidden;position:relative;}
.focus ul{height:380px;position:absolute;} .focus ul li{float:left;position:relative;width:800px;height:280px;overflow:hidden;} .focus ul li div{position:absolute;overflow:hidden;}
.focus .btnBg{position:absolute;width:800px;height:20px;left:;bottom:;background:#000;}
.focus .btn{position:absolute;width:780px;height:10px;padding:5px 10px;right:;bottom:;text-align:right;}
.focus .btn span{display:inline-block;_display:inline;_zoom:;width:25px;height:10px;_font-size:;margin-left:5px;cursor:pointer;background:#fff;opacity:0.4;filter:alpha(opacity=40);}
.focus .btn span.on{background:#fff;opacity:;filter:alpha(opacity=100);} .focus .preNext{width:45px;height:100px;position:absolute;top:90px;background:url(images/sprite.png) no-repeat 0 0;cursor:pointer;opacity:0.2;filter:alpha(opacity=20);}
.focus .current {
opacity:0.5;filter:alpha(opacity=50);
}
.focus .pre{left:;}
.focus .next{right:;background-position:right top;} </style>

JS代码如下:

/**
* switchable 切换
*/ $(function(){
function SwitchTab() {
this.config = {
wrapContainer : '#focus', // 焦点图的外部容器
prev : '.prev' , // 上一页按钮
next : '.next', // 下一页按钮
autoplay : true, // 是否自动播放 默认为自动
time : 3000, // 间隔时间
current : 'current', // 左右按钮当前状态
on : 'on', // 数字按钮当前状态
isNum : true // 是否动态生成数字按钮1,2,3,4 默认为true
}; this.cache = {
index : 0, //当前的索引
picTimer : undefined // 保存定时器的时间 };
} SwitchTab.prototype = { init: function(customConfig){
this.config = $.extend(this.config, customConfig || {});
var self = this,
_config = self.config,
_cache = self.cache; var sWidth = $(_config.wrapContainer).width(), //获取焦点图外层容器宽度
len = $(_config.wrapContainer + ' ul li').length; /* 下面的代码初始化 数字按钮 按钮半透明 上一页和下一页按钮*/
var btn = "<div class='btnBg'></div><div class='btn'>";
if(_config.isNum) {
for(var i = 0; i < len; i+=1) {
btn+= "<span></span>";
}
} btn += "</div><div class='preNext prev'></div><div class='preNext next'></div>";
$(_config.wrapContainer).append(btn); //为小按钮添加鼠标滑入事件,以显示相应的内容
$(_config.wrapContainer + ' .btn span') &&
$(_config.wrapContainer + ' .btn span').mouseover(function(){
_cache.index = $(_config.wrapContainer + ' .btn span').index(this);
t && clearTimeout(t);
var t = setTimeout(function(){
hover();
},100);
}).eq(0).trigger("mouseover"); function hover(){
self.showPics(_cache.index,sWidth);
} // 上一页 下一页按钮透明处理
$(_config.wrapContainer + ' .preNext').hover(function(){
$(this).stop(true,false).addClass(_config.current);
},function(){
$(this).stop(true,false).removeClass(_config.current);
}); // 上一页按钮
$(_config.prev).click(function(){
_cache.index--;
if(_cache.index == -1) {
_cache.index = len - 1;
} self.showPics(_cache.index,sWidth);
}); // 下一页按钮
$(_config.next).click(function(){
_cache.index++;
if(_cache.index == len) {
_cache.index = 0;
}
self.showPics(_cache.index,sWidth);
}); //本例为左右滚动,即所有li元素都是在同一排向左浮动,所以这里需要计算出外围ul元素的宽度
$(_config.wrapContainer + ' ul').css("width",sWidth * len); if(_config.autoplay) {
// 鼠标滑到焦点图时候 停止自动播放 滑出时自动播放
$(_config.wrapContainer).hover(function(){
_cache.picTimer && clearInterval(_cache.picTimer);
},function(){
_cache.picTimer = setInterval(function(){
self.showPics(_cache.index,sWidth);
_cache.index++;
if(_cache.index == len) {
_cache.index = 0;
}
},_config.time);
}).trigger("mouseleave");
}
},
showPics: function(index,sWidth){
var self = this,
_config = self.config,
nowLeft = -index*sWidth; //通过animate()调整ul元素滚动到计算出的position
$(_config.wrapContainer + " ul").stop(true,false).animate({"left":nowLeft},300);
$(_config.wrapContainer + ' .btn span') &&
$(_config.wrapContainer + ' .btn span').removeClass(_config.on).eq(index).addClass(_config.on); //为当前的按钮切换到选中的效果
}
} new SwitchTab().init({});
});

上面都有注释 就不用解释了哦!

switchable图片切换的更多相关文章

  1. 推荐几款jquery图片切换插件

    一.前言 毕业季到了,大家都在匆匆忙忙的记录大学里最美好的时光,照片中各种花式.各种姿势都涌现出来了.这么多的照片怎么展示出来给自己的好友看呢?有人选择做成视频,有人选择ps之后做成图片集,而我选择利 ...

  2. Android中使用ImageViewSwitcher实现图片切换轮播导航效果

    前面写过了使用ViewFlipper和ViewPager实现屏幕中视图切换的效果(ViewPager未实现轮播)附链接: Android中使用ViewFlipper实现屏幕切换 Android中使用V ...

  3. JavaScript 最简单的图片切换

    使用前在文件外部要有1.jpg 2.jpg 只是简单的模仿flash图片切换,可在此基础上引申出各种不同的效果. 思路: 建立一个数组存放图片的src,然后调用setInterval周期性的调用cha ...

  4. JQuery图片切换动画效果

    由于博主我懒,所以页面画的比较粗糙,但是没关系,因为我主要讲的是如何实现图片动画切换. 思路:想必大家都逛过淘宝或者其他的一些网站,一般都会有图片动画切换的效果,那是怎样实现的呢?博主我呢,技术不是很 ...

  5. uwp 图片切换动画

    最近在学习安卓,LOL自定义战绩项目近乎停工,而且腾旭把界面全改了,好烦.刚好学习到安卓中的图片切换动画,我就想在LOL项目中实现一个.首先上百度查看一下,妈的,资料少的可怜. 还是自己来吧.自定义控 ...

  6. css3全屏背景图片切换特效

    效果体验:http://hovertree.com/texiao/css3/10/ 一般做图片切换效果,都会使用JS或者jQuery脚本,今天发现,其实只用CSS也可以实现.试试效果吧. 效果图: 代 ...

  7. js鼠标滚轮滚动图片切换效果

    效果体验网址:http://keleyi.com/keleyi/phtml/image/12.htm HTML文件代码: <!DOCTYPE html PUBLIC "-//W3C// ...

  8. javascript马赛克遮罩图片切换效果:XMosaic.js(转)

    新鲜出炉的javascript图片切换特效,实现的是马赛克遮罩切换.在flash里,好实现遮罩动画很简单,不过JS实现起来就有些困难了. XMosaic.js,与XScroll.js和XScroll2 ...

  9. jQuery旋转木马仿3D效果的图片切换特效代码

    用jQuery实现的一款仿3D效果的图片切换特效代码,类似旋转木马一样,幻灯图片以三维视觉上下滑动切换,效果很酷炫,兼容IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜 ...

随机推荐

  1. Unable to open debugger port (127.0.0.1:63777): java.net.BindException "Address

    困扰了我好久,试过删掉taget文件夹rebuild,不删除Tomcat Server配置手动修改端口号也不行,试过杀掉java进程和重启机器,但是就是没效果. 解决: 删除Tomcat Server ...

  2. 微信公众号本地断点调试(frp反向代理或Remote Debugger)

    问题描述: 需要开发微信授权和订阅推送,但是感觉调试不方便,就试着几种方式.因为是用的C#开发,Visual Studio工具自带配套的远程工具 (Remote Debugger).但是感觉不稳定,容 ...

  3. 洛谷P4396 [AHOI2013]作业(树套树)

    题意 题目链接 Sol 为什么一堆分块呀..三维数点不应该是套路离线/可持久化+树套树么.. 亲测树状数组套权值线段树可过 复杂度\(O(nlog^2n)\),空间\(O(nlogn)\)(离线) # ...

  4. Nginx的防爬虫优化

    我们可以根据客户端的 user-agents 首部字段来阻止指定的爬虫爬取我们的网站: 虚拟主机配置如下:(红色标记为添加或者修改内容) [root@Nginx www_date]# cat bria ...

  5. WOSA/XFS PTR FORM—基础知识

    目录 一.XFS PTR 二.XFS PTR FORM 三.Form.子Form.字段.框架.表单和媒介的定义 正文 一.XFS PTR Class Name PTR Class Identifier ...

  6. ReactNative仿微信朋友圈App

    摘要: 欢迎各位同学加入: React-Native群:397885169 大前端群:544587175 大神超多,热情无私帮助解决各种问题. 一.前沿||潜心修心,学无止尽.生活如此,coding亦 ...

  7. 类与接口(五)java多态、方法重写、隐藏

    一.Java多态性 面向对象的三大特性:封装.继承.多态. 多态的类型,分为以下两种: 编译时多态: 指的是 方法重载.编译时多态是在编译时确定调用处选择那个重载方法,所以也叫 静态多态,算不上真正的 ...

  8. springcloud 入门 2 (Enreka的服务和注册)

    spring cloud eureka: eureka 用以服务发现.服务注册,比较流行的有consul(后面再介绍) eureka介绍: eureka为netflix开源软件,分为三个部分: eur ...

  9. Azure 托管镜像和非托管镜像对比

    目前中国区 Azure 也已经可以使用命令制作托管镜像了.但对于托管镜像和非托管镜像,就像托管磁盘和非托管磁盘一样,很多人可能一开始无法理解.这里就此进行了一个简单对比: 通过对比测试,这里总结了这两 ...

  10. ISO8583组包、解包

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace POS. ...