实现一个3D图片轮播插件 —— 更新版
前言:
前段时间写下了之前那篇 3D图片轮播效果,后来发现了 Pedro Botelho 写的jquery.gallery.js ,于是重新修改了自己的这个图片轮播,使之可以成为一个插件来使用。
基于jquery.gallery.js 添加了 自适应图片数量,以及添加了 swipe-indicators 切换按钮
源代码:here
demo:here
具体使用:
html结构:
<div id="swipe">
<div class="swipe-wrapper">
<div class="swipe-list">
<a href="#">
<img src="https://y.gtimg.cn/music/common/upload/t_focus_info_iphone/67011.jpg" alt="" class="swipe_list_pic">
</a>
</div>
<ol class='swipe-indicator'>
<li data-index="0"></li>
<li data-index="1"></li>
<!-- .... -->
</ol>
<nav class="swipe-action">
<a href="#" class="prev"><span>«</span></a>
<a href="#" class='next'><span>»</span></a>
</nav>
</div>
</div>
通过javascript使用
$("#swipe").Swipe()
选项 Options:
| Name | Type | Default |
| interval | number | 3000 |
| autoplay | boolean | false |
| current | number | 0 |
初始化方法 :Swipe(options)
使用可选选项初始化轮播,然后开始循环播放项目。
$('#swipe').Swipe({
interval: 2000,
autoplay:true
})
原理分析:
Swipe插件的几个主要私有方法:
_setItems():用于更新图片位置,五个主要元素,按照先后顺序分别为:this.$prevItem,this.$leftItem.this.$currentItem,this.$rightItem.this.$nextItem,顾名思义,大家很容易懂得
// 更新图片位置
_setItems: function () {
this.$items.removeClass('current'); this.$currentItem = this.$items.eq(this.current)
.addClass('current');
this.$leftItem = (this.current === 0) ? this.$items.eq(this.itemsCount -
1) : this.$items.eq(this.current - 1); this.$rightItem = (this.current === this.itemsCount - 1) ? this
.$items
.eq(0) : this.$items.eq(this.current + 1); //next & previous items
if (this.itemsCount > 3) {
// next item
this.$nextItem = (this.$rightItem.index() === this.itemsCount -
1) ?
this.$items.eq(0) : this.$rightItem.next(); // previous item
this.$prevItem = (this.$leftItem.index() === 0) ? this.$items
.eq(
this.itemsCount - 1) : this.$leftItem.prev();
}
},
根据this.current找到这五个元素,其他元素通过 opacity:0; 进行隐藏。
_layout():定义样式
_layout: function () {
// current, left and right items
this._setItems();
// current item is not changed
// left and right one are rotated and translated
this.$leftItem.css(this._getCoordinates('left'));
this.$rightItem.css(this._getCoordinates('right'));
this.$currentItem.css(this._getCoordinates('center'));
this.$nextItem.css(this._getCoordinates('outright'));
this.$prevItem
.css(this._getCoordinates('outleft'));
// 定义indicators样式,当前索引 高亮背景
this.$indicators.eq(this.current)
.css('background', 'rgb(244, 67, 54)')
.siblings()
.css("background", "rgba(0, 0, 0, 0.2)");
},
_getCoordinates(position):接受一个position参数,获取位置值,返回_layout()所需要的样式;【可通过修改此处的样式,自定义自己所需要的轮播效果】
具体看源代码
_loadEvent():初始化绑定各种事件
_slide(dir):接受一个滑动方向,用于图片滑动。根据滑动方向,调整this.current的索引,然后调用this._layout()进行图片位置更新和样式变化
_slide: function (dir) {
if (this.isAnim)
return false;
this.isAnim = true;
this.$items.addClass("swipe-transition");
switch (dir) {
case 'next':
this.current = this.$rightItem.index();
this._layout();
break;
case 'prev':
this.current = this.$leftItem.index();
this._layout();
break;
};
}
_switchItems():主要是用于indicators的切换图片
_cycle(): 定义一个定时器,用于图片循环
_cycle: function () {
var _self = this;
this.$cycle = setTimeout(function () {
_self._slide('next');
if (_self.options.autoplay) {
_self._cycle();
}
}, this.options.interval);
}
利用$.fn实现Swipe方法,看Jquery源码便可知:$.fn=$.prototype
$.fn.Swipe = function (options) {
if (options === undefined) options = {};
if (typeof options === 'object') {
this.each(function () {
// jQuery.data( element, key, value )
var instance = $.data(this, 'Swipe');
if (!instance) {
$.data(this, 'Swipe', new $.Swipe(options, this));
}
});
} else {
this.each(function () {
var instance = $.data(this, 'Swipe');
if (instance) {
switch (options) {
case 'cycle':
instance._cycle();
instance.options.autoplay = true;
break;
case 'stop':
instance._stopCycle();
instance.options.autoplay = false;
break;
case 'next':
instance._slide('next');
break;
case 'prev':
instance._slide('prev');
break;
default:
logError("no such method '" + options +
"' for Swipe instance");
break;
}
} else {
logError(
"cannot call methods on Swipe prior to initialization; " +
"attempted to call method '" + options + "'");
return;
}
});
}
return this;
};
此处给jQuery对象添加了一个Swipe()方法,接受一个可选选项,通过 $("#id").Swipe(); 可声明一个Swipe轮播对象,当Swipe对象初始化成功后,即可通过传入 string类型,调用API
// 可选方法
$("#id").Swipe('cycle')
循环通过旋转木马项目从左到右。 $("#id").Swipe('stop')
停止旋转木马循环播放项目。 $("#id").Swipe('prev')
循环到上一个项目。 $("#id").Swipe('next')
循环到下一个项目。
结束语
刚才在爱脚本网,发现了自己的这篇博文,因此重新附下此句版权声明
版权声明:本文为博主原创文章,未经博主允许不得转载。
实现一个3D图片轮播插件 —— 更新版的更多相关文章
- Javascript和jQuery WordPress 图片轮播插件, 内容滚动插件,前后切换幻灯片形式显示
用于在有限的网页空间内展示一组产品图片或者照片,同时还有非常吸引人的动画效果.本文向大家推荐12款实用的 jQuery 图片轮播效果插件,帮助你在你的项目中加入一些效果精美的图片轮播效果,希望这些插件 ...
- 12款经典的白富美型—jquery图片轮播插件—前端开发必备
图片轮播是网站中的常用功能,用于在有限的网页空间内展示一组产品图片或者照片,同时还有非常吸引人的动画效果.本文向大家推荐12款实用的 jQuery 图片轮播效果插件,帮助你在你的项目中加入一些效果精美 ...
- AdPlayBanner:功能丰富、一键式使用的图片轮播插件
AdPlayBanner:功能丰富.一键式使用的图片轮播插件 AdPlayBanner是一个Android平台基于ViewPager实现的轮播图插件,主要用以自动或者手动地播放轮播图,提供了Fresc ...
- PgwSlideshow-基于Jquery的图片轮播插件
0 PgwSlideshow简介 PgwSlideshow是一款基于Jquery的图片轮播插件,基本布局分为上下结构,上方为大图轮播区域,用户可自定义图片轮播切换的间隔时间,也可以通过单击左右方向按键 ...
- JQuery插件之图片轮播插件–slideBox
来源:http://www.ido321.com/852.html 今天偶然发现了一个比较好用的图片轮播插件—slideBox 先看看效果:http://slidebox.sinaapp.com/ 代 ...
- 结构-行为-样式-requireJs实现图片轮播插件
最近工作需要,就自己写了一个图片轮播插件,不过想到要集成到框架中,于是又用RequireJs改了一遍. 主要文件: style.css jquery-1.11.1.min.js require.js ...
- 面板支持单个,多个元素的jQuery图片轮播插件
一.先附上demo <!doctype html> <html> <head> <meta charset="utf-8"> < ...
- jquery做一个小的轮播插件---有BUG,后续修改
//首页无缝轮播 ; (function($, window, document, undefined) { $.fn.slider = function(options) { var default ...
- Nivo Slider - 世界上最棒的 jQuery 图片轮播插件
Nivo Slider 号称世界上最棒的图片轮播插件,有独立的 jQuery 插件和 WordPress 插件两个版本.目前下载量已经突破 1,800,000 次!jQuery 独立版本的插件主要有如 ...
随机推荐
- “玲珑杯”ACM比赛 Round #18 1147 - 最后你还是AK了(思维,边的贡献)
题目链接:http://www.ifrog.cc/acm/problem/1147 题解:这题很容易想到的是边的贡献也就是每条边最多被取到几次,和点的贡献类似,那些加边只要加在边贡献大的边上就行.然后 ...
- JOBDU 1140 八皇后
题目1140:八皇后 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1064 解决:665 题目描述: 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个 ...
- Unity3D 客户端编程
Photon Server 和 Unity3D 数据交互: Photon Server 服务端编程 Unity3D 客户端编程. VS2017 之 MYSQL实体数据模 1:打开unity新建新项目, ...
- 一文读懂 Android TouchEvent 事件分发、拦截、处理过程
什么是事件?事件是用户触摸手机屏幕,引起的一系列TouchEvent,包括ACTION_DOWN.ACTION_MOVE.ACTION_UP.ACTION_CANCEL等,这些action组合后变成点 ...
- 逆向破解之160个CrackMe —— 030
CrackMe —— 030 160 CrackMe 是比较适合新手学习逆向破解的CrackMe的一个集合一共160个待逆向破解的程序 CrackMe:它们都是一些公开给别人尝试破解的小程序,制作 c ...
- SpringCloud超简单的入门(1)--一些简单的介绍
简介 简单来说,springcloud的就是由一组springboot应用(服务)组成,相互之间通过REST等方式进行通信. 两个springboot应用,其中一个作为服务提供者,一个作为服务消费者, ...
- NUMA导致的MySQL服务器SWAP问题分析
[作者] 王栋:携程技术保障中心数据库专家,对数据库疑难问题的排查和数据库自动化智能化运维工具的开发有强烈的兴趣. [问题描述] 我们知道当mysqld进程使用到SWAP时,就会严重影响到MySQL的 ...
- DevExpress的TextEdit、RadioGroup、ColorPickEdit设置默认值
场景 Winform中实现ZedGraph的多条Y轴(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1001322 ...
- 注解@Async解决异步调用问题
序言:Spring中@Async 根据Spring的文档说明,默认采用的是单线程的模式的.所以在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的. 那么当多个任务的执行势必会相互影响. ...
- Linux环境下进行分布式压测踩过的坑
背景:公司为了满足大并发的情况,需要测试组配合,就需要分布式压测,这里我把我踩过坑都记录下来: 环境:Linux + jmeter-v.5.1.1;使用3台2核4G的压力机: Q1: Server f ...