- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一段 jquery 可以在我的博客上加载新帖子,问题是它运行了两次,这意味着帖子是重复的。我还想找到一种方法,让它在用户距离文档底部还有几个像素(比如 50 个像素)时运行。 (图很简单)。
该错误是在我在调整代码之前获取代码的网站上报告的: http://blog.hycus.com/2011/03/15/infinite-scrolling-like-new-twitter-with-php-mysql-jquery
一条评论建议使用 .data() 但我不太确定它是如何工作的。该代码是从一个单独的 js 文件运行的,其链接位于正文中。 Firebug 报告了两个帖子请求,最终博客帖子显示了两次。
$(document).ready(function(){
$(window).scroll(function(){
if($(window).scrollTop() === $(document).height() - $(window).height()){
$('div#loadmoreajaxloader').show();
$.post("blog/loadmore", {pid: $(".blogpost:last").attr("id")}, function(html){
if(html){
$("#bloglist").append(html);
$('div#loadmoreajaxloader').hide();
}else{
$('div#loadmoreajaxloader').html('<center>No more posts to show.</center>');
}
});
};
});
});
我无法发布自己问题的答案...我解决了它:
这似乎解决了我的问题,如果有人有更好的解决方案,请告诉我。
还尝试了一些方法来使页面加载在到达页面末尾之前发生(我想 if ($(window).scrollTop()+50 === $(document).height() - $(window).height()) 可能可以解决问题)但它们不起作用。
var lid = $(".blogpost:last").attr("id");
$(document).ready(function() {
$(this).scrollTop(0);
$(window).scroll(function() {
if ($(window).scrollTop() === $(document).height() - $(window).height()) {
if(lid !== $(".blogpost:last").attr("id")){
$('div#loadmoreajaxloader').show();
$.post("blog/loadmore", {
pid: $(".blogpost:last").attr("id")
}, function(html) {
if (html) {
$("#bloglist").append(html);
$('div#loadmoreajaxloader').hide();
} else {
$('div#loadmoreajaxloader').html('<center>No more posts to show.</center>');
}
});
lid = $(".blogpost:last").attr("id");
}
};
});
});
最佳答案
查看该代码,我不太喜欢它的设置方式。该脚本需要对元素进行一些缓存,这样您就不会不断地一遍又一遍地调用同一个选择器。
我在 onDOMReady
匿名函数回调中使用了 inproc
(进程中)变量。这是在顶层设置的,但由于匿名函数提供的闭包,该变量将保持对包含函数可用。在这里,我测试一下它是否true
。
您将看到我将所有 jQuery 调用移至开头并将它们添加为变量。这称为缓存,有助于防止在同一脚本中一遍又一遍地发出相同的请求。效率要高得多。请注意,使用 $('div#id')
即可,但无需添加 id
前缀;只需以 id
开头,例如 $('#id')
。在我看来,之前的任何事情都是不必要的。
我还设置了 200px
预加载距离。 50px
并不像您想象的那么高;几乎看不出有什么不同。
超时完全是为了延迟内容的加载,以便您可以测试滚动。打开 Firebug 以查看 console.log()
消息。
此脚本大量包含与测试相关的变量和函数,但您应该能够删除任何“出于测试目的”或其某些变体,并使其正常工作。
// For test purposes.
var i = 0;
$(document).ready(function(){
var inproc = false,
id,
$win = $(window),
$doc = $(document),
$bloglist = $("#bloglist"),
$loadmore = $('#loadmoreajaxloader'),
// For test purposes.
dummycontent = $('.dummycontent').html(),
loadtimeout = false;
// Note, load is for testing; it goes with the setTimeout().
var callbackBlogLoaded = function(html){
var self = this;
// For testing purposes.
if (loadtimeout !== false) {
loadtimeout = false;
setTimeout(function(html){
callbackBlogLoaded.call(self, html);
}, 3000);
return false;
}
inproc = false;
// For test purposes.
i++;
html = dummycontent;
if (i > 5) html = '';
if (html) {
// For test purposes.
$bloglist.append('<p style="color: red; font-weight: bold;">i is ' + i + ' of 5 total</p>' + html);
$loadmore.hide();
} else {
// <center> is deprecated; use a class on a P or DIV instead.
$loadmore.html('<p class="center">No more posts to show.</p>');
}
};
// For test purposes.
$bloglist.append($('.dummycontent').html());
$win.scroll(function(){
// You need to subtract the scroll-load pad distance from the
// right-side height calculation, not .scrollTop().
if ($win.scrollTop() > ($doc.height() - $win.height() - 200)){
if (inproc == true) {
// For testing purposes.
console.log('Content load blocked: In process with another request.');
return false;
}
inproc = true;
id = $bloglist.has(':last').attr('id');
// For test purposes
id = 10;
loadtimeout = true;
$loadmore.show();
// The /echo/html/ is for testing purposes.
$.post("/echo/html/", { pid: id }, callbackBlogLoaded);
};
});
});
关于基于滚动位置的 jquery post 代码触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9249471/
这个问题已经有答案了: jQuery trigger click vs click ()? (3 个回答) 已关闭 5 年前。 我无法区分 trigger('click')与 trigger('cli
我正在运行 VS 2008 和 .NET 3.5 SP1。 我想在 HttpModule 中实现命中跟踪在我的 ASP.NET 应用程序中。很简单,我想。然而,BeginRequest我的事件 Htt
这是一段代码,我收到以下错误 #1064 - You have an error in your SQL syntax; check the manual that corresponds to yo
有没有办法用任意增量触发滚轮事件。就像 jQuery 对“点击”所做的那样: $('#selector').trigger('click'); 我需要类似的东西,只需一个滚轮即可: $('#selec
我正在尝试在配音数据库中触发时间。我想检查一下在不出现角色的电影配音中不能对角色进行配音。这是PDM: 和CDM 我是SQL的初学者,但我知道表“DUBBES”中应该有一些触发器。我试图做这样的事情,
这个问题已经有答案了: jquery programmatically click on new dom element (3 个回答) 已关闭 6 年前。 我有一个 jQuery 事件定义如下: $
主菜单的点击代码适用于类更改,但不适用于子菜单...当单击食物或鞋子等子菜单项时,它不会触发警报命令...事实上,悬停非常适合子菜单但不是活跃的 HTML
问题非常简单: $('#btn1').click(function(event){ alert( "pageX: " + event.pageX + "\npa
我使用 Spring 的调度程序 (@EnableScheduling) 并具有以下 @Scheduled 方法,该方法每分钟调用一次: @Component public class Schedul
错误 SQL 查询:文档 CREATE TRIGGER `triggers_div` AFTER INSERT ON `produits` FOR EACH ROW BEGIN INSERT INTO
我想在插入另一个表时填充表中的一些列值,并为特定列设置条件。我使用触发器: CREATE TRIGGER inserttrigger AFTER INSERT ON table1 FOR EACH R
我可以在 5.6 MySQL 环境中使用一些关于触发器的指导。我想创建一个触发器,如果发现具有相同速度的电脑的价格较低,则该触发器会停止更新。 架构是产品(制造商、型号、类型)PC(型号、速度、内
背景:我们有一个 completed_flag,默认为 0,当有人完成调查时更新为 1。我想记录这次更新发生的时间戳 在编写了这个触发器/函数以在标志从 0 触发到 1 时更新时间戳后,我怀疑我这样做
数据库中有两个表 KistStatus和 LastKistStatus .后者将保存 KistStatus 的所有“最新”值。 . KistStatus有大约 174.000 条记录,LastKist
我正在开发一个使用 APNS 的 iPhone 应用程序。我很清楚实现 APNS、创 build 备 token 的过程,等等等等……我不知道如何通过 Web 服务从提供商端触发和启动 APNS。任何
我有这个 javascript,当数量更改时会触发 update_cart... jQuery('div.woocommerce').on('change', '.qty', function
当我单击任何按钮时,click 事件不会被触发。艰难的是,我使用 $("div").on("click", "button", function () { 让它工作,但我想看到它使用 .class 工
如何在我的代码中触发 Android onCreateOptionsMenu 函数,即无需用户单击手机上的选项菜单按钮? 最佳答案 Activity.openOptionsMenu(); 就可以了 关
我将表单包装在 中然后我设置 list android:windowSoftInputMode="adjustResize" (默认 react native )。现在,当我用手指触摸事件手动聚焦一
我有一个 Android 编程问题。使用下面的代码我想验证一个字符串匹配。它验证正常,但 LogCat 显示 TextWatcher 方法在每次击键时触发两次,我不明白为什么。我希望每次击键只触发一次
我是一名优秀的程序员,十分优秀!