当您激活实时更新时,新条目会动态添加div。在此阶段,滚动会自动移动。此操作提供您在可见区域上不会错过的内容。
如果您想看到此动作,您还可以观看此截屏视频; http://www.viddler.com/explore/itod/videos/45/
我的方法;
// Firstly, i am storing the first entry's(in view) positions in window object;
jQuery(window).scroll(function() {
var q = 0;
jQuery(".entry").each(function (i) {
if (jQuery(this).offset().top > jQuery(window).scrollTop()) {
if (q == 0) {
window.show_id = jQuery(this).attr("id");
window.pos_y = jQuery(this).offset().top - jQuery(window).scrollTop();
q = 1;
}
}
});
});
// After coming to the new entry, i call this function;
function scroll_control() {
var scroll_top = jQuery(window).scrollTop();
if (scroll_top != 0) {
if (jQuery('#'+window.show_id).length != 0) {
var scr = jQuery('#'+window.show_id).offset().top - window.pos_y;
window.scrollTo(0, scr);
}
}
}
// but this is due to flashing. I guess not fast enough
答案 0 :(得分:0)
我假设您想要顺畅地滚动到顶部而不是跳跃来解决'闪烁'。我会做这样的事情:
function scroll_control() {
var scroll_top = jQuery(window).scrollTop();
if (scroll_top != 0) {
if (jQuery('#'+window.show_id).length != 0) {
var scr = jQuery('#'+window.show_id).offset().top - window.pos_y;
// Instead of jumping to src use jQuery animate
// window.scrollTo(0, scr);
var scrollElem = scrollableElement('html', 'body');
$(scrollElem).animate({scrollTop: scr}, 400);
}
}
}
/*
* Use the first element that is "scrollable" (cross-browser fix?)
* http://css-tricks.com/snippets/jquery/smooth-scrolling/
*/
function scrollableElement(els) {
for (var i = 0, argLength = arguments.length; i <argLength; i++) {
var el = arguments[i],
$scrollElement = $(el);
if ($scrollElement.scrollTop()> 0) {
return el;
} else {
$scrollElement.scrollTop(1);
var isScrollable = $scrollElement.scrollTop()> 0;
$scrollElement.scrollTop(0);
if (isScrollable) {
return el;
}
}
}
return [];
}