switchable图片切换
前提: 最近由于项目的需要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图片切换的更多相关文章
- 推荐几款jquery图片切换插件
一.前言 毕业季到了,大家都在匆匆忙忙的记录大学里最美好的时光,照片中各种花式.各种姿势都涌现出来了.这么多的照片怎么展示出来给自己的好友看呢?有人选择做成视频,有人选择ps之后做成图片集,而我选择利 ...
- Android中使用ImageViewSwitcher实现图片切换轮播导航效果
前面写过了使用ViewFlipper和ViewPager实现屏幕中视图切换的效果(ViewPager未实现轮播)附链接: Android中使用ViewFlipper实现屏幕切换 Android中使用V ...
- JavaScript 最简单的图片切换
使用前在文件外部要有1.jpg 2.jpg 只是简单的模仿flash图片切换,可在此基础上引申出各种不同的效果. 思路: 建立一个数组存放图片的src,然后调用setInterval周期性的调用cha ...
- JQuery图片切换动画效果
由于博主我懒,所以页面画的比较粗糙,但是没关系,因为我主要讲的是如何实现图片动画切换. 思路:想必大家都逛过淘宝或者其他的一些网站,一般都会有图片动画切换的效果,那是怎样实现的呢?博主我呢,技术不是很 ...
- uwp 图片切换动画
最近在学习安卓,LOL自定义战绩项目近乎停工,而且腾旭把界面全改了,好烦.刚好学习到安卓中的图片切换动画,我就想在LOL项目中实现一个.首先上百度查看一下,妈的,资料少的可怜. 还是自己来吧.自定义控 ...
- css3全屏背景图片切换特效
效果体验:http://hovertree.com/texiao/css3/10/ 一般做图片切换效果,都会使用JS或者jQuery脚本,今天发现,其实只用CSS也可以实现.试试效果吧. 效果图: 代 ...
- js鼠标滚轮滚动图片切换效果
效果体验网址:http://keleyi.com/keleyi/phtml/image/12.htm HTML文件代码: <!DOCTYPE html PUBLIC "-//W3C// ...
- javascript马赛克遮罩图片切换效果:XMosaic.js(转)
新鲜出炉的javascript图片切换特效,实现的是马赛克遮罩切换.在flash里,好实现遮罩动画很简单,不过JS实现起来就有些困难了. XMosaic.js,与XScroll.js和XScroll2 ...
- jQuery旋转木马仿3D效果的图片切换特效代码
用jQuery实现的一款仿3D效果的图片切换特效代码,类似旋转木马一样,幻灯图片以三维视觉上下滑动切换,效果很酷炫,兼容IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜 ...
随机推荐
- 一些减少代码量、提高开发效率的利器(Java)
Spring Boot mybatis-plus代码生成器和自带CRUD接口 lombok 库: Apache Commons & guava AOP Java8: stream & ...
- JQ面试问题(转载)
1 你在公司是怎么用jquery的? 答:在项目中是怎么用的是看看你有没有项目经验(根据自己的实际情况来回答) 你用过的选择器啊,动画啊,表单啊,ajax事件等 配置Jquery环境 下载jquery ...
- JS 关于this p9
关于this这个货,常常让我感到头疼,也很难说清这货到底是什么机制,今天就详细记录一下this,了解他就跟理解闭包差不多,不理解的时候我们会感到很难受总想着避开他,当我们真正理解之后,会有种茅塞顿开的 ...
- 自定义多选框(checkbox)和单选框(radio)css样式
直接上代码: input[type="radio"],input[type="checkbox"]{ -webkit-appearance: none; out ...
- ionic1 项目微信支付
使用的插件参照地址:https://github.com/xu-li/cordova-plugin-wechat:(这里包含微信登录,微信分享和微信支付) 插件安装 cordova plugin ad ...
- MariaDB MySQL变量取值避免四舍五入的方法
MySQL变量取值避免四舍五入的方法 By:授客 QQ:1033553122 在一些对数据精确度要求比较高的场景(比如资金结算)下,变量取值时不能对变量值进行四舍五入操作,这时候就要做些预处理工作. ...
- JavaScript判断当前手机是Android还是iOS系统
$(function () { var u = navigator.userAgent, app = navigator.appVersion; var isAndroid = u.indexOf(' ...
- 网站与phpwind用户同步的方法
搭建了一个个人网站,希望使用phpwind来完成论坛功能.但很快就发现存在用户同步的问题,我的网站已经有了用户管理功能, phpwind论坛也有.因此用户同步注册,登陆和注销是必须要实现的. 网上说可 ...
- 70部MAYA灯光材质渲染教程合集
MAYA灯光材质渲染教程合集 教程格式:MP4和flv 两种格式 使用版本:教程不是一年出的教程,各个版本都有 (教程软件为英文版) 清晰度:可以看清软件上的文字 语言:部分中文字幕,其他英文(通过看 ...
- HTML5详解
本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. HTML5的介绍 Web 技术发展时间线 1991 HTML 1994 ...