Javascript 加载()后我可以滚动到此DIV吗?
我很抱歉。以下是我问题的后续内容: 所有的解决方案都奏效了,其实这是我的错误,解释得不够好 发生的情况是,我有此代码,可以在单击时加载更多帖子:Javascript 加载()后我可以滚动到此DIV吗?,javascript,jquery,Javascript,Jquery,我很抱歉。以下是我问题的后续内容: 所有的解决方案都奏效了,其实这是我的错误,解释得不够好 发生的情况是,我有此代码,可以在单击时加载更多帖子: // load more $(".loadmore").click(function(){ if ($.cookie('n_more')) { count = $.cookie('n_more'); count++; $.cookie('n_more', count, { path: '/',
// load more
$(".loadmore").click(function(){
if ($.cookie('n_more')) {
count = $.cookie('n_more');
count++;
$.cookie('n_more', count, { path: '/', expires: 100 });
} else {
$.cookie('n_more', 1, { path: '/', expires: 100 });
}
$(".QueryElement").trigger('change');
var posts_visible = <?php echo $news['num_per_page']; ?>;
var posts_more = <?php echo $news['num_per_more']; ?>;
var newposition = posts_visible + (posts_more * $.cookie('n_more'));
var lastpos = newposition;
var thispost = $("#post-" + lastpos).position();
$('html,body').animate({
scrollTop: thispost.top
},1000);
});
//加载更多
$(“.loadmore”)。单击(函数(){
如果($.cookie('n_more')){
count=$.cookie('n_more');
计数++;
$.cookie('n_more',count,{path:'/',expires:100});
}否则{
$.cookie('n_more',1,{path:'/',expires:100});
}
$(“.QueryElement”).trigger('change');
var posts_visible=;
var posts_more=;
var newposition=posts_visible+(posts_more*$.cookie('n_more'));
var lastpos=新位置;
var thispost=$(“#post-”+lastpos).position();
$('html,body')。设置动画({
scrollTop:thispost.top
},1000);
});
这段代码对我来说很好,因为它调用.QueryElement更改触发器,并且更改触发器包含:
$(".QueryElement").change(function() {
// i hide almost all code here i think they are not related to the issue
$("#result").load('<?php bloginfo('template_directory'); ?>/GetResults.php?' + $.cookie('nw-query'));
});
$(“.QueryElement”).change(函数(){
//我在这里隐藏了几乎所有的代码,我认为它们与问题无关
$(“#result”).load('/GetResults.php?'+$.cookie('nw-query'));
});
逻辑是,当用户点击加载更多帖子时,我试图触发更改,这很好,新帖子也加载了,很好
但是滚动动画不起作用,我想是因为load()内容同时发生,并且动画滚动无法识别正在加载的新帖子ID??我不确定
但我真的想知道如何初始化animate onclick,并保证load()函数完成。我不知道从这里该怎么办
编辑:如果在加载()之前将位置设置为visible DIV/ID,则它可以工作,但在将新内容加载到页面并插入新DIV时,无法使其滚动。
例如,
var lastpos=newposition-1代码>因为我将其设置为一个div back(在通过load more加载内容之前)在运行时添加项目时,请尝试live而不是bind
$('.loadmore').live('click', function() {
// Live handler called.
});
阅读此内容了解更多信息
在您的更改(…)
函数中,我将利用完整参数并在那里执行滚动。实际上,它几乎显示了触发器之后的所有内容。
可以驻留在该回调中
EDITv2
使用您发布的代码,这就是更改。我已将所有内容从触发器移到加载的完整事件。在不知道代码中其他所有内容的情况下,据我所知,这应该是可行的
// load more
$(".loadmore").click(function(){
if ($.cookie('n_more')) {
count = $.cookie('n_more');
count++;
$.cookie('n_more', count, { path: '/', expires: 100 });
} else {
$.cookie('n_more', 1, { path: '/', expires: 100 });
}
$(".QueryElement").trigger('change');
});
$(".QueryElement").change(function() {
// i hide almost all code here i think they are not related to the issue
$("#result").load('<?php bloginfo('template_directory'); ?>/GetResults.php?' + $.cookie('nw-query'),function(){
var posts_visible = <?php echo $news['num_per_page']; ?>;
var posts_more = <?php echo $news['num_per_more']; ?>;
var newposition = posts_visible + (posts_more * $.cookie('n_more'));
var lastpos = newposition;
var thispost = $("#post-" + lastpos).position();
$('html,body').animate({
scrollTop: thispost.top
},1000);
});
});
//加载更多
$(“.loadmore”)。单击(函数(){
如果($.cookie('n_more')){
count=$.cookie('n_more');
计数++;
$.cookie('n_more',count,{path:'/',expires:100});
}否则{
$.cookie('n_more',1,{path:'/',expires:100});
}
$(“.QueryElement”).trigger('change');
});
$(“.QueryElement”).change(函数(){
//我在这里隐藏了几乎所有的代码,我认为它们与问题无关
$(“#result”).load('/GetResults.php?'+$.cookie('nw-query'),function(){
var posts_visible=;
var posts_more=;
var newposition=posts_visible+(posts_more*$.cookie('n_more'));
var lastpos=新位置;
var thispost=$(“#post-”+lastpos).position();
$('html,body')。设置动画({
scrollTop:thispost.top
},1000);
});
});
您还可以尝试使用jQuery的scrollTo插件。文件可在
这里有一些。您需要为新添加的项目使用live,以便它为将来的元素绑定。请给我一个非常快速的示例好吗?
// load more
$(".loadmore").click(function(){
if ($.cookie('n_more')) {
count = $.cookie('n_more');
count++;
$.cookie('n_more', count, { path: '/', expires: 100 });
} else {
$.cookie('n_more', 1, { path: '/', expires: 100 });
}
$(".QueryElement").trigger('change');
});
$(".QueryElement").change(function() {
// i hide almost all code here i think they are not related to the issue
$("#result").load('<?php bloginfo('template_directory'); ?>/GetResults.php?' + $.cookie('nw-query'),function(){
var posts_visible = <?php echo $news['num_per_page']; ?>;
var posts_more = <?php echo $news['num_per_more']; ?>;
var newposition = posts_visible + (posts_more * $.cookie('n_more'));
var lastpos = newposition;
var thispost = $("#post-" + lastpos).position();
$('html,body').animate({
scrollTop: thispost.top
},1000);
});
});