根据插件Swipe,结合jQuery封装成的新的jQuery插件
swipe支持电脑上的自动滑动,也支持手机端的滑动效果.但是每次调用只能支持一个效果或者说一个页面出现n个这样的效果,我们就得调用n次这个插件.
我使用swipe+jQuery使得swip变得方便使用,具有了jQuery插件的有点.只需要一个调用就可以使页面拥有n个这样的效果.
另外在效果中置入了鼠标的点击效果.这个接口是早就给出来了,可是很少在网上看到有人用过这个接口的.

<style type="text/css">
#gys img{height:300px;}
</style>
<div class="gys" id="gys">
<div style="background-color:Red;">第一张</div>
<div style="background-color:Green">第二章</div>
<div style="background-color:Gray;">第三章</div>
<div style="background-color:Red;">第四张</div>
<div style="background-color:Green">第五章</div>
<div style="background-color:Gray;">第六章</div>
</div>
<style type="text/css">
#gys2{ margin-top:20px;}
#gys2 img{height:300px;}
</style>
<div class="gys" id="gys2">
<div><img src="1.jpg" style=""/></div>
<div><img src="2.jpg" style=""/></div>
<div><img src="3.jpg" style=""/></div>
<div><img src="4.jpg" style=""/></div>
<div><img src="5.jpg" style=""/></div>
<div><img src="6.jpg" style=""/></div>
</div> <style type="text/css">
#gys3{ margin-top:20px;}
#gys3 > div >div{height:200px;}
</style>
<div class="gys" id="gys3">
<div style='background:url(1.jpg) center no-repeat;background-size: cover;'><h1>1</h1></div>
<div style='background:url(2.jpg) center no-repeat;background-size: cover;'><h1>2</h1></div>
<div style='background:url(3.jpg) center no-repeat;background-size: cover;'><h1>3</h1></div>
<div style='background:url(4.jpg) center no-repeat;background-size: cover;'><h1>4</h1></div>
<div style='background:url(5.jpg) center no-repeat;background-size: cover;'><h1>5</h1></div>
<div style='background:url(6.jpg) center no-repeat;background-size: cover;'><h1>6</h1></div>
</div>
; (function ($) {
var defaults={
width:"",//幻灯片的宽度
startSlide: 2,//Swipe开始的索引
speed: 400, //前进和后台的速度,单位毫秒
auto: 3000,//自动滑动
nav:1,//是否有按钮导航,1.有,2.没有
continuous: true, //是否可以循环播放
disableScroll: false, //停止触摸滑动
};
$.fn.bnWapPPt=function(opt){
opt=$.extend({},defaults,opt);
return this.each(function(){
if(!opt.width)
opt.width=$(this).parent().width();
var children=$(this).children()
var count=children.length;
$(this).css({"overflow": "hidden","visibility":"hidden","position":"relative",width:opt.width+"px"});
children.css({"float":"left","position":"relative","overflow":"hidden","height":"100%"});
children.wrapAll("<div style='overflow: hidden;position: relative;'></div>");
if(opt.nav==1){
var html="<div class='jsSwipeNavBj'><ul class='jsSwipeNav'>";
for(var i=0;i<count;i++){
if(i==opt.startSlide)
html+="<li class='on'></li>";
else
html+="<li></li>";
}
html+="</ul></div>";
$(this).append(html);
$("ul.jsSwipeNav li",this).click(function(){
$(this).siblings().removeClass("on");
$(this).addClass("on");
SwipeResult.gys($(this).index());
});
}
var obj=this;
if(opt.nav==1){
var navLis=$("ul.jsSwipeNav li",obj);
var liWidth=0;
navLis.each(function(){
liWidth+=$(this).outerWidth(true);
});
$("ul.jsSwipeNav",obj).width(liWidth).css({"overflow":"hidden"});
}
//通过递归获取底部导航的索引
function getIndex(index,n){
if(index>n){
return arguments.callee(index-=count,n);
}
else {
return index;
}
}
var navsChange=function (nav){
if(nav==1){
return function(index){
index=getIndex(index,count-1);
navLis.removeClass("on").eq(index).addClass("on");
}
}
else
{
return function(){}
}
}(opt.nav);
var SwipeResult=Swipe($(this).get(0),{
startSlide: opt.startSlide,//Swipe开始的索引
speed: opt.speed, //前进和后台的速度,单位毫秒
auto: opt.auto,//自动滑动
continuous: opt.continuous, //是否可以循环播放
disableScroll: opt.disableScroll, //停止触摸滑动
stopPropagation: false, //停止事件传播
callback: function (index, elem) {//回调函数,可以获取到滑动中图片的索引.
navsChange(index);
},
transitionEnd: function (index, elem) {//在滑动过渡时执行.
}
})
});
}
function Swipe(container, options) {
"use strict";
var noop = function () { }; // simple no operation function
var offloadFn = function (fn) { setTimeout(fn || noop, 0) }; // offload a functions execution
var jsLib = window.jQuery || window.Zepto; // jQuery or Zepto
// check browser capabilities
var browser = {
addEventListener: !!window.addEventListener,
touch: ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch,
transitions: (function (temp) {
var props = ['transitionProperty', 'WebkitTransition', 'MozTransition', 'OTransition', 'msTransition'];
for (var i in props) if (temp.style[props[i]] !== undefined) return true;
return false;
})(document.createElement('swipe'))
};
// quit if no root element
if (!container) return; //如果容器为假,不执行
var element = container.children[0]; //容器中的第一个子元素(运动区域的元素集合)
var slides, slidePos, width, length;
options = options || {};
var index = parseInt(options.startSlide, 10) || 0;
var speed = options.speed || 300;
options.continuous = options.continuous !== undefined ? options.continuous : true;
function setup() {
// cache slides
slides = element.children; //figure集合
length = slides.length; //figure数
// set continuous to false if only one slide
if (slides.length < 2) options.continuous = false;
//special case if two slides
if (browser.transitions && options.continuous && slides.length < 3) {
// return;
element.appendChild(slides[0].cloneNode(true));
element.appendChild(element.children[1].cloneNode(true));
slides = element.children;
}
// create an array to store current positions of each slide
slidePos = new Array(slides.length);
// determine width of each slide
width = container.getBoundingClientRect().width || container.offsetWidth;
element.style.width = (slides.length * width) + 'px';
// stack elements
var pos = slides.length;
while (pos--) {
var slide = slides[pos];
slide.style.width = width + 'px';
slide.setAttribute('data-index', pos);
if (browser.transitions) {
slide.style.left = (pos * -width) + 'px';
move(pos, index > pos ? -width : (index < pos ? width : 0), 0);
}
}
// reposition elements before and after index
if (options.continuous && browser.transitions) {
move(circle(index - 1), -width, 0);
move(circle(index + 1), width, 0);
}
if (!browser.transitions) element.style.left = (index * -width) + 'px';
container.style.visibility = 'visible';
}
function prev() {
if (options.continuous) slide(index - 1);
else if (index) slide(index - 1);
}
function next() {
if (options.continuous) slide(index + 1);
else if (index < slides.length - 1) slide(index + 1);
}
function circle(index) {
// a simple positive modulo using slides.length
return (slides.length + (index % slides.length)) % slides.length;
}
function gys(container, options,gysIndex){
options=$.extend(options,gysIndex);
Swipe(container, options);
}
function slide(to, slideSpeed) {
// do nothing if already on requested slide
if (index == to) return;
if (browser.transitions) {
var direction = Math.abs(index - to) / (index - to); // 1: backward, -1: forward
// get the actual position of the slide
if (options.continuous) {
var natural_direction = direction;
direction = -slidePos[circle(to)] / width;
// if going forward but to < index, use to = slides.length + to
// if going backward but to > index, use to = -slides.length + to
if (direction !== natural_direction) to = -direction * slides.length + to;
}
var diff = Math.abs(index - to) - 1;
// move all the slides between index and to in the right direction
while (diff--) move(circle((to > index ? to : index) - diff - 1), width * direction, 0);
to = circle(to);
move(index, width * direction, slideSpeed || speed);
move(to, 0, slideSpeed || speed);
if (options.continuous) move(circle(to - direction), -(width * direction), 0); // we need to get the next in place
} else {
to = circle(to);
animate(index * -width, to * -width, slideSpeed || speed);
//no fallback for a circular continuous if the browser does not accept transitions
}
index = to;
offloadFn(options.callback && options.callback(index, slides[index]));
}
function move(index, dist, speed) {
translate(index, dist, speed);
slidePos[index] = dist;
}
function translate(index, dist, speed) {
var slide = slides[index];
var style = slide && slide.style;
if (!style) return;
style.webkitTransitionDuration =
style.MozTransitionDuration =
style.msTransitionDuration =
style.OTransitionDuration =
style.transitionDuration = speed + 'ms';
style.webkitTransform = 'translate(' + dist + 'px,0)' + 'translateZ(0)';
style.msTransform =
style.MozTransform =
style.OTransform = 'translateX(' + dist + 'px)';
}
function animate(from, to, speed) {
// if not an animation, just reposition
if (!speed) {
element.style.left = to + 'px';
return;
}
var start = +new Date;
var timer = setInterval(function () {
var timeElap = +new Date - start;
if (timeElap > speed) {
element.style.left = to + 'px';
if (delay) begin();
options.transitionEnd && options.transitionEnd.call(event, index, slides[index]);
clearInterval(timer);
return;
}
element.style.left = (((to - from) * (Math.floor((timeElap / speed) * 100) / 100)) + from) + 'px';
}, 4);
}
// setup auto slideshow
var delay = options.auto || 0;
var interval;
function begin() {
interval = setTimeout(next, delay);
}
function stop() {
delay = 0;
clearTimeout(interval);
}
// setup initial vars
var start = {};
var delta = {};
var isScrolling;
// setup event capturing
var events = {
handleEvent: function (event) {
switch (event.type) {
case 'touchstart': this.start(event); break;
case 'touchmove': this.move(event); break;
case 'touchend': offloadFn(this.end(event)); break;
case 'webkitTransitionEnd':
case 'msTransitionEnd':
case 'oTransitionEnd':
case 'otransitionend':
case 'transitionend': offloadFn(this.transitionEnd(event)); break;
case 'resize': offloadFn(setup); break;
}
if (options.stopPropagation) event.stopPropagation();
},
start: function (event) {
var touches = event.touches[0];
// measure start values
start = {
// get initial touch coords
x: touches.pageX,
y: touches.pageY,
// store time to determine touch duration
time: +new Date
};
// used for testing first move event
isScrolling = undefined;
// reset delta and end measurements
delta = {};
// attach touchmove and touchend listeners
element.addEventListener('touchmove', this, false);
element.addEventListener('touchend', this, false);
},
move: function (event) {
// ensure swiping with one touch and not pinching
if (event.touches.length > 1 || event.scale && event.scale !== 1) return
if (options.disableScroll) event.preventDefault();
var touches = event.touches[0];
// measure change in x and y
delta = {
x: touches.pageX - start.x,
y: touches.pageY - start.y
}
// determine if scrolling test has run - one time test
if (typeof isScrolling == 'undefined') {
isScrolling = !!(isScrolling || Math.abs(delta.x) < Math.abs(delta.y));
}
// if user is not trying to scroll vertically
if (!isScrolling) {
// prevent native scrolling
event.preventDefault();
// stop slideshow
stop();
// increase resistance if first or last slide
if (options.continuous) { // we don't add resistance at the end
translate(circle(index - 1), delta.x + slidePos[circle(index - 1)], 0);
translate(index, delta.x + slidePos[index], 0);
translate(circle(index + 1), delta.x + slidePos[circle(index + 1)], 0);
} else {
delta.x =
delta.x /
((!index && delta.x > 0 // if first slide and sliding left
|| index == slides.length - 1 // or if last slide and sliding right
&& delta.x < 0 // and if sliding at all
) ?
(Math.abs(delta.x) / width + 1) // determine resistance level
: 1); // no resistance if false
// translate 1:1
translate(index - 1, delta.x + slidePos[index - 1], 0);
translate(index, delta.x + slidePos[index], 0);
translate(index + 1, delta.x + slidePos[index + 1], 0);
}
}
},
end: function (event) {
// measure duration
var duration = +new Date - start.time;
// determine if slide attempt triggers next/prev slide
var isValidSlide =
Number(duration) < 250 // if slide duration is less than 250ms
&& Math.abs(delta.x) > 20 // and if slide amt is greater than 20px
|| Math.abs(delta.x) > width / 2; // or if slide amt is greater than half the width
// determine if slide attempt is past start and end
var isPastBounds =
!index && delta.x > 0 // if first slide and slide amt is greater than 0
|| index == slides.length - 1 && delta.x < 0; // or if last slide and slide amt is less than 0
if (options.continuous) isPastBounds = false;
// determine direction of swipe (true:right, false:left)
var direction = delta.x < 0;
// if not scrolling vertically
if (!isScrolling) {
if (isValidSlide && !isPastBounds) {
if (direction) {
if (options.continuous) { // we need to get the next in this direction in place
move(circle(index - 1), -width, 0);
move(circle(index + 2), width, 0);
} else {
move(index - 1, -width, 0);
}
move(index, slidePos[index] - width, speed);
move(circle(index + 1), slidePos[circle(index + 1)] - width, speed);
index = circle(index + 1);
} else {
if (options.continuous) { // we need to get the next in this direction in place
move(circle(index + 1), width, 0);
move(circle(index - 2), -width, 0);
} else {
move(index + 1, width, 0);
}
move(index, slidePos[index] + width, speed);
move(circle(index - 1), slidePos[circle(index - 1)] + width, speed);
index = circle(index - 1);
}
options.callback && options.callback(index, slides[index]);
} else {
if (options.continuous) {
move(circle(index - 1), -width, speed);
move(index, 0, speed);
move(circle(index + 1), width, speed);
} else {
move(index - 1, -width, speed);
move(index, 0, speed);
move(index + 1, width, speed);
}
}
}
// kill touchmove and touchend event listeners until touchstart called again
element.removeEventListener('touchmove', events, false)
element.removeEventListener('touchend', events, false)
},
transitionEnd: function (event) {
if (parseInt(event.target.getAttribute('data-index'), 10) == index) {
if (delay) begin();
options.transitionEnd && options.transitionEnd.call(event, index, slides[index]);
}
}
}
// trigger setup
setup();
// start auto slideshow if applicable
if (delay) begin();
// add event listeners
if (browser.addEventListener) {
// set touchstart event on element
if (browser.touch) element.addEventListener('touchstart', events, false);
if (browser.transitions) {
element.addEventListener('webkitTransitionEnd', events, false);
element.addEventListener('msTransitionEnd', events, false);
element.addEventListener('oTransitionEnd', events, false);
element.addEventListener('otransitionend', events, false);
element.addEventListener('transitionend', events, false);
}
// set resize event on window
window.addEventListener('resize', events, false);
} else {
window.onresize = function () { setup() }; // to play nice with old IE
}
// expose the Swipe API
return {
setup: function () {
setup();
},
gys:function(to){
stop();
gys(container, options,{startSlide:to});
},
slide: function (to, speed) {
stop();
slide(to, speed);
},
prev: function () {
// cancel slideshow
stop();
prev();
},
next: function () {
// cancel slideshow
stop();
next();
},
stop: function () {
// cancel slideshow
stop();
},
getPos: function () {
// return current index position
return index;
},
getNumSlides: function () {
// return total number of slides
return length;
},
kill: function () {
// cancel slideshow
stop();
// reset element
element.style.width = 'auto';
element.style.left = 0;
// reset slides
var pos = slides.length;
while (pos--) {
var slide = slides[pos];
slide.style.width = '100%';
slide.style.left = 0;
if (browser.transitions) translate(pos, 0, 0);
}
// removed event listeners
if (browser.addEventListener) {
// remove current event listeners
element.removeEventListener('touchstart', events, false);
element.removeEventListener('webkitTransitionEnd', events, false);
element.removeEventListener('msTransitionEnd', events, false);
element.removeEventListener('oTransitionEnd', events, false);
element.removeEventListener('otransitionend', events, false);
element.removeEventListener('transitionend', events, false);
window.removeEventListener('resize', events, false);
}
else {
window.onresize = null;
}
}
}
}
})(jQuery);
调用:
$(function(){
$(".gys").bnWapPPt({
width:500,//幻灯片宽度
startSlide: 1,//Swipe开始的索引
speed: 400, //滑动的速度,单位毫秒
auto: 3000,//自动滑动之间的间隔时间
nav:1,//是否有底部原点的导航.1表示有,其他任何值表示没有
continuous: true, //是否可以循环播放
disableScroll: false //停止触摸滑动
});
})
根据插件Swipe,结合jQuery封装成的新的jQuery插件的更多相关文章
- EasyUI 日期选择插件封装成选择到月份的插件
将普通的日期选择插件封装成选择到月份的插件: var nowMonth = new Date(); var month = ...
- 自己做jQuery插件:将audio5js封装成jQuery语音播放插件
日前的一个项目需要用到语音播放功能.发现Audio5js符合需求且使用简单,又鉴于jQuery控件便于开发操作,于是有了以下的封装. 首先先简单介绍一下Audio5js吧. Audio5js是一个能够 ...
- 把jQuery的类、插件封装成seajs的模块的方法
这篇文章主要介绍了把jQuery的类.插件封装成seajs的模块的方法,需要的朋友可以参考下 注:本文使用的seajs版本是2.1.1 一.把Jquery封装成seajs的模块 define(func ...
- Jquery封装ajax
Jquery封装ajax Load方法 <!-- 将jquery.js导入进来 --> <script type="text/javascript&qu ...
- 如何将Js代码封装成Jquery插件
很多相同的Jquery代码会在很多页面使用,每次都复制粘贴太麻烦了,不如封装成一个Jquery插件就方便了,至于影响网页的速度不,我就没有测试了哈. 代码如下 这是一个自定闪烁打印文字的Jquery特 ...
- 星级评分--封装成jquery插件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- js封装成插件
由于项目原因,工作一年多还没用js写过插件,项目太成熟,平时基本都是在使用已经封装好的功能插件.感觉自己好low......这两天想自己抽空写一个canvas画统计图与折现图的插件,所以就去网上学习了 ...
- jquery 封装插件
如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jquery 及ui 内置web项目里了.至于使用jquery好处这里就不再赘述了,用过的都知道.今天我们来讨论下jq ...
- 如何用jQuery封装插件
引子 现在网上关于js和jquery封装的插件很多,我刚刚接触前端的时候,就很敬佩那些自己写插件的大牛们!因为是他们给网站开发更多的便利,很多网页效果,网上很多现成的插件!那么这些插件是如何写的呢?首 ...
随机推荐
- [WCF安全2]使用wsHttpBinding构建UserName授权的WCF应用程序,非SSL
上一篇文章中介绍了如何使用basicHttpBinding构建UserName授权的WCF应用程序,本文将为您介绍如何使用wsHttpBinding构建非SSL的UserName安全授权的WCF应用程 ...
- linux find/grep 与cat联合使用
find ./ -name file_name.txt | xargs cat >> file_name.txt
- Uva 11178 Morley's Theorem 向量旋转+求直线交点
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=9 题意: Morlery定理是这样的:作三角形ABC每个 ...
- Iterator(迭代器)
意图: 提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示. 适用性: 访问一个聚合对象的内容而无需暴露它的内部表示. 支持对聚合对象的多种遍历. 为遍历不同的聚合结构提供一个 ...
- spring boot: 输出json
spring boot: 输出json 注意:关闭java的Terminate后,在重新启动,否则报错 app.java启动配置 package com.muyang.boot1; import o ...
- Python 实现图片上表格的写入
直接上代码:import matplotlib.pylab as pltimport numpy as npplt.figure()axes=plt.gca()y= np.random.randn(9 ...
- 识别User Agent屏蔽一些Web爬虫防采集
识别User Agent屏蔽一些Web爬虫防采集 from:https://jamesqi.com/%E5%8D%9A%E5%AE%A2/%E8%AF%86%E5%88%ABUser_Agent%E5 ...
- 052——VUE中使用vue-cli初始化单页面应用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 009PHP文件处理——文件处理 file_get_contents file_put_contents fgetc fgets fgetss
<?php /** * 文件处理 file_get_contents file_put_contents fgetc fgets fgetss */ //fgetc() 传入文件操作句柄.每次获 ...
- 007——php字符串中的处理函数(六)
<?php /** * 一.addslashes() 在预定义字符串前添加反斜杠 * * stripslashes() 把转义字符串前的反斜杠删除 * get_magic_quotes_gpc ...