为什么我无法使此jquery代码正常工作? [英] Why I can't get this jquery code to work?
本文介绍了为什么我无法使此jquery代码正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试滚动页面以张贴具有特定ID的帖子,例如21后22后等等
I am trying to make the page scroll to post which has specific id e.g. post-21 post-22 etc
但是它行不通.请问我的语法有什么问题吗?
But It won't work. what could be wrong with my syntax here please?
// this is number of posts visible. returns e.g. 2
var posts_visible = <?php echo $news['num_per_page']; ?>;
// this is number of more posts loaded. returns e.g. 2
var posts_more = <?php echo $news['num_per_more']; ?>;
// here i calculate the value to know which position should we be at after scroll
var newposition = posts_visible + (posts_more * $.cookie('n_more'));
// here i am trying to set #post-(thepostnumber)
var thispost = '$("#post-' + newposition + '")';
$('html,body').animate({
scrollTop: thispost.position().top + 'px',
scrollLeft: thispost.position().left + 'px'
},1000);
请注意,alert(thispost)
会准确地返回具有我应该在的正确ID的帖子.只是无法使其在动画/滚动中正常工作.请帮助我
Please note that alert(thispost)
returns accurately the post with the proper ID i should be at. Just can't get it to work in the animate/scroll. Please help me
推荐答案
您只需要选择器作为字符串,而不需要整个$()
函数调用,如下所示:
You need just the selector as a string, not the entire $()
function call, like this:
var thispost = $("#post-" + newposition).position();
$('html,body').animate({
scrollTop: thispost.top + 'px',
scrollLeft: thispost.left + 'px'
},1000);
还可以像上面那样进行优化...无需两次调用 .position()
得到相同的结果.
Also it can be optimized like above...no need to call .position()
twice to get the same result.
这篇关于为什么我无法使此jquery代码正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文