Question

I'm attempting to create a horizontal scroller simliar to this. http://www.k2.pl/

I made a prototype here. http://jsfiddle.net/Uu3aP/1

I looked into the carousel plugins but none function how I would like.

I'm just curious how I perform speed calculation based on the x position to the left and right and maybe use an easing method for the sides.

This is where I am currently at, any assistance would be appreciated.

HTML

<div id="scroll">
<div id="scrollContainer">
<ul>
    <li><img src="http://placehold.it/350x350" alt="" /></li>
    <li><img src="http://placehold.it/350x350" alt="" /></li>
    <li><img src="http://placehold.it/350x350" alt="" /></li>
    <li><img src="http://placehold.it/350x350" alt="" /></li>
    <li><img src="http://placehold.it/350x350" alt="" /></li>
</ul>
</div>

CSS

body {
    margin: 0;
}
#scroll {
    width: 100%;
    overflow: hidden;
}
#scrollContainer * {
    float: left;
    list-style: none;
    margin: 0;
    padding: 0;
}

JAVASCRIPT

$(document).ready(function() {

    var $totalwidth = 0;
    var $paneTarget = $('#scroll');
    var $paneContainer = $('#scrollContainer');
    var $this = $(this);
    var $w = $this.width();

    $paneTarget.find('li').each(function() {
        $totalwidth += $this.width();
        $paneContainer.width($totalwidth);
    });

    $paneTarget.on('mousemove', function(e) {
        if ((e.pageX) < $w / 2) {
            $paneTarget.stop().scrollTo( {top:'0', left:'-=100'}, 200, {easing: 'linear'});
        } else if ((e.pageX) > ($w / 2)) {
            $paneTarget.stop().scrollTo( {top:'0', left:'+=100'}, 200, {easing: 'linear'});
        }
        else {
            $paneTarget.stop();
        }
    });

});
Was it helpful?

Solution

I played a bit with your fiddle and came up with this: Fiddle

Instead of using the scrollTo plugin or jQuery's animate, I created a custom animation method that gets called with setInterval.

// This gets called from the setInterval and handles the animating
function animationLoop() {
    var dx = targetX - posX,    // Difference
        vx = dx * EASING;       // Velocity

    // Add velocity to x position and update css with new position
    posX += vx;
    $paneContainer.css({left: posX});             
}

The targetX gets updated in the mousemove handler

// Calculate the new x position for the scroll container
targetX = Math.round((event.pageX / windowWidth) * maxScroll);

[Edit]
To handle window resize you basically only have to reset the windowWidth and maxScroll variables:

$(window).on('resize', function() {
    windowWidth = $(window).width();
    maxScroll = -(containerWidth - windowWidth);

    // OPTIONAL:
    // Move scrollpane to original position on resize
    targetX = 0;
    // Start animating if it's not already
    // Probably better in a new function: duplicate code from mousemove..
    if (!animInterval) {
        animInterval = setInterval(animationLoop, 1000 / FPS);
    }
});

Updated jsfiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top