0

I want to scroll the HTML page based on the mouse cursor movements, so that when moving the mouse cursor in any direction (X & Y), the scrollbars of the page move with it, scrolling the page. I'd also like the scroll to have an easing, so that it has a slight delay when moving the mouse.

I have a working prototype (without easing): https://jsfiddle.net/ssq7cda4/12/

$(document).mousemove(function (e) {
    var winW = $(window).width(),
        docW = $(this).width() - winW,
        i = docW / winW, //increment value
        x = (e.pageX - $(window).scrollLeft()) * i;

    //$(window).scrollLeft(x);
    //$("html, body").animate({scrollLeft:x}, 500, 'swing');
    $("html, body").scrollLeft(x);
});

$(document).mousemove(function (e) {
    var winW = $(window).height(),
        docW = $(this).height() - winW,
        i = docW / winW, //increment value
        x = (e.pageY - $(window).scrollTop()) * i;

    //$("html, body").animate({scrollTop:x}, 500, 'swing');
    $(window).scrollTop(x);
});

The only problem is that I haven't found a way to add the easing to the scrolling. Using .animate makes the scrolling jaggy and weird. How should I animate the scrolling with easing based on mouse move?

Tahir Ahmed
  • 5,687
  • 2
  • 17
  • 28
gordy
  • 1
  • 1
  • 2

1 Answers1

2

Does this not work for you?

var win=$(window),doc=$(document);
var winWidth=win.width(),winHeight=win.height();
var docWidth=doc.width()-winWidth,docHeight=doc.height()-winHeight;
var increment=0,x=0,y=0;
doc.on('mousemove',function(e){
    increment=docWidth/winWidth;
    x=(e.pageX-win.scrollLeft())*increment;
    increment=docHeight/winHeight;
    y=(e.pageY-win.scrollTop())*increment;
    $('html,body').stop().animate({scrollLeft:x,scrollTop:y});
});
#canvas { width: 3000px; height: 2000px; background: #ededed; }

.content {
    position: absolute;
    border: 3px solid pink;
    width: 300px;
    height: 300px;
    padding: 30px;
}

#island1 { top: 200px; left: 400px; }
#island2 { top: 300px; left: 1700px; }
#island3 { top: 700px; left: 1400px; }
#island4 { top: 1200px; left: 1900px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="canvas">    
    <div class="content" id="island1"><p>Hello world!</p></div>
    <div class="content" id="island2"><p>Hello world!</p></div>
    <div class="content" id="island3"><p>Hello world!</p></div>
    <div class="content" id="island4"><p>Hello world!</p></div>
</div>

Hope it helps in some way.

Update:

Try this:

window.requestAnimFrame = (function () {
    return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (callback) {
        window.setTimeout(callback, 1000 / 60);
    };
})();

var win=$(window),doc=$(document),canvas=$('#canvas');
var winWidth=win.width(),winHeight=win.height();
var docWidth=doc.width()-winWidth,docHeight=doc.height()-winHeight;
var increment=0,x=0,y=0,mouseX=0,mouseY=0,damping=10;

requestAnimFrame(render);

doc.on('mousemove',function(e){
    // see [http://stackoverflow.com/a/23202637/3344111]
    mouseX=e.pageX*(canvas.width()-winWidth)/winWidth;
    mouseY=e.pageY*(canvas.height()-winHeight)/winHeight;
});

function render(){
    requestAnimFrame(render);
    // see [http://stackoverflow.com/a/4847893] & [http://stackoverflow.com/a/4315833]
    x+=(-mouseX-x)/damping;
    y+=(-mouseY-y)/damping;
    canvas.css({'-webkit-transform':'translate('+x+'px,'+y+'px)'});
}
#canvas { width: 3000px; height: 2000px; background: #ededed; }

.content {
    position: absolute;
    border: 3px solid pink;
    width: 300px;
    height: 300px;
    padding: 30px;
}

#island1 { top: 200px; left: 400px; }
#island2 { top: 300px; left: 1700px; }
#island3 { top: 700px; left: 1400px; }
#island4 { top: 1200px; left: 1900px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="canvas">    
    <div class="content" id="island1"><p>island1</p></div>
    <div class="content" id="island2"><p>island2</p></div>
    <div class="content" id="island3"><p>island3</p></div>
    <div class="content" id="island4"><p>island4</p></div>
</div>

Helpful?

Tahir Ahmed
  • 5,687
  • 2
  • 17
  • 28
  • I tried your solution but it seems that the page is scrolled to the corresponding cursor position only after the cursor stops moving. I'm looking for a way to scroll the page with easing immediately once the mouse moves, like this page does for example: http://www.chanel.com/en_WW/fashion.html – gordy Jun 08 '15 at 20:03
  • I have added an **Update** above. – Tahir Ahmed Jun 08 '15 at 21:07