jQuery Scrool

by Twisty

HTML

<div id="main">
  <div class="part" style="background-color: rgba(255,0,0,0.25); top: 0; left:0;"></div>
  <div class="part" style="background-color: rgba(0,0,255,0.25); top: 0; left: 1500px;"></div>
  <div class="part" style="background-color: rgba(255,255,0,0.25); top: 900px; left: 0;"></div>
  <div class="part" style="background-color: rgba(0,255,0,0.25); top: 900px; left: 1500px;"></div>
</div>

CSS

body {
  margin: 0;
  padding: 0;
  overflow: hidden;
}

#main {
  position: relative;
}

.part {
  width: 1500px;
  height: 900px;
  position: absolute;
}

JavaScript

$(function() {
  function moveToCenter() {
    var vert = $(document).height() - $(window).height();
    var horz = $(document).width() - $(window).width();
    console.log("Centering View:", Math.round(horz / 2), Math.round(vert / 2));
    window.scrollTo(Math.round(horz / 2), Math.round(vert / 2));
  }

  function calcEdges(buff) {
    var t = [0, buff];
    var b = [$(window).height() - buff, $(window).height()];
    var l = [0, buff];
    var r = [$(window).width() - buff, $(window).width()];
    return {
      top: t,
      left: l,
      right: r,
      bottom: b
    };
  }

  function move(d, n) {
    var c, g;
    if (d == "top") {
      c = $(window).scrollTop();
      g = c + n;
      if (g <= 0 || (g >= $(document).height())) {
        console.log("Hit Top/Bottom Boundry");
        return false;
      }
      $(window).scrollTop(g);
    } else {
      c = $(window).scrollLeft();
      g = c + n;
      if (g <= 0 || (g >= $(document).width())) {
        console.log("Hit Left/Right Boundry");
        return false;
      }
      $(window).scrollLeft(g)
    }
  }

  function init() {
    moveToCenter();
    var sides = calcEdges(30);
    $(window).mousemove(function(e) {
      var x = e.clientX,
        y = e.clientY;
      if (x > sides.left[0] && x < sides.left[1]) {
        console.log("Dir: Left", x);
        move("left", -5);
      } else if (x > sides.right[0] && x < sides.right[1]) {
        console.log("Dir: Right", x);
        move("left", 5);
      }
      if (y > sides.top[0] && y < sides.top[1]) {
        console.log("Dir: Top", y);
        move("top", -5);
      } else if (y > sides.bottom[0] && y < sides.bottom[1]) {
        console.log("Dir: Bottom", y);
        move("top", 5);
      }
    });
  }
  init();
});