我怎样才能制作一个类似窗口滚动控制系统的好友推送?

发布于 2024-08-15 13:48:51 字数 1087 浏览 8 评论 0原文

当您激活实时更新时,新条目会动态添加 div。在此阶段,滚动条会自动移动。此操作在可见区域提供您不会错过的内容。

如果你想看这个动作,你也可以观看这个截屏视频; http://www.viddler.com/explore/itod/videos/45/ < /a>

我的方法;

    // 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

When you active real-time updates, new entries dynamically adding a div. At this stage scroll is automatically moving. This action provides the content you do not miss on visible area.

If you want to see this action, you can also watch this screencast; http://www.viddler.com/explore/itod/videos/45/

My method;

    // 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱果果 2024-08-22 13:48:51

我假设您想平滑地滚动到顶部而不是跳到解决“闪烁”问题。我会做这样的事情:

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 [];
}

I assume you want to scroll smoothly to the top instead of jumping to solve 'flashing'. I would do something like this:

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 [];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文