Load Content While Scrolling With jQuery and MVC 3.0
2011-10-29 16:30
447 查看
Source:http://www.webresourcesdepot.com/load-content-while-scrolling-with-jquery/ <---
http://webdeveloperplus.com/jquery/create-a-dynamic-scrolling-content-box-using-ajax/
http://superdit.com/2011/04/07/jquery-collapsible-load-scroll/
Content structure look like:[/b]
<div class="contentlatest" data-id="8">Some content is here...</div>
<div class="contentlatest" data-id="9">Some content is here...</div>
Firstly,I created a controller like:[/b]
public ActionResult Content()
{
int count = 5;
var list = JokeRepository.FindAll().OrderByDescending(j => j.CreateTime).Take(count);
return View(list);
}
public ActionResult ScrollToContent(int lastId)
{
int count = 5;
var list = JokeRepository.FindAll()
.Where(i => i.Id < lastId)
.Take(count).OrderByDescending(i => i.Id);
return Json(list,JsonRequestBehavior.AllowGet);
}
Then,a view is created.[/b]
@model IEnumerable<Funny.Models.Joke>
@foreach (var item in Model)
{
<div class="contentlatest" data-id="@item.Id">@item.Title</div>
}
<div id="lastpost-loading">
</div>
Finally,let's write some js to achieve scroll to load data.[/b]
<script type="text/javascript">
$(document).ready(function () {
var fetching = false;
var lastPost = function () {
fetching = true;
$('div#lastpost-loading').html('<img src=\'#\'" /Content/images/loading.gif">');
$.post("/Admin/Admin/Scroll?lastId=" + $('.contentlatest:last').data('id'),
function (data) {
if (data) {
for (var i in data) {
$('.contentlatest:last').after($.format('<div class=\"contentlatest\" data-id=\"{0}\">{1}</div>', data[i].Id, data[i].Title));
}
}
setTimeout(function(){
fetching = false;
},300);
$('div#lastpost-loading').empty();
}, "json");
};
$(window).scroll(function () {
var bufferzone = $(window).scrollTop() * 0.20;
if (!fetching && ($(window).scrollTop() + bufferzone > ($(document).height() - $(window).height()))) {
lastPost();
}
else{
$('div#lastpost-loading').text('data loaded');
}
});
});
</script>
Hope this help,
Lisknove.
http://webdeveloperplus.com/jquery/create-a-dynamic-scrolling-content-box-using-ajax/
http://superdit.com/2011/04/07/jquery-collapsible-load-scroll/
Content structure look like:[/b]
<div class="contentlatest" data-id="8">Some content is here...</div>
<div class="contentlatest" data-id="9">Some content is here...</div>
Firstly,I created a controller like:[/b]
public ActionResult Content()
{
int count = 5;
var list = JokeRepository.FindAll().OrderByDescending(j => j.CreateTime).Take(count);
return View(list);
}
public ActionResult ScrollToContent(int lastId)
{
int count = 5;
var list = JokeRepository.FindAll()
.Where(i => i.Id < lastId)
.Take(count).OrderByDescending(i => i.Id);
return Json(list,JsonRequestBehavior.AllowGet);
}
Then,a view is created.[/b]
@model IEnumerable<Funny.Models.Joke>
@foreach (var item in Model)
{
<div class="contentlatest" data-id="@item.Id">@item.Title</div>
}
<div id="lastpost-loading">
</div>
Finally,let's write some js to achieve scroll to load data.[/b]
<script type="text/javascript">
$(document).ready(function () {
var fetching = false;
var lastPost = function () {
fetching = true;
$('div#lastpost-loading').html('<img src=\'#\'" /Content/images/loading.gif">');
$.post("/Admin/Admin/Scroll?lastId=" + $('.contentlatest:last').data('id'),
function (data) {
if (data) {
for (var i in data) {
$('.contentlatest:last').after($.format('<div class=\"contentlatest\" data-id=\"{0}\">{1}</div>', data[i].Id, data[i].Title));
}
}
setTimeout(function(){
fetching = false;
},300);
$('div#lastpost-loading').empty();
}, "json");
};
$(window).scroll(function () {
var bufferzone = $(window).scrollTop() * 0.20;
if (!fetching && ($(window).scrollTop() + bufferzone > ($(document).height() - $(window).height()))) {
lastPost();
}
else{
$('div#lastpost-loading').text('data loaded');
}
});
});
</script>
Hope this help,
Lisknove.
相关文章推荐
- Load Content While Scrolling With jQuery
- Using jQuery Mobile with MVC and Netduino for Home Automation
- MVC Techniques with jQuery, JSON, Knockout, and C#
- [转]Using the HTML5 and jQuery UI Datepicker Popup Calendar with ASP.NET MVC - Part 4
- Asp.net MVC 4 application with Flexigrid, jQuery UI, and jQuery validation
- Twitter style paging with ASP.NET MVC and jQuery
- How To Generate JSON With PHP and Parse It With JQuery
- 海洋工作室——网站建设专家:jQuery-Ui Tabs and Asp.net MVC
- Multiple Files Upload in ASP.NET and ASP.NET with jQuery
- EGOImageLoading在UITableView滚动时无法加载图片do not load while scrolling
- IntelliJ IDEA:Getting Started with Spring MVC, Hibernate and JSON实践
- How to read out WhatsApp messages with Tasker and react on their content in real time
- jQuery Templates with ASP.NET MVC
- Using jQuery Grid With ASP.NET MVC
- [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)
- How to stop caching with jQuery and javascript
- Invalid content was found starting with element 'mvc:exclude-mapping'.
- IntelliJIDEA Getting+Started+with+Spring+MVC,+Hibernate+and+JSON
- 在ASP.NET MVC中使用jQuery的Load方法加载静态页面的一个注意点
- securely implement request processing, filtering and content redirection with HTTP pipeline in ASP.NET