jQuery 砌体和无限滚动问题
<script>
$(window).load(function () {
var $wall = $('#content');
$wall.imagesLoaded(function () {
$wall.masonry({
itemSelector: '.oddpost',
isAnimated: true
});
$(".oddpost").each (function (idx, el) {
if ($(el).position().left < $(el).width()) {
$('.rightarrow', el).show();
}
else {
$('.leftarrow', el).show();
}
});
});
$wall.infinitescroll({
navSelector: '#pagination',
nextSelector: '#pagination li a.pagination_nextlink',
itemSelector: '.oddpost',
loadingImg: "http://static.tumblr.com/qrevc1p/WTKlx2dsg/gsnjnwui-um.gif",
loadingText: " ",
donetext: " ",
bufferPx: 100,
debug: false,
errorCallback: function () {
$('#infscr-loading').animate({
opacity: .8
}, 2000).fadeOut('normal');
}
}, function (newElements) {
var $newElems = $(newElements);
$newElems.hide();
$newElems.imagesLoaded(function () {
$wall.masonry('appended', $newElems, {
isAnimated: true,
animationOptions: {
duration: 900,
easing: 'linear',
queue: false
}
}, function () {
$newElems.fadeIn('slow');
});
});
});
$('#content').show(500);
});
</script>
我正在 Tumblr 主题上使用该脚本,并在其上应用砖石和无限滚动。这是一个两列布局,因此 div 只能向左或向右移动。除了在主题上应用砖石之外,它还在向左的 div 上放置了一个箭头(.rightarrow),并在向右的 div 上放置了一个不同的箭头(.left arrow)。
到目前为止,所有这些都运行良好。当加载下一页时,我的问题就开始了。显然,箭头代码仅适用于第一页,而不适用于新附加的元素。我尝试过在代码的不同部分迭代箭头代码,但我没有运气。这真的开始让我困惑了。我对 jQuery 并不陌生,但我真的很难让这项工作成功。
谁能告诉我应该在哪一部分放置箭头代码,以便它也适用于新附加的 div?
<script>
$(window).load(function () {
var $wall = $('#content');
$wall.imagesLoaded(function () {
$wall.masonry({
itemSelector: '.oddpost',
isAnimated: true
});
$(".oddpost").each (function (idx, el) {
if ($(el).position().left < $(el).width()) {
$('.rightarrow', el).show();
}
else {
$('.leftarrow', el).show();
}
});
});
$wall.infinitescroll({
navSelector: '#pagination',
nextSelector: '#pagination li a.pagination_nextlink',
itemSelector: '.oddpost',
loadingImg: "http://static.tumblr.com/qrevc1p/WTKlx2dsg/gsnjnwui-um.gif",
loadingText: " ",
donetext: " ",
bufferPx: 100,
debug: false,
errorCallback: function () {
$('#infscr-loading').animate({
opacity: .8
}, 2000).fadeOut('normal');
}
}, function (newElements) {
var $newElems = $(newElements);
$newElems.hide();
$newElems.imagesLoaded(function () {
$wall.masonry('appended', $newElems, {
isAnimated: true,
animationOptions: {
duration: 900,
easing: 'linear',
queue: false
}
}, function () {
$newElems.fadeIn('slow');
});
});
});
$('#content').show(500);
});
</script>
I'm using that script on a Tumblr theme I'm working on to apply masonry and infinite scrolling on it. It's a two column layout so the divs only goes either to the left or to the right. And aside from applying masonry on the theme, it also puts an arrow(.rightarrow) on the div that goes to the left and a different arrow(.left arrow) for the ones that goes to the right.
So far all of that is working fine now. My problem starts when the next page is loaded. Apparently, the arrow codes only applies on the first page but not on the newly appended elements. I've tried iterating the arrow codes on different parts of the codes but i Had no lucks on it. It's really starting to confuse me. I'm not new to jQuery but I'm really having a hard time trying to make this work.
Can anybody tell me which part I should put the arrow codes so it would apply to the newly appended divs too?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在将箭头添加到页面后运行显示箭头的代码。
我建议您使用一个函数来避免复制/粘贴。
在新元素上显示箭头的逻辑可能会遇到一些问题,因为当您向 jQuery 询问其位置和宽度时这些箭头不可见(取决于您的 css),如果您遇到问题,请尝试以下操作:
计算项目可见后显示的箭头。
You need to run the code that shows the arrows after they have been added to the page.
I recommend you use a function to avoid copy/paste.
You might have some troubles with your logic for showing the arrows on the new elements, because these are not visible at the time you ask jQuery for their position and width (depends on your css), if you are having trouble, try the following:
To calculate the arrow to show once the item is visible.