前提: 最近由于项目的需要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. 设计模式-组合模式(Composite)

    一.概念 将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有一致性. 二.模式动机 组合模式,通过设计一个抽像的组件类,使它既代表叶子对象,又代表组合对 ...

  2. 求解方程A5+B5+C5+D5+E5=F5

    方程A5+B5+C5+D5+E5=F5刚好有一个满足0<A≤B≤C≤D≤E≤F≤75的整数解.请编写一个求出该解的程序: using System; namespace ReverseTheEx ...

  3. 创建Cordova项目 报错Error: Unhandled "error" event

    cordova版本7.0以上版本 创建cordova项目错误信息 Error: Unhandled "error" event. (  Error from Cordova Fet ...

  4. HDU1824(2-SAT)

    Let's go home Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. 大数据java基础day01

    day01笔记 1.==操作符和equals方法 equals方法存在于Object类中,所有类的equals方法都继承于Object 2.String类的常用方法 ①.replace()替换字符串 ...

  6. js-权威指南学习笔记20

    第二十章 客户端存储 1.客户端存储有一下几种形式:Web存储.cookie.IE userData.离线Web应用.Web数据库.文件系统API. 2.Web存储标准所描述的API包含localSt ...

  7. 记录常用的git命令

    之前的公司都是用svn的,现在的公司用git了,所以现在记录一下所用到的git命令,方便以后查看! git 常用命令 git add git add 命令可将该文件添加到缓存 git add . // ...

  8. 从零开始学习html(十二)CSS布局模型——下

    五.什么是层模型? 什么是层布局模型?层布局模型就像是图像软件PhotoShop中非常流行的图层编辑功能一样, 每个图层能够精确定位操作,但在网页设计领域,由于网页大小的活动性,层布局没能受到热捧. ...

  9. input框中自动展示当前日期 yyyy/mm/dd

    直接上代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  10. MSCRM中报表开发一:创建基于SQL报表

    1.       新建报表项目.打开SQL Server Business Intelligence Development Studio,点击 文件 > 新建 > 项目,项目类型选择 商 ...