Home > Web Front-end > JS Tutorial > body text

Ajax realizes clicking to load more data pictures (preloading)

青灯夜游
Release: 2018-10-11 17:59:31
forward
3061 people have browsed it

This article will introduce to you how ajax can achieve the effect of clicking to load more data pictures (preloading). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>ajax点击加载更多数据--博客园--勇淘未来</title>
        <script type="text/javascript" src="jquery-1.11.1.min.js"></script>
        <style>
            *{padding:0;margin:0;}
            .box {margin: 100px auto;width: 550px;}
            ul li {width:550px;list-style: none;}
            ul li span{text-align:center;display:block;}
            .clear {clear: both;}
            .load {text-align: center;display: none;margin-top:50px;color:#ccc;}
            .end{display:none;color:#ccc;}
        </style>
    </head>
    <body>
        <div class="box">
            <ul></ul>
            <div class="clear"></div>
            <div class="load">加载中...</div>
            <div class="more" style="text-align: center;margin-top:50px;">
                <button class="btn">查看更多图片</button>
                <div class="end">没有更多了</div>
            </div>
        </div>
        <script>
            var num = 0;
            var start = 0;
            var size = 2;
            $.ajax({
                url: "dataNews.json",
                type: "get",
                success: function(res){
                    var str = "";
                    for(var i = 0;i < 2;i++){
                        str += "<li><img src=" + res[i].img + "><span>"+ res[i].title +"</span></li>";
                    }
                    $(".box ul").append(str);
                },
                error:function(){
                    console.log(errors);
                }
            })
            $(".btn").click(function(){
                $(".load").show();
                setTimeout(function(){
                    $(".load").hide();
                    num++;
                    console.log(num);
                    start = num * size;
                    $.ajax({
                        url:"dataNews.json",
                        type:"get",
                        success:function(res){
                            var sum = res.length;
                            if(start + size > sum) {
                                size = sum - start;
                                $(".btn").css("display","none");
                                $(".end").css("display","block");
                            }
                            var str = "";
                            for(var i = start;i<(start + size);i++) {
                                str += "<li><img src=" + res[i].img + "><span>"+ res[i].title +"</span></li>";
                            }
                            console.log(start + size);
                            $("ul").append(str);
                        }
                    });
                },300)
            }
        )
        </script>
</body>
</html>
Copy after login

Local test dataNews.json file:

[ {
	"img":"img/sina.jpg","title":"百度音乐1"
}
, {
	"img":"img/tengxu.jpg","title":"百度音乐2"
}
, {
	"img":"img/sina.jpg","title":"百度音乐3"
}
, {
	"img":"img/tengxu.jpg","title":"百度音乐4"
}
, {
	"img":"img/tengxu.jpg","title":"百度音乐5"
}
, {
	"img":"img/sina.jpg","title":"百度音乐6"
}
, {
	"img":"img/tengxu.jpg","title":"百度音乐7"
}
, {
	"img":"img/sina.jpg","title":"百度音乐8"
}
, {
	"img":"img/tengxu.jpg","title":"百度音乐9"
}
, {
	"img":"img/sina.jpg","title":"百度音乐10"
}
, {
	"img":"img/tengxu.jpg","title":"百度音乐11"
}
, {
	"img":"img/sina.jpg","title":"百度音乐12"
}
, {
	"img":"img/tengxu.jpg","title":"百度音乐13"
}
, {
	"img":"img/sina.jpg","title":"百度音乐14"
}
, {
	"img":"img/tengxu.jpg","title":"百度音乐15"
}
]
Copy after login

Click on more pictures to load 2 pieces of data. When the data is loaded, it will display "No more"

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study. For more related tutorials, please visit AJAX Video Tutorial!

The above is the detailed content of Ajax realizes clicking to load more data pictures (preloading). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template