我想给这段jQuery代码添加自动循环向上滚动效果,请高手帮忙,代码如下:
<!DOCTYPEHTML><HTML><title>jquery上下图片移动</title><HEAD><styletype="text/css">*{margin:0...
<!DOCTYPE HTML>
<HTML>
<title>jquery上下图片移动</title>
<HEAD>
<style type="text/css">
*{margin:0;padding:0;}
ul{list-style:none;position:relative;top:-170px;}
div{width:151px;margin:20px 0 0 50px;height:510px;overflow:hidden;position:relative;}
img{width:151px;border:0;}
a{font-size:12px;text-align:center;line-height:20px;}
</style>
</HEAD>
<BODY>
<button class="top">猛击向上</button>
<div>
<ul>
<li><a href="javascript:;"><img src="/uploads/common/images/wall_s1.jpg" />Webcam Hosting</a></li>
<li><a href="javascript:;"><img src="/uploads/common/images/wall_s2.jpg" />Sullivan Inc.</a></li>
<li><a href="javascript:;"><img src="/uploads/common/images/wall_s3.jpg" />Maui Stables</a></li>
<li><a href="javascript:;"><img src="/uploads/common/images/wall_s4.jpg" />Code Rebel 3.0</a></li>
<li><a href="javascript:;"><img src="/uploads/common/images/wall_s5.jpg" />SecurityPro Shop</a></li>
</ul>
</div>
<button class="bottom">猛击向下</botton>
<script type="text/javascript" src="jquery-1.3.1.js"></script>
<script type="text/javascript">
$(function(){
var $top=$("button.top");
var $bottom=$("button.bottom");
$top.click(function(){
$("ul").animate({marginTop:"-170px"},1000,function(){
$("ul li:eq(0)").appendTo($("ul"));
$("ul").css("marginTop","0px");
});
});
$bottom.click(function(){
$("ul").animate({marginTop:"170px"},1000,function(){
$("ul li:last").insertBefore($("ul li:first"));
$("ul").css("marginTop","0px");
});
});
})
</script>
</BODY>
<htML> 展开
<HTML>
<title>jquery上下图片移动</title>
<HEAD>
<style type="text/css">
*{margin:0;padding:0;}
ul{list-style:none;position:relative;top:-170px;}
div{width:151px;margin:20px 0 0 50px;height:510px;overflow:hidden;position:relative;}
img{width:151px;border:0;}
a{font-size:12px;text-align:center;line-height:20px;}
</style>
</HEAD>
<BODY>
<button class="top">猛击向上</button>
<div>
<ul>
<li><a href="javascript:;"><img src="/uploads/common/images/wall_s1.jpg" />Webcam Hosting</a></li>
<li><a href="javascript:;"><img src="/uploads/common/images/wall_s2.jpg" />Sullivan Inc.</a></li>
<li><a href="javascript:;"><img src="/uploads/common/images/wall_s3.jpg" />Maui Stables</a></li>
<li><a href="javascript:;"><img src="/uploads/common/images/wall_s4.jpg" />Code Rebel 3.0</a></li>
<li><a href="javascript:;"><img src="/uploads/common/images/wall_s5.jpg" />SecurityPro Shop</a></li>
</ul>
</div>
<button class="bottom">猛击向下</botton>
<script type="text/javascript" src="jquery-1.3.1.js"></script>
<script type="text/javascript">
$(function(){
var $top=$("button.top");
var $bottom=$("button.bottom");
$top.click(function(){
$("ul").animate({marginTop:"-170px"},1000,function(){
$("ul li:eq(0)").appendTo($("ul"));
$("ul").css("marginTop","0px");
});
});
$bottom.click(function(){
$("ul").animate({marginTop:"170px"},1000,function(){
$("ul li:last").insertBefore($("ul li:first"));
$("ul").css("marginTop","0px");
});
});
})
</script>
</BODY>
<htML> 展开
展开全部
给你一个函数
你调用下就OK了,支持加速滚动、循环滚动、间隔滚动、上下滚动、左右滚动等等
直接将这段代码拷贝到一个JS文件里面,这段代码需要jquery1.3以上运行环境。
代码里面有调用示例。
望采纳··
/**
* @classDescription 超级Marquee,可做图片导航,图片轮换
* @dependence jQuery 1.3.2
* @DOM
* <div id="marquee">
* <ul>
* <li></li>
* <li></li>
* </ul>
* </div>
* @CSS
* #marquee {width:200px;height:50px;overflow:hidden;}
* @Usage
* $('#marquee').superMarquee(options);
* @options
* distance:200,//一次滚动的距离
* duration:20,//缓动效果,单次移动时间,越小速度越快,为0时无缓动效果
* time:5,//停顿时间,单位为秒
* direction: 'left',//滚动方向,'left','right','up','down'
* scrollAmount:1,//步长
* scrollDelay:20//时长,单位为毫秒
* isEqual:true,//所有滚动的元素长宽是否相等,true,false
* loop: 0,//循环滚动次数,0时无限
* btnGo:{left:'#goL',right:'#goR'},//控制方向的按钮ID,有四个属性left,right,up,down分别对应四个方向
* eventGo:'click',//鼠标事件
* controlBtn:{left:'#goL',right:'#goR'},//控制加速滚动的按钮ID,有四个属性left,right,up,down分别对应四个方向
* newAmount:4,//加速滚动的步长
* eventA:'mouseenter',//鼠标事件,加速
* eventB:'mouseleave',//鼠标事件,原速
* navId:'#marqueeNav', //导航容器ID,导航DOM:<ul><li>1</li><li>2</li><ul>,导航CSS:.navOn
* eventNav:'click' //导航事件
*/
(function($){
$.fn.superMarquee = function(options){
var opts = $.extend({},$.fn.superMarquee.defaults, options);
return this.each(function(){
var $marquee = $(this);//滚动元素容器
var _scrollObj = $marquee.get(0);//滚动元素容器DOM
var scrollW = $marquee.width();//滚动元素容器的宽度
var scrollH = $marquee.height();//滚动元素容器的高度
var $element = $marquee.children(); //滚动元素
var $kids = $element.children();//滚动子元素
var scrollSize=0;//滚动元素尺寸
var _type = (opts.direction == 'left' || opts.direction == 'right') ? 1:0;//滚动类型,1左右,0上下
var scrollId, rollId, isMove, marqueeId;
var t,b,c,d,e; //滚动动画的参数,t:当前时间,b:开始的位置,c:改变的位置,d:持续的时间,e:结束的位置
var _size, _len; //子元素的尺寸与个数
var $nav,$navBtns;
var arrPos = [];
var numView = 0; //当前所看子元素
var numRoll=0; //轮换的次数
var numMoved = 0;//已经移动的距离
//防止滚动子元素比滚动元素宽而取不到实际滚动子元素宽度
$element.css(_type?'width':'height',10000);
//获取滚动元素的尺寸
var navHtml = '<ul>';
if (opts.isEqual) {
_size = $kids[_type?'outerWidth':'outerHeight']();
_len = $kids.length;
scrollSize = _size * _len;
for(var i=0;i<_len;i++){
arrPos.push(i*_size);
navHtml += '<li>'+ (i+1) +'</li>';
}
}else{
$kids.each(function(i){
arrPos.push(scrollSize);
scrollSize += $(this)[_type?'outerWidth':'outerHeight']();
navHtml += '<li>'+ (i+1) +'</li>';
});
}
navHtml += '</ul>';
//滚动元素总尺寸小于容器尺寸,不滚动
if (scrollSize<(_type?scrollW:scrollH)) return;
//克隆滚动子元素将其插入到滚动元素后,并设定滚动元素宽度
$element.append($kids.clone()).css(_type?'width':'height',scrollSize*2);
//轮换导航
if (opts.navId) {
$nav = $(opts.navId).append(navHtml).hover( stop, start );
$navBtns = $('li', $nav);
$navBtns.each(function(i){
$(this).bind(opts.eventNav,function(){
if(isMove) return;
if(numView==i) return;
rollFunc(arrPos[i]);
$navBtns.eq(numView).removeClass('navOn');
numView = i;
$(this).addClass('navOn');
});
});
$navBtns.eq(numView).addClass('navOn');
}
//设定初始位置
if (opts.direction == 'right' || opts.direction == 'down') {
_scrollObj[_type?'scrollLeft':'scrollTop'] = scrollSize;
}else{
_scrollObj[_type?'scrollLeft':'scrollTop'] = 0;
}
if(opts.isMarquee){
//滚动开始
//marqueeId = setInterval(scrollFunc, opts.scrollDelay);
marqueeId = setTimeout(scrollFunc, opts.scrollDelay);
//鼠标划过停止滚动
$marquee.hover(
function(){
clearInterval(marqueeId);
},
function(){
//marqueeId = setInterval(scrollFunc, opts.scrollDelay);
clearInterval(marqueeId);
marqueeId = setTimeout(scrollFunc, opts.scrollDelay);
}
);
//控制加速运动
if(opts.controlBtn){
$.each(opts.controlBtn, function(i,val){
$(val).bind(opts.eventA,function(){
opts.direction = i;
opts.oldAmount = opts.scrollAmount;
opts.scrollAmount = opts.newAmount;
}).bind(opts.eventB,function(){
opts.scrollAmount = opts.oldAmount;
});
});
}
}else{
if(opts.isAuto){
//轮换开始
start();
//鼠标划过停止轮换
$marquee.hover( stop, start );
}
//控制前后走
if(opts.btnGo){
$.each(opts.btnGo, function(i,val){
$(val).bind(opts.eventGo,function(){
if(isMove == true) return;
opts.direction = i;
rollFunc();
if (opts.isAuto) {
stop();
start();
}
});
});
}
}
function scrollFunc(){
var _dir = (opts.direction == 'left' || opts.direction == 'right') ? 'scrollLeft':'scrollTop';
if(opts.isMarquee){
if (opts.loop > 0) {
numMoved+=opts.scrollAmount;
if(numMoved>scrollSize*opts.loop){
_scrollObj[_dir] = 0;
return clearInterval(marqueeId);
}
}
var newPos = _scrollObj[_dir]+(opts.direction == 'left' || opts.direction == 'up'?1:-1)*opts.scrollAmount;
}else{
if(opts.duration){
if(t++<d){
isMove = true;
var newPos = Math.ceil(easeOutQuad(t,b,c,d));
if(t==d){
newPos = e;
}
}else{
newPos = e;
clearInterval(scrollId);
isMove = false;
return;
}
}else{
var newPos = e;
clearInterval(scrollId);
}
}
if(opts.direction == 'left' || opts.direction == 'up'){
if(newPos>=scrollSize){
newPos-=scrollSize;
}
}else{
if(newPos<=0){
newPos+=scrollSize;
}
}
_scrollObj[_dir] = newPos;
if(opts.isMarquee){
marqueeId = setTimeout(scrollFunc, opts.scrollDelay);
}else if(t<d){
if(scrollId) clearTimeout(scrollId);
scrollId = setTimeout(scrollFunc, opts.scrollDelay);
}else{
isMove = false;
}
};
function rollFunc(pPos){
isMove = true;
var _dir = (opts.direction == 'left' || opts.direction == 'right') ? 'scrollLeft':'scrollTop';
var _neg = opts.direction == 'left' || opts.direction == 'up'?1:-1;
numRoll = numRoll +_neg;
//得到当前所看元素序号并改变导航CSS
if(pPos == undefined&&opts.navId){
$navBtns.eq(numView).removeClass('navOn');
numView +=_neg;
if(numView>=_len){
numView = 0;
}else if(numView<0){
numView = _len-1;
}
$navBtns.eq(numView).addClass('navOn');
numRoll = numView;
}
var _temp = numRoll<0?scrollSize:0;
t=0;
b=_scrollObj[_dir];
//c=(pPos != undefined)?pPos:_neg*opts.distance;
e=(pPos != undefined)?pPos:_temp+(opts.distance*numRoll)%scrollSize;
if(_neg==1){
if(e>b){
c = e-b;
}else{
c = e+scrollSize -b;
}
}else{
if(e>b){
c =e-scrollSize-b;
}else{
c = e-b;
}
}
d=opts.duration;
//scrollId = setInterval(scrollFunc, opts.scrollDelay);
if(scrollId) clearTimeout(scrollId);
scrollId = setTimeout(scrollFunc, opts.scrollDelay);
}
function start(){
rollId = setInterval(function(){
rollFunc();
}, opts.time*1000);
}
function stop(){
clearInterval(rollId);
}
function easeOutQuad(t,b,c,d){
return -c *(t/=d)*(t-2) + b;
}
function easeOutQuint(t,b,c,d){
return c*((t=t/d-1)*t*t*t*t + 1) + b;
}
});
};
$.fn.superMarquee.defaults = {
isMarquee:false,//是否为Marquee
isEqual:true,//所有滚动的元素长宽是否相等,true,false
loop: 0,//循环滚动次数,0时无限
newAmount:3,//加速滚动的步长
eventA:'mousedown',//鼠标事件,加速
eventB:'mouseup',//鼠标事件,原速
isAuto:true,//是否自动轮换
time:5,//停顿时间,单位为秒
duration:50,//缓动效果,单次移动时间,越小速度越快,为0时无缓动效果
eventGo:'click', //鼠标事件,向前向后走
direction: 'left',//滚动方向,'left','right','up','down'
scrollAmount:1,//步长
scrollDelay:10,//时长
eventNav:'click'//导航事件
};
$.fn.superMarquee.setDefaults = function(settings) {
$.extend( $.fn.superMarquee.defaults, settings );
};
})(jQuery);
你调用下就OK了,支持加速滚动、循环滚动、间隔滚动、上下滚动、左右滚动等等
直接将这段代码拷贝到一个JS文件里面,这段代码需要jquery1.3以上运行环境。
代码里面有调用示例。
望采纳··
/**
* @classDescription 超级Marquee,可做图片导航,图片轮换
* @dependence jQuery 1.3.2
* @DOM
* <div id="marquee">
* <ul>
* <li></li>
* <li></li>
* </ul>
* </div>
* @CSS
* #marquee {width:200px;height:50px;overflow:hidden;}
* @Usage
* $('#marquee').superMarquee(options);
* @options
* distance:200,//一次滚动的距离
* duration:20,//缓动效果,单次移动时间,越小速度越快,为0时无缓动效果
* time:5,//停顿时间,单位为秒
* direction: 'left',//滚动方向,'left','right','up','down'
* scrollAmount:1,//步长
* scrollDelay:20//时长,单位为毫秒
* isEqual:true,//所有滚动的元素长宽是否相等,true,false
* loop: 0,//循环滚动次数,0时无限
* btnGo:{left:'#goL',right:'#goR'},//控制方向的按钮ID,有四个属性left,right,up,down分别对应四个方向
* eventGo:'click',//鼠标事件
* controlBtn:{left:'#goL',right:'#goR'},//控制加速滚动的按钮ID,有四个属性left,right,up,down分别对应四个方向
* newAmount:4,//加速滚动的步长
* eventA:'mouseenter',//鼠标事件,加速
* eventB:'mouseleave',//鼠标事件,原速
* navId:'#marqueeNav', //导航容器ID,导航DOM:<ul><li>1</li><li>2</li><ul>,导航CSS:.navOn
* eventNav:'click' //导航事件
*/
(function($){
$.fn.superMarquee = function(options){
var opts = $.extend({},$.fn.superMarquee.defaults, options);
return this.each(function(){
var $marquee = $(this);//滚动元素容器
var _scrollObj = $marquee.get(0);//滚动元素容器DOM
var scrollW = $marquee.width();//滚动元素容器的宽度
var scrollH = $marquee.height();//滚动元素容器的高度
var $element = $marquee.children(); //滚动元素
var $kids = $element.children();//滚动子元素
var scrollSize=0;//滚动元素尺寸
var _type = (opts.direction == 'left' || opts.direction == 'right') ? 1:0;//滚动类型,1左右,0上下
var scrollId, rollId, isMove, marqueeId;
var t,b,c,d,e; //滚动动画的参数,t:当前时间,b:开始的位置,c:改变的位置,d:持续的时间,e:结束的位置
var _size, _len; //子元素的尺寸与个数
var $nav,$navBtns;
var arrPos = [];
var numView = 0; //当前所看子元素
var numRoll=0; //轮换的次数
var numMoved = 0;//已经移动的距离
//防止滚动子元素比滚动元素宽而取不到实际滚动子元素宽度
$element.css(_type?'width':'height',10000);
//获取滚动元素的尺寸
var navHtml = '<ul>';
if (opts.isEqual) {
_size = $kids[_type?'outerWidth':'outerHeight']();
_len = $kids.length;
scrollSize = _size * _len;
for(var i=0;i<_len;i++){
arrPos.push(i*_size);
navHtml += '<li>'+ (i+1) +'</li>';
}
}else{
$kids.each(function(i){
arrPos.push(scrollSize);
scrollSize += $(this)[_type?'outerWidth':'outerHeight']();
navHtml += '<li>'+ (i+1) +'</li>';
});
}
navHtml += '</ul>';
//滚动元素总尺寸小于容器尺寸,不滚动
if (scrollSize<(_type?scrollW:scrollH)) return;
//克隆滚动子元素将其插入到滚动元素后,并设定滚动元素宽度
$element.append($kids.clone()).css(_type?'width':'height',scrollSize*2);
//轮换导航
if (opts.navId) {
$nav = $(opts.navId).append(navHtml).hover( stop, start );
$navBtns = $('li', $nav);
$navBtns.each(function(i){
$(this).bind(opts.eventNav,function(){
if(isMove) return;
if(numView==i) return;
rollFunc(arrPos[i]);
$navBtns.eq(numView).removeClass('navOn');
numView = i;
$(this).addClass('navOn');
});
});
$navBtns.eq(numView).addClass('navOn');
}
//设定初始位置
if (opts.direction == 'right' || opts.direction == 'down') {
_scrollObj[_type?'scrollLeft':'scrollTop'] = scrollSize;
}else{
_scrollObj[_type?'scrollLeft':'scrollTop'] = 0;
}
if(opts.isMarquee){
//滚动开始
//marqueeId = setInterval(scrollFunc, opts.scrollDelay);
marqueeId = setTimeout(scrollFunc, opts.scrollDelay);
//鼠标划过停止滚动
$marquee.hover(
function(){
clearInterval(marqueeId);
},
function(){
//marqueeId = setInterval(scrollFunc, opts.scrollDelay);
clearInterval(marqueeId);
marqueeId = setTimeout(scrollFunc, opts.scrollDelay);
}
);
//控制加速运动
if(opts.controlBtn){
$.each(opts.controlBtn, function(i,val){
$(val).bind(opts.eventA,function(){
opts.direction = i;
opts.oldAmount = opts.scrollAmount;
opts.scrollAmount = opts.newAmount;
}).bind(opts.eventB,function(){
opts.scrollAmount = opts.oldAmount;
});
});
}
}else{
if(opts.isAuto){
//轮换开始
start();
//鼠标划过停止轮换
$marquee.hover( stop, start );
}
//控制前后走
if(opts.btnGo){
$.each(opts.btnGo, function(i,val){
$(val).bind(opts.eventGo,function(){
if(isMove == true) return;
opts.direction = i;
rollFunc();
if (opts.isAuto) {
stop();
start();
}
});
});
}
}
function scrollFunc(){
var _dir = (opts.direction == 'left' || opts.direction == 'right') ? 'scrollLeft':'scrollTop';
if(opts.isMarquee){
if (opts.loop > 0) {
numMoved+=opts.scrollAmount;
if(numMoved>scrollSize*opts.loop){
_scrollObj[_dir] = 0;
return clearInterval(marqueeId);
}
}
var newPos = _scrollObj[_dir]+(opts.direction == 'left' || opts.direction == 'up'?1:-1)*opts.scrollAmount;
}else{
if(opts.duration){
if(t++<d){
isMove = true;
var newPos = Math.ceil(easeOutQuad(t,b,c,d));
if(t==d){
newPos = e;
}
}else{
newPos = e;
clearInterval(scrollId);
isMove = false;
return;
}
}else{
var newPos = e;
clearInterval(scrollId);
}
}
if(opts.direction == 'left' || opts.direction == 'up'){
if(newPos>=scrollSize){
newPos-=scrollSize;
}
}else{
if(newPos<=0){
newPos+=scrollSize;
}
}
_scrollObj[_dir] = newPos;
if(opts.isMarquee){
marqueeId = setTimeout(scrollFunc, opts.scrollDelay);
}else if(t<d){
if(scrollId) clearTimeout(scrollId);
scrollId = setTimeout(scrollFunc, opts.scrollDelay);
}else{
isMove = false;
}
};
function rollFunc(pPos){
isMove = true;
var _dir = (opts.direction == 'left' || opts.direction == 'right') ? 'scrollLeft':'scrollTop';
var _neg = opts.direction == 'left' || opts.direction == 'up'?1:-1;
numRoll = numRoll +_neg;
//得到当前所看元素序号并改变导航CSS
if(pPos == undefined&&opts.navId){
$navBtns.eq(numView).removeClass('navOn');
numView +=_neg;
if(numView>=_len){
numView = 0;
}else if(numView<0){
numView = _len-1;
}
$navBtns.eq(numView).addClass('navOn');
numRoll = numView;
}
var _temp = numRoll<0?scrollSize:0;
t=0;
b=_scrollObj[_dir];
//c=(pPos != undefined)?pPos:_neg*opts.distance;
e=(pPos != undefined)?pPos:_temp+(opts.distance*numRoll)%scrollSize;
if(_neg==1){
if(e>b){
c = e-b;
}else{
c = e+scrollSize -b;
}
}else{
if(e>b){
c =e-scrollSize-b;
}else{
c = e-b;
}
}
d=opts.duration;
//scrollId = setInterval(scrollFunc, opts.scrollDelay);
if(scrollId) clearTimeout(scrollId);
scrollId = setTimeout(scrollFunc, opts.scrollDelay);
}
function start(){
rollId = setInterval(function(){
rollFunc();
}, opts.time*1000);
}
function stop(){
clearInterval(rollId);
}
function easeOutQuad(t,b,c,d){
return -c *(t/=d)*(t-2) + b;
}
function easeOutQuint(t,b,c,d){
return c*((t=t/d-1)*t*t*t*t + 1) + b;
}
});
};
$.fn.superMarquee.defaults = {
isMarquee:false,//是否为Marquee
isEqual:true,//所有滚动的元素长宽是否相等,true,false
loop: 0,//循环滚动次数,0时无限
newAmount:3,//加速滚动的步长
eventA:'mousedown',//鼠标事件,加速
eventB:'mouseup',//鼠标事件,原速
isAuto:true,//是否自动轮换
time:5,//停顿时间,单位为秒
duration:50,//缓动效果,单次移动时间,越小速度越快,为0时无缓动效果
eventGo:'click', //鼠标事件,向前向后走
direction: 'left',//滚动方向,'left','right','up','down'
scrollAmount:1,//步长
scrollDelay:10,//时长
eventNav:'click'//导航事件
};
$.fn.superMarquee.setDefaults = function(settings) {
$.extend( $.fn.superMarquee.defaults, settings );
};
})(jQuery);
展开全部
你需要控制的是给div加个高度
<div style="height: 170px;">
<ul>
......
</div>
然后简单实现你要的效果:
$bottom.click(function(){
down();
});
function down(){
$("ul").animate({marginTop: 340},3500,function(){
$("ul").css("marginTop","0px");
down.call();
});
}
具体的数值你自己控制好就行了。
<div style="height: 170px;">
<ul>
......
</div>
然后简单实现你要的效果:
$bottom.click(function(){
down();
});
function down(){
$("ul").animate({marginTop: 340},3500,function(){
$("ul").css("marginTop","0px");
down.call();
});
}
具体的数值你自己控制好就行了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在onload事件中调用button的方法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询