• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
丶chunchun
博客园    首页    新随笔    联系   管理    订阅  订阅

前端实现左右翻页功能Demo

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
section {
width: 600px;
height: 400px;
margin: auto;
overflow: hidden;
}
.container {
width: 2400px;
height: 100%;
/* 去掉子元素中的间隙 */
background-color: aquamarine;
font-size: 0;
}
.container div {
display: inline-block;
width: 600px;
height: calc(100% - 60px);
font-size: 20px;
}
.btn_next,
.btn_pre {
width: 150px;
height: 40px;
line-height: 40px;
text-align: center;
border-radius: 5px;
border: 1px solid yellowgreen;
margin-bottom: 20px;
cursor: pointer;
}
</style>
</head>
<body>
<section>
<div class="btn_next">请点击我向后翻页</div>
<div class="btn_pre">请点击我向前翻页</div>
<div class="container">
<div>元素一</div>
<div>元素二</div>
<div>元素三</div>
<div>元素四</div>
</div>
</section>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script>
let margin = 0;
$('.btn_next').on('click',function() {
handleClickNext();
})
$('.btn_pre').on('click',function() {
handleClickPre();
})
 
function handleClickNext() {
if(margin >= -1200) {
margin = margin - 600;
$('.container').css({
transform: 'translateX('+ margin +'px)',
'transition': 'transform 1s'
})
} else {
alert('已经是最后一页了')
}
}
function handleClickPre() {
if(margin <= -600) {
margin = margin + 600;
$('.container').css({
transform: 'translateX('+ margin +'px)',
'transition': 'transform 1s'
})
} else {
alert('已经是第一页了')
}
}
</script>
</body>
</html>
posted @ 2020-12-25 15:08  丶chunchun  阅读(1141)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3