jq插件第二款来袭 图片滚动
这第二款也是非常实用的插件,也是与图片相关,关于图片的需求太多了,这个是图片滚动哦,不过不是无缝滚动,是左像右滚动,到头的话再往回滚动,利用scrollLeft实现的,支持自动滚动和每次滚动的个数默认为1张,另外提供了连个外接方法, 停止自动滚动$.fn.roll.stop,继续自动滚动$.fn.roll.auto,不多说,上菜!
JQ插件代码
(function($){
$.fn.roll = function(options){
if($(this).length == 0) return false;
var opts = $.extend({},$.fn.roll.defaults,options); function Rolling(o){
this.oParent = o;
this.imgList = o.find(opts['imgList']);
this.rollEle = o.find(opts['rollEle']);
this.rollEleParent = o.find(opts['rollEleParent']);
this.bl = o.find(opts['btn-left']);
this.br = o.find(opts['btn-right']);
this.auto = opts['automatic'];
this.speed = opts['speed'];
this.num = opts['num'];
this.stop = opts['stop'];
this.onOff = true;
this.leftValue = 0;
this.len = 0;
this.index = 0;
this.timer = null;
this.singleW = 0;
this.preIndex = -1;
this.nowIndex = 0; } Rolling.prototype = {
init:function(){
var _this = this;
this.len = this.rollEle.length;
this.singleWidth();
this.rollEleParent.width(this.singleW*this.len);
this.bl.on('click',function(){
_this.stopRoll();
_this.fnRoll(0);
return false;
});
this.br.on('click',function(){
_this.stopRoll();
_this.fnRoll(1);
return false;
});
if(this.auto){
this.timer = setInterval(function(){
_this.automatic();
},this.speed[0]); this.oParent.hover(function(){
_this.stopRoll();
},function(){
_this.timer = setInterval(function(){
_this.automatic();
},_this.speed[0])
})
}
},
singleWidth:function(){
this.singleW = this.rollEle.eq(0).outerWidth(true);
}, fnRoll:function(dir){ //dir == 1,→→滚, dir==0,←←滚
var _this = this;
this.onOff = false;
this.preIndex = this.index;
var scrolling = dir ? {scrollLeft:_this.singleW*this.num + _this.singleW*_this.index}:{scrollLeft:_this.leftValue - _this.singleW*this.num} this.imgList.animate(scrolling,_this.speed[1],function(){
_this.onOff = true;
if(dir){
if(_this.index < _this.len){
_this.index += _this.num;
_this.nowIndex = _this.index;
}else{
_this.index = _this.len; } }else{
if(_this.index > 0){
_this.index -= _this.num;
_this.nowIndex = _this.index
}else{
_this.index = 0; }
_this.nowIndex = _this.index;
} _this.leftValue = $(this).scrollLeft();
if(this.nowIndex == 0){ //当前索引为0是,则应该向右滚动,重置preIndex值(当前为0)
this.preIndex = -1;
} }) }, stopRoll:function(){
clearInterval(this.timer)
},
automatic:function(){
if(this.leftValue >= this.rollEleParent.width() - this.imgList.width() + parseFloat(this.rollEleParent.css('margin-left'))){
//判断 滚动容器是否滚到头,若到头了,则往回滚
this.fnRoll(0)
} if(this.preIndex < this.nowIndex){ //判断上一次与当前索引的关系,若大于,则是向有滚动,若小于或等于则向左滚动
this.fnRoll(1)
return;
} if(this.preIndex >= this.nowIndex ){ this.fnRoll(0)
} if(this.nowIndex == 0){ //当前索引为0是,则应该向右滚动,重置preIndex值(当前为0)
this.preIndex = -1;
} } } return this.each(function(){
var $this = $(this);
var obj = new Rolling($this);
obj.init();
$.fn.roll.stop = function(){
obj.stopRoll();
} //曝露停止自动滚动方法,共外部条用
$.fn.roll.auto = function(){
obj.stopRoll();
obj.timer = setInterval(function(){
obj.automatic();
},obj.speed[0]);
} //曝露继续自动滚动方法,共外部条用 })
}; $.fn.roll.defaults = {
'imgList' : '.sildeImgList', //滚动容器
'rollEle' : 'li', //滚动元素
'rollEleParent' : 'ul', //滚动元素父级
'btn-left': '.btn-left', //button←←
'btn-right': '.btn-right', //button→→
'automatic': false, //是否自动滚动
'speed' : [3000,300], //间隔,速度
'num' : 1, //一次滚动的数量
}; })(jQuery);
html
<div class="sildeImgBox">
<span class="btn-left"></span>
<div class="sildeImgList">
<ul>
<li><img src="data:images/mm1.jpg" alt=""></li>
<li><img src="data:images/mm2.jpg" alt=""></li>
<li><img src="data:images/mm3.jpg" alt=""></li>
<li><img src="data:images/mm1.jpg" alt=""></li>
<li><img src="data:images/mm2.jpg" alt=""></li>
<li><img src="data:images/mm3.jpg" alt=""></li>
<li><img src="data:images/mm1.jpg" alt=""></li>
<li><img src="data:images/mm2.jpg" alt=""></li>
</ul>
</div>
<span class="btn-right"></span>
</div>
调用
<script> $(function(){
$('.sildeImgBox').eq(0).roll({'automatic' : true})
}) </script>
css
.sildeImgBox { width: 940px; margin: 0 auto; height: 344px; }
.sildeImgBox .btn-left { background-position: 0 0; left:; }
.sildeImgBox .btn-right { background-position: 100% 0; right:; }
.sildeImgBox .sildeImgList { width: 820px; height: 349px; overflow: hidden; }
.sildeImgBox .sildeImgList ul { left:; right:; margin-left: -15px; width: 5000px; }
.sildeImgBox .sildeImgList li { display: inline; float: left; width: 262px; height: 344px; margin-left: 15px; box-shadow: 2px 2px 5px rgba(4, 0, 0, 0.75); }
.sildeImgBox .btn-left, .sildeImgBox .btn-right .sildeImgBox .sildeImgList ul { position: absolute; }
.sildeImgBox .btn-left, .sildeImgBox .btn-right { display: block; }
.sildeImgBox, .sildeImgBox .sildeImgList { position: relative; }
jq插件第二款来袭 图片滚动的更多相关文章
- jQuery插件实例三:图片滚动[切换]效果一
图片切换效果在很多网站上都能看到,是一种常见的广告/活动宣传方式,通常位于网页上端.这个插件是众多图片切换效果的形式中的一种,数据源可在前端配置,也可从后台通JSON格式传输数据,当然,数据格式是固定 ...
- 【精心推荐】12款很好用的 jQuery 图片滚动插件
这里收集了12款很好用的 jQuery 图片滚动插件分享给大家.jQuery 作为最流行的 JavaScript 框架,使用简单灵活,同时还有许多优秀的插件可供使用.其中最令人印象深刻的应用之一就是各 ...
- 10款很好用的 jQuery 图片滚动插件
jQuery 作为最流行的 JavaScript 框架,使用简单灵活,同时还有许多优秀的插件可供使用.其中最令人印象深刻的应用之一就是各种很酷的图片效果,它可以让的网站更具吸引力.这里收集了10款很好 ...
- 介绍一款网站前台图片滚动插件之"switchable"
一.简单介绍:jQuery.Switchable是一款整合了Tabs.Slide.Scrollable等常见UI组件的jQuery插件,在这里,简答说说他的Slide.像Tabs,在Jquery-UI ...
- jq插件处女座 图片轮播
好久没写博客了,变得好懒呀,无地自容.最近一直在学sass和jq插件的写法,照猫画虎的谢了一个jq的插件,也算是第一次真正称得上插件的插件 ,废话不多说 上代码 (function($) { $.fn ...
- 分享一款页面视差滚动切换jquery.localscroll插件
今天给大家分享一款页面视差滚动切换jquery.localscroll插件. 滚动鼠标液动条看下页面的切换效果.该插件适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera ...
- JQ插件之imgAreaSelect实现对图片的在线截图功能(java版)
前言:在做网站的时候经常用的功能就是,用户上传图片对自己上传的图片进行截图,DIV自己的头像.或者上传幻灯片大图进行DIV设置小图. 解决方案:目前我知道的解决方案有两个如下: 一.fla ...
- 个人学习JQ插件编写成果:little酷炫的图片滑动切换效果
工作一个多月了,好久没来冒冒泡了,看了@wayong的JQ插件教程,自己编写了一个模仿拉勾网首页广告栏滑动特效的JQ插件,现在跟朋友们分享分享! 先上demo链接:http://runjs.cn/de ...
- 一个简单而实用的JQ插件——lazyload.js图片延迟加载插件
前 言 Cherish 看多了炫酷的插件之后再来看这么一个小清新的东西,是不是突然感觉JQ插件感觉很友好了,简单强大最重要的是实用. 这篇文章将详细讲解一下lazyload.js的用法 lazy ...
随机推荐
- Oracle创建主键自增表
Oracle创建主键自增表 1.创建表 create table Test_Increase( userid number(10) NOT NULL primary k ...
- Firebug介绍及使用技巧
一.介绍 Firebug是网页浏览器Firefox下的一款开发调试工具.安装firebug后在浏览器的插件工具栏中(上方)会有一个小甲虫的图标. F12打开和关闭Firebug窗口. 二.FireBu ...
- <正见>摘抄
1- 没有全能的力量能够扭转死亡之路,因此也就不会困在期待之中.如果没有盲目的期待,就不会有失望,如果能够了解一切都是无常,就不会攀缘执著.如果不攀缘执著,就不会患得患失,也才能真正完完全全地活着. ...
- collections——高性能容器数据类型
由于最近对机器学习算法感兴趣,一直知道python有一个包collections封装了一些比dict,list之类高级点的类,所以抽空研究下,为接下来的工作准备. 主要参考是https://docs. ...
- Unix 环境高级编程 (APUE) 之 网络 IPC:套接字
一起学 Unix 环境高级编程 (APUE) 之 网络 IPC:套接字 . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级 ...
- 400 bad request
这是查到的Http400状态码的定义 400 错误请求 - 请求中有语法问题,或不能满足请求. HTTP 400 - 请求无效. 表单传入的参数 userNa ...
- 世纪大争论:Linux还是GNU/Linux?
我们在网上已经习惯用“Linux”来称呼Linux操作系统了,然而,偶尔也用“GNU/Linux”来称呼和指代同样的操作系统和软件.同时人们也在争论这两种称呼哪个更合适. 本文将不会选边站队,仅力图向 ...
- C# 语法技巧_三目运算_switch_case
一.三目运算符 三目运算符的一种简便写法: bool b = str == "abc" ? false : true; 当是自身时,实际上别吝啬那一个括号,有一个括号,实际上更容易 ...
- BufferedStream类 - 缓冲流
BufferedStream常用于对其他流的一个封装,它必须和其他流结合一起使用.MemoryStream将所有的内容都放入内存中,而BufferedStream不是.BufferedStream在基 ...
- Silverlight 结合ArcGis 在地图画面上显示名称+ 点选图层事件委派
原文 http://www.dotblogs.com.tw/justforgood/archive/2012/05/10/72083.aspx 如下图,我希望我的滑鼠经过此标记的点时显示名称 其实简单 ...