<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
*{
margin: 0;
padding: 0;
}
.data{
float: left;
width: 150px;
margin-left: 20px;
}
img{
width: 150px;
height: 150px;
}
.jia,.hua{
display: inline-block;
}
.jia{
color: red;
}
.more{
width: 100px;
height: 50px;
margin: 20px 125px;
}
#all{
width: 350px;
height: 100%;
}
# box{
width: 350px;
}
.hua{
text-decoration: line-through;
}
#all{
width: 305px;
margin: 0 auto;
}
.top{
position: fixed;
top: 350px;
right: 0;
width: 35px;
height: 35px;
background: red;
color: white;
border: 0;
outline: 0;
}
</style>
<script src="jquery-1.8.3.js"></script>
</head>
<body>
<div id="all">
<div id="box">
<!--<div class="data">
<img src="img/1.jpg" alt=""/>
<div class="mai">买一送一特价大放送</div>
<div class="bao">包邮</div>
<div class="jia">¥<span>2</span></div><div class="hua">原价 ¥<span>20</span></div>
</div>-->
</div>
<button class="top">回到顶部</button>
<button class="more">加载更多</button>
</div>
</body>
</html>
<script>
$(".top").hide();
$.ajax({
url: "data.json",
type:"get",
success: function(data){
var num=0;
var seed=6;//滚动条数
function moreTwo(){
var html="";
for(var k=num;k<num+seed&&k<data.length;k++){
html+="<div class='data'>"+
"<img src='"+data[k].imgs+"' alt=''/>"+
"<div class='mai'>"+data[k].title+"</div>"+
"<div class='bao'>包邮</div>"+
"<div class='jia'>¥<span>2</span></div><div class='hua'>原价 ¥<span>20</span></div>"+
"</div>";
//判断是否还有数据
if((k+1)>=data.length){
$(".more").html("我是有底线的");
}
}
$("# box").append(html);
}//moveTwo
//点击加载
$(".more").click(function(){
num+=seed;
moreTwo();
});
moreTwo();
//回到顶部
$(".top").click(function(){
$('body,html').animate({scrollTop:0},500);
});
//距离加载数据
$(window).scroll(function(){
var heightAll=$(document).height(); //获取页面长度
var lookAll=$(window).height();//获取可视区域
var scrAll=$(window).scrollTop();//获取滑动距离
var hAll=heightAll+lookAll;
//滑动加载页面的条件
if(hAll>=scrAll){
num+=seed;
moreTwo();
}
//top按钮显示的条件
if(scrAll>=350){
$(".top").show();
}
});
}//success
});
</script>
//json的数据 名叫data.json (数据可添加)
[
{"imgs":"img/1.jpg","title":"买就送","bao":"包邮","price0":"20","price1":"2"},
{"imgs":"img/2.jpg","title":"买就送","bao":"包邮","price0":"38","price1":"1"},
{"imgs":"img/3.jpg","title":"买就送","bao":"包邮","price0":"280","price1":"20"},
{"imgs":"img/4.jpg","title":"买就送","bao":"包邮","price0":"270","price1":"45"}
]
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
*{
margin: 0;
padding: 0;
}
.data{
float: left;
width: 150px;
margin-left: 20px;
}
img{
width: 150px;
height: 150px;
}
.jia,.hua{
display: inline-block;
}
.jia{
color: red;
}
.more{
width: 100px;
height: 50px;
margin: 20px 125px;
}
#all{
width: 350px;
height: 100%;
}
# box{
width: 350px;
}
.hua{
text-decoration: line-through;
}
#all{
width: 305px;
margin: 0 auto;
}
.top{
position: fixed;
top: 350px;
right: 0;
width: 35px;
height: 35px;
background: red;
color: white;
border: 0;
outline: 0;
}
</style>
<script src="jquery-1.8.3.js"></script>
</head>
<body>
<div id="all">
<div id="box">
<!--<div class="data">
<img src="img/1.jpg" alt=""/>
<div class="mai">买一送一特价大放送</div>
<div class="bao">包邮</div>
<div class="jia">¥<span>2</span></div><div class="hua">原价 ¥<span>20</span></div>
</div>-->
</div>
<button class="top">回到顶部</button>
<button class="more">加载更多</button>
</div>
</body>
</html>
<script>
$(".top").hide();
$.ajax({
url: "data.json",
type:"get",
success: function(data){
var num=0;
var seed=6;//滚动条数
function moreTwo(){
var html="";
for(var k=num;k<num+seed&&k<data.length;k++){
html+="<div class='data'>"+
"<img src='"+data[k].imgs+"' alt=''/>"+
"<div class='mai'>"+data[k].title+"</div>"+
"<div class='bao'>包邮</div>"+
"<div class='jia'>¥<span>2</span></div><div class='hua'>原价 ¥<span>20</span></div>"+
"</div>";
//判断是否还有数据
if((k+1)>=data.length){
$(".more").html("我是有底线的");
}
}
$("# box").append(html);
}//moveTwo
//点击加载
$(".more").click(function(){
num+=seed;
moreTwo();
});
moreTwo();
//回到顶部
$(".top").click(function(){
$('body,html').animate({scrollTop:0},500);
});
//距离加载数据
$(window).scroll(function(){
var heightAll=$(document).height(); //获取页面长度
var lookAll=$(window).height();//获取可视区域
var scrAll=$(window).scrollTop();//获取滑动距离
var hAll=heightAll+lookAll;
//滑动加载页面的条件
if(hAll>=scrAll){
num+=seed;
moreTwo();
}
//top按钮显示的条件
if(scrAll>=350){
$(".top").show();
}
});
}//success
});
</script>
//json的数据 名叫data.json (数据可添加)
[
{"imgs":"img/1.jpg","title":"买就送","bao":"包邮","price0":"20","price1":"2"},
{"imgs":"img/2.jpg","title":"买就送","bao":"包邮","price0":"38","price1":"1"},
{"imgs":"img/3.jpg","title":"买就送","bao":"包邮","price0":"280","price1":"20"},
{"imgs":"img/4.jpg","title":"买就送","bao":"包邮","price0":"270","price1":"45"}
]