所以我有一個生成這樣jQuery和AJAX--添加了Ajax的對象動態不能用於jQuery功能?
<div class="photoset">
<img />
<img />
</div>
<div class="photoset">
<img />
<img />
<img />
</div>
東西MVC編碼和我試圖做一個jQuery的無限滾動和用戶滾動,它會產生更多的photosets。
我在這方面做得有些成功,但是在用無限滾動生成更多的照片後,新添加的代碼無法與之前的jQuery插件一起在圖像之間導航。
這裏是jQuery代碼導航:
jQuery的
$(document).ready(function() {
$('.photoset').each(function() {
$(this).data('counter', 0);
$items = $(this).find('img')
$(this).data('numItems', $items.length);
$(this).closest('.row').prev('.row').find('.nav-informer').text($(this).data('counter') + 1 + " de " + $(this).data('numItems'));
if (($items.eq(0).width()/$items.eq(0).height()) < 1) {
$(this).css({
width: "445",
height: "667",
margin: "auto"
});
}
});
var showCurrent = function (photoset) {
$items = photoset.find('img');
var counter = photoset.data('counter');
var numItems = $items.length;
var itemToShow = Math.abs(counter % numItems);
$items.fadeOut(1000);
$items.eq(itemToShow).fadeIn(1000);
};
/* ---------------------------------------------- /*
* Prev
/* ---------------------------------------------- */
$('.prev').on('click', function() {
var photoset = $(this).closest('.row').prev('.row').find('.photoset');
photoset.data('counter', photoset.data('counter') - 1);
if (photoset.data('counter') < 0)
photoset.data('counter', photoset.data('numItems') - 1);
photoset.closest('.row').prev('.row').find('.nav-informer').text(photoset.data('counter') + 1 + " de " + photoset.data('numItems'));
showCurrent(photoset);
});
/* ---------------------------------------------- /*
* Next
/* ---------------------------------------------- */
$('.next').on('click', function() {
var photoset = $(this).closest('.row').prev('.row').find('.photoset');
photoset.data('counter', photoset.data('counter') + 1);
if (photoset.data('counter') > photoset.data('numItems') - 1)
photoset.data('counter', 0);
photoset.closest('.row').prev('.row').find('.nav-informer').text(photoset.data('counter') + 1 + " de " + photoset.data('numItems'));
showCurrent(photoset);
});
});
而阿賈克斯函數來調用更多的photosets,傳遞頁碼:
阿賈克斯
var page = 0,
inCallback = false,
hasReachedEndOfInfiniteScroll = false;
var ulScrollHandler = function() {
if (hasReachedEndOfInfiniteScroll == false &&
($(window).scrollTop() == $(document).height() - $(window).height())) {
loadMoreToInfiniteScrollUl(moreRowsUrl);
resizeNewElements();
}
}
function loadMoreToInfiniteScrollUl(loadMoreRowsUrl) {
if (page > -1 && !inCallback) {
inCallback = true;
page++;
$(".loading").show();
$.ajax({
type: 'GET',
url: loadMoreRowsUrl,
data: "pageNum=" + page,
success: function (data, textstatus) {
if (data != '') {
$(".infinite-scroll").append(data);
}
else {
page = -1;
}
inCallback = false;
$(".loading").hide();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
}
});
}
}
function showNoMoreRecords() {
hasReachedEndOfInfiniteScroll = true;
}
這是調用無限滾動功能:我做了一些研究,但不能stabilish一個原因,我的代碼不能正常工作
$(function() {
$(".loading").hide();
});
var moreRowsUrl = '@Url.Action("index", "home")';
$(window).scroll(ulScrollHandler);
,嘗試了一些東西,但沒有成功。如果有人能幫助我,那會很棒!在此先感謝,對於任何錯誤感到抱歉,我對jQuery和Ajax非常感興趣。
護理添加的jsfiddle? – 2016-01-23 22:22:45
你檢查錯誤嗎? – ZgrKARALAR
您使用AJAX動態添加了哪些元素?如果你添加了更多的'photoset' DIV,他們將不會有任何數據和'$(「。photoset」)。each()'循環添加的CSS,因爲這隻適用於那裏的元素當頁面被加載時。 – Barmar