English 中文(简体)
How can i make a friendfeed like window scroll control system?
原标题:

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
问题回答

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




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签