Ajax附加的元素赢得't与masonry.js定位的相似元素一致
Ajax appended elements won't fall in line with like elements positioned by masonry.js
我正在这里写这个博客:http://insights.signetaccel.com/blog
这个列表页面上的博客摘要是使用一个名为masonry.js的脚本定位的。摘要的容器是".grid",而摘要都被归类为".griditem"。
我有另一个脚本,它创建了一个无限滚动效果,请求url到下一页,找到所有.grid项,并将它们附加到页面上.grid容器的底部。
问题是masonry.js脚本将内联样式应用于.grid项div来定位它们。新添加的.grid项div不受masony.js的影响,因为它们是在页面加载后添加的。
如何让masonry.js脚本在添加新元素后将自己"重新应用"到页面上,以便将其应用到这些新元素上?
无限滚动脚本
(function($) {
$.fn.swoosh=function(loadingImgUrl,callback){
if(!loadingImgUrl){
loadingImgUrl="Loading...";
}
if (callback==null){
callback=-1;
}
var postList=this;
//SET VARIABLES
var turnOff=false;
var pageNumber=2; //toggle switch for infinite scroll. shuts off when on last page
var urlArray=window.location.href.toString().split("/"); //the array of url sections
var blogUrl=urlArray[0]+"//"+urlArray[2]+"/"+urlArray[3]+"/"; //the url for the blog
var baseUrl=blogUrl+"page/"; //URL with the page number stripped off the end
var postUrl=""; //initialize postURL string
var processing = false; //tells the scripts if there is already an ajax load happening
//insert the loading bar at the end of the posts-list
if(loadingImgUrl!="Loading..."){
postList.parent().append('<div class="loading"><img src="'+loadingImgUrl+'"></div>');
}
else{
postList.parent().append('<div class="loading">'+loadingImgUrl+'</div>');
}
$(".loading").hide(); //make sure loading bar is hidden
$(document).scroll(function(){
//kick out of function if it's already running, if it has been shut off, or if there is no 2nd page
if (processing || turnOff || pageNumber==0){
return false;
}
//when scrolling gets to the footer, start chugging!
if ($(window).scrollTop() >= $(document).height() - $(window).height() - $(".footer-container-wrapper").height()-150){
processing = true;
//currently processessing, so don't call function again until done
$(".loading").fadeIn(200); //fade in loading bar
postUrl=baseUrl + pageNumber; //calculate the page to load
//AJAX CALL
$.get(postUrl, function(data){
//grab only post items from the loaded page
var posts=$(data).find(".grid-item");
//check that the loaded page has content
if (posts.length > 0){
console.log(loadingImgUrl);
//fade out the loading bar, then attach the new items to the end of the list
$(".loading").fadeOut(200,function(){
posts.appendTo(".grid");
console.log(posts);
});
pageNumber++; //increment the page to load.
$(".next-posts-link").attr("href",baseUrl+pageNumber);
}
//if the loaded page doesn't have content, it means we have reached the end.
else{
turnOff=true;
$(".next-posts-link").after('<div class="next-posts-link unactive">Next</div>');
$(".next-posts-link:not(.unactive)").remove();
$(".loading").fadeOut(200);
}
processing=false; //we are done processing, so set up for the next time
setTimeout(function(){
twttr.widgets.load();
IN.parse();
FB.XFBML.parse();
gapi.plusone.go();
},350);
});
}
});
};
})(jQuery);
您应该在posts.appendTo(".grid");
之后$.post
之后rebind
masonry
//take your binding in a variable
var container = $('.grid').masonry({
// options
itemSelector: '.grid-item',
columnWidth: 200,
columnWidth: '.grid-item',
percentPosition: true
});
//do this on $.post
$(".loading").fadeOut(200,function(){
posts.appendTo(".grid");
console.log(posts);
//changes made here in your code
container.append(posts).masonry('appended', posts);
});
相关文章:
- JQuery添加元素需要在我的js之前再次添加JQuery脚本
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 使用CSS或js,使用动态选择器选择任意li的下一个元素
- HTML 显示/隐藏元素 JS 不起作用
- 定位 d3 上的其他元素.js事件上
- 如何获取
- 元素 js/ajax 的 Id 值
- 从元素 JS 中获取类
- 单击 html 元素 JS
- 数据未追加到车把中的元素.js
- 如何使用节点逐个获取元素.js Cheerio
- MEJS(媒体元素JS)播放功能
- 检测是否在输入元素JS中输入了特殊字符
- 如何根据下拉选择消除HTML表单中的元素?(JS吗?)
- 带有动态链接标记的可满足元素(JS、jQuery)
- 仅为UL的第一个元素(JS或jquery)添加类的样式
- 自动时间元素js
- 可以't获取API响应的特定元素[JS]
- 修改指定li元素JS的父级
- 奇异:CSS转换元素JS "点击通过"
- 通过文本框获取数组元素(JS验证)