scroll

by Marc Chehab

HTML

blabladüoasnfd
<br />
asdüoian
<div id="container">
  <div class="comment" data-id="c1" data-comment-time="1">rinter took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of
    Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.rinter took a galley of type and scrambled it to m</div>
  <div class="comment" data-id="c2" data-comment-time="5">HERE IT SHOULD BE setting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.rinter
    took a galley of type and scrambled it to m</div>
  <div class="comment" data-id="c3" data-comment-time="30">setting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.rinter took a galley of type and scrambled it to msetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.rinter took a galley of type and scrambled it to msetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.rinter took a galley of type and scrambled it to msetting, remaining essentially unchanged. It was popularised in the 1960s with...

CSS

#container {
  width: 400px;
  height: 200px;
  border: 1px dotted;
  overflow-y: scroll;
}

.comment {
  border-bottom: 5px solid black;
}

JavaScript

var vidtime = 0;
var timer, commenttimer;
var comments = new Array();

function buildarray() {
  var array = [];
  array = $("#container > div.comment").map(function() {
    return [$.map($(this).data(), function(v) {
        return v;
    })];
  }).get();
  comments = array;
}

function settime (x) {
 if (!(isNaN(x))) {vidtime=x};
 $("#timer").html(vidtime);
 clearInterval(timer);
 timer = setInterval(function () {
    ++vidtime;
    $("#timer").html(vidtime);
}, 1000);
}

function reset (y) {
 if (!(isNaN(y))) { settime(y) } else {settime(0)} ;
 scroll(1);
}

function scroll (jump) {
 $("#container").stop();
 $("#button2").css("opacity", "0.5");
 for(var i=0; i < comments.length;i++ ) {
  if (comments[i][1] > vidtime) {
//jump if x=1
if (jump) { if(i==0) { 
$("#container").scrollTop(0);
} else { 
$("#container").scrollTop($('*[data-id="' + comments[i-1][0] + '"]').offset().top + $("#container").scrollTop()  - $("#container").offset().top);
} }
//scroll to next element
   $("#container").animate({ scrollTop: 
 	  $('*[data-id="' + comments[i][0] + '"]').offset().top + $("#container").scrollTop()  - $("#container").offset().top
 }, (comments[i][1]-vidtime)*1000);
//time next instance
   clearInterval(commenttimer);
    commenttimer = setInterval(function () {
     scroll();
    }, (comments[i][1]-vidtime)*1000);
//bugfixxing
//$('#print').html(comments[i][0] + ", " + comments[i][1] + ", " + (comments[i][1]-vidtime)*1000 + "/" +  $('*[data-id="' + comments[i][0] + '"]').offset().top + ", " + vidtime);
break;
   }
  }
}

function unlink () {
 clearInterval(commenttimer);
 $("#container").stop();
 $("#button2").css("opacity", "1");
}

$(document).ready(function() {
  $('#button').click(reset);
  $('#button2').click(function() { reset(vidtime) });
  $('#container').bind('wheel touchstart', function(e) {   $('#print').html(e.which + ", " + e.type); unlink (); });
  $('#container').bind('mousedown', function(e) {
  if (e.target === $('#container').get(0)) {...