javascript 如何实现 jScroll?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17895916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement jScroll?
提问by Blaze Tama
Im a beginner in JS & jQuery so please bear with me.
我是 JS 和 jQuery 的初学者,所以请耐心等待。
Im trying to create a dynamic list <ul>
using JS and finally its working. Now i need to implement the infinite scrolling concept in my list, using jScrollplugin.
我试图<ul>
使用 JS创建一个动态列表,最后它的工作。现在我需要使用jScroll插件在我的列表中实现无限滚动的概念。
So i researched a lot about jScroll, but i cant find any tutorial i needed. Most of the tutorials using PHP
language pretty much, while in my case i have done my server (PHP
) code using simple SELECT
query with LIMIT
and OFFSET
on it and returning a json
.
所以我研究了很多关于 jScroll,但我找不到任何我需要的教程。大多数教程PHP
几乎都使用语言,而在我的情况下,我PHP
使用简单的SELECT
查询完成了我的服务器()代码,LIMIT
并OFFSET
在其上返回了一个json
.
This is my jQuery/AJAX code that create the dynamic list from database, its already working:
这是我从数据库创建动态列表的 jQuery/AJAX 代码,它已经在工作:
$.ajax({
url: "http://localhost/jwmws/index.php/jwm/search/msmall/"+keyword, //This is the current doc
type: "GET",
error : function(jq, st, err) {
alert(st + " : " + err);
},
success: function(result){
//generate search result
//float:left untuk hack design
$('#search').append('<p style="float:left;">Search for : ' + keyword + '</p>'
+ '<br/>'
+ '<p>Found ' + result.length + ' results</p>');
if(result.length == 0)
{
//temp
alert("not found");
}
else{
for(var i = 0; i < result.length; i++)
{
//generate <li>
$('#list').append('<li class="box"><img class="picture" src="images/HotPromo/tagPhoto1.png"/><p class="name"><b>Name</b></p><p class="address">Address</p></li>');
}
var i=0;
$(".box").each(function(){
var name, address, picture = "";
if(i < result.length)
{
name = result[i].name;
address = result[i].address;
picture = result[i].boxpicture;
}
$(this).find(".name").html(name);
$(this).find(".address").html(address);
$(this).find(".picture").attr("src", picture);
i++;
});
}
}
});
Because my dynamic list is already working, now i just need to implement the jScroll. However, i dont understand its code, like :
因为我的动态列表已经在工作,现在我只需要实现 jScroll。但是,我不明白它的代码,例如:
$('.infinite-scroll').jscroll({
loadingHtml: '<img src="loading.gif" alt="Loading" /> Loading...',
padding: 20,
nextSelector: 'a.jscroll-next:last',
contentSelector: 'li'
});
How to implement this in my case? I just append <li>
in my jQUery/AJAX so how about the nextSelector
?
在我的情况下如何实现这一点?我只是附加<li>
在我的 jQUEry/AJAX 中,那么nextSelector
?
Any help is appreciated, please just ask if you have some question.
任何帮助表示赞赏,请询问您是否有任何问题。
Thanks for your help :D
谢谢你的帮助:D
回答by XIMRX
You hava every thing prety much set just needed to call jscroll at proper time.
您几乎设置了所有需要在适当的时间调用 jscroll 的东西。
$.ajax({
url: "http://localhost/jwmws/index.php/jwm/search/msmall/"+keyword, //This is the current doc
type: "GET",
error : function(jq, st, err) {
alert(st + " : " + err);
},
success: function(result){
//generate search result
//float:left untuk hack design
$('#search').append('<p style="float:left;">Search for : ' + keyword + '</p>'
+ '<br/>'
+ '<p>Found ' + result.length + ' results</p>');
if(result.length == 0)
{
//temp
alert("not found");
}
else{
for(var i = 0; i < result.length; i++)
{
//generate <li>
$('#list').append('<li class="box"><img class="picture" src="images/HotPromo/tagPhoto1.png"/><p class="name"><b>Name</b></p><p class="address">Address</p></li>');
}
//After generation of <li> put a next link
$('#list').append('<a href="#" class="jscroll-next">NEXT</a>');
//call to jscroller to be triggered
jscroller();
var i=0;
$(".box").each(function(){
var name, address, picture = "";
if(i < result.length)
{
name = result[i].name;
address = result[i].address;
picture = result[i].boxpicture;
}
$(this).find(".name").html(name);
$(this).find(".address").html(address);
$(this).find(".picture").attr("src", picture);
i++;
});
}
}
});
//The function to be called when <li> are rendered.
function jscroller(){
$('.infinite-scroll').jscroll({
loadingHtml: '<img src="loading.gif" alt="Loading" /> Loading...',
padding: 20,
nextSelector: 'a.jscroll-next:last',
contentSelector: 'li'
});
}