js实例教程-jQuery图片滚动图片的效果(另类实现)

发布时间:2018-12-04 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了js实例教程-jQuery图片滚动图片的效果(另类实现)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

需求 : 图片切换的时候下一屏不允许出现空白的项,换句话说就是 :

  1、当移动的最后一屏移动的个数小于要展示的个数的时候 ,只移动(展示个数-最后一屏的个数的)差值。 举个例子: 每一屏都要展示7个,但总个数才10个,滚动到下一屏时候用户看到的还是7个,这个时候需要移动的是三个

这个效果是基于jQuery写的,只是想纪念下自己的学习 话不多说了,贴代码

 

. 代码如下:


(function( $ ){
VAR slider = function( elem , args ){
this.config = $.extend({
effect : 'x', //效果 水平 - x
sPEed : 600 , //动画速度
trigger : ';mouseenter', //触发事件
callback : null , // 回调函数
view : 7
}, args || {} );

this.history = [];//记录移动的@R_512_1788@
this.index = 0;
this.el = elem;
this.length = this.el.find('li').length;//记录总长度
this.width = this.el.find('li').eq(0).outerWidth();//记录每一个单项的
this.inIT();
}
slider.PRototype = {
constructor : slider,
init : function(){
this.bindEvents();
},
bindEvents : function(){
this.prev();
this.next();
},
prev : function(){
this.el.find('[data-type="left"]').click( $.Proxy(function(){

if( !this.history.length ) return;//如果记录为空,证明没有进行移动过,所以直接return

this.index--;
var step = this.history.pop();//取最后的移动步骤
var move = step * this.width;//算出移动宽度
this.el.find("ul").aniMATE( { "left" : "+="+move+"px" } , this.config.speed )

} , this));
},
next : function(){
this.el.find('[data-type="right"]').click( $.proxy(function(){
//如果是当前的最后一页,直接return
if(this.index == parseint( this.length / this.config.view , 10 ) ){
return;
}
this.index++;
//这个计算很重要
//计算 ( 下一页 * view ) 展示个数是否大于总长度 : 好比当前在第一页 (1+1) *7 > 12(总长度)
//则this.step 赋值为取余 ,也就是剩下要移动的个数
if( this.config.view * (this.index+1) > this.length ){
this.step = this.length%this.config.view;
}else{
//否则移动展示的个数
this.step = this.config.view;
}
//记录移动的历史记录
this.history.push(this.step);
var move = -1 * this.step * this.width;
this.el.find("ul").animate( { "left" : "+="+move+"px" } , this.config.speed )

} , this));
}
}

$.fn.slider = function( args ){
return this.each(function(){
var el = this;
var plugins = new slider( $( el ) , args );
$(el).data("slider" , plugins );
});
}
})(jquery)

 

开始对这个实现没有好的想法,本来想利用一个变量记录当前的移动个数的,但是后面突然想到用数组来做这样子的处理,顿时感觉清晰了。

这个的实现重点是一个记录移动步骤的数组。向左移动的时候往数组里面push移动的步骤,向右移动的时候,从数组里面取最后一项 [].pop()。

需求 : 图片切换的时候下一屏不允许出现空白的项,换句话说就是 :

  1、当移动的最后一屏移动的个数小于要展示的个数的时候 ,只移动(展示个数-最后一屏的个数的)差值。 举个例子: 每一屏都要展示7个,但总个数才10个,滚动到下一屏时候用户看到的还是7个,这个时候需要移动的是三个

这个效果是基于jQuery写的,只是想纪念下自己的学习 话不多说了,贴代码

 

. 代码如下:


(function( $ ){
var slider = function( elem , args ){
this.config = $.extend({
effect : 'x', //效果 水平 - x
speed : 600 , //动画速度
trigger : 'mouseenter', //触发事件
callback : null , // 回调函数
view : 7
}, args || {} );

this.history = [];//记录移动的历史记录
this.index = 0;
this.el = elem;
this.length = this.el.find('li').length;//记录总长度
this.width = this.el.find('li').eq(0).outerWidth();//记录每一个单项的宽度
this.init();
}
slider.prototype = {
constructor : slider,
init : function(){
this.bindEvents();
},
bindEvents : function(){
this.prev();
this.next();
},
prev : function(){
this.el.find('[data-type="left"]').click( $.proxy(function(){

if( !this.history.length ) return;//如果记录为空,证明没有进行移动过,所以直接return

this.index--;
var step = this.history.pop();//取最后的移动步骤
var move = step * this.width;//算出移动宽度
this.el.find("ul").animate( { "left" : "+="+move+"px" } , this.config.speed )

} , this));
},
next : function(){
this.el.find('[data-type="right"]').click( $.proxy(function(){
//如果是当前的最后一页,直接return
if(this.index == parseInt( this.length / this.config.view , 10 ) ){
return;
}
this.index++;
//这个计算很重要
//计算 ( 下一页 * view ) 展示个数是否大于总长度 : 好比当前在第一页 (1+1) *7 > 12(总长度)
//则this.step 赋值为取余 ,也就是剩下要移动的个数
if( this.config.view * (this.index+1) > this.length ){
this.step = this.length%this.config.view;
}else{
//否则移动展示的个数
this.step = this.config.view;
}
//记录移动的历史记录
this.history.push(this.step);
var move = -1 * this.step * this.width;
this.el.find("ul").animate( { "left" : "+="+move+"px" } , this.config.speed )

} , this));
}
}

$.fn.slider = function( args ){
return this.each(function(){
var el = this;
var plugins = new slider( $( el ) , args );
$(el).data("slider" , plugins );
});
}
})(jQuery)

 

开始对这个实现没有好的想法,本来想利用一个变量记录当前的移动个数的,但是后面突然想到用数组来做这样子的处理,顿时感觉清晰了。

这个的实现重点是一个记录移动步骤的数组。向左移动的时候往数组里面push移动的步骤,向右移动的时候,从数组里面取最后一项 [].pop()。

觉得可用,就经常来吧!Javascript技巧 脚本宝典 欢迎评论哦! js技巧,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的js实例教程-jQuery图片滚动图片的效果(另类实现)全部内容,希望文章能够帮你解决js实例教程-jQuery图片滚动图片的效果(另类实现)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。