jQuery实现滑动效果

<html>
<head>
<script type="text/javascript" src="jquery-1.8.2.js"></script>
<script type="text/javascript" >
$(function(){
//切换速度
var swithSpeed = 500;
//每页宽度
var pageWidth = $('#container').css('width');
$('#content table tr td').css('width',pageWidth);
//设置高度
$('#content table tr td').css('height',$('#container').css('height'));
$('#content').css('width',$('#container').css('height'));
//去掉px字符,转换成int类型
var pageWidthNum=parseInt(pageWidth.substring(0,pageWidth.length-2));
//content 宽度
$('#content').css('width',$('#content table tr td').length*pageWidthNum);
//第一页和最后一页交换位置
$('table td:first').before($('table td:last'));
//面板初始化left为-pageWidth(一页的宽度),即先向左窜一页
$('#content').css('left','-'+pageWidth);
//按钮事件
$('#prev').click(function(){
//原始left
var orLeft = $('#content').css('left');
//目标left
var newLeft = parseInt(orLeft.substring(0,orLeft.length-2))-pageWidthNum;
if(!$('#content').is(":animated")){//如果未处在执行动画的状态
$("#content").animate({left:newLeft+'px'},swithSpeed,function(){
//前后页位置调换
$('table td:last').after($('table td:first'));
//left位置还原
$("#content").css("left",newLeft+pageWidthNum+'px');
});
}
});
$('#next').click(function(){
//原始left
var orLeft = $('#content').css('left');
//目标left
var newLeft = parseInt(orLeft.substring(0,orLeft.length-2))+pageWidthNum;
if(!$('#content').is(":animated")){//如果未处在执行动画的状态
$("#content").animate({left:newLeft+'px'},swithSpeed,function(){
//前后页位置调换
$('table td:first').before($('table td:last'));
//left位置还原
$("#content").css("left",newLeft-pageWidthNum+'px');
});
}
});
//定时切换
//var interval = setInterval(function(){
// $('#prev').click();
//},3000) 
//clearInterval(interval) //清除定时
});
</script>
<style type = "text/css">
#container{
background-color:#278382; 
height:500px;
width:400px;
overflow:hidden;
}
#content{
position:absolute;
}
</style>
</head>
<body>
<a id = "prev" href = "javascript:void();">前</a>
<a id = "next" href = "javascript:void();">后</a>
<div id = "container" style = "">
<div id = "content">
<table  border="0" cellspacing="0" cellpadding="0">
<tr>
<td class = "page" style = "background-color:#287382">
<div style= "width:100%;height:100%;">这里是第一页的内容</div>
</td>
<td class = "page" style = "background-color:#233382">
<div style = "width:100%;height:100%">
q
</div>
</td>
<td class = "page" style = "background-color:#287111">3</td>
<td class = "page" style = "background-color:#2aaa82">4</td>
<td class = "page" style = "background-color:#244482">5</td>
<td class = "page" style = "background-color:#244482">6</td>
<td class = "page" style = "background-color:#244482">7</td>
<td class = "page" style = "background-color:#244482">8</td>
</tr>
</table>
</div>
</div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值