Jquery在水平轮播中改变图像宽度

时间:2014-05-28 18:09:42

标签: jquery

我试图将图片轮播更改为其中一个图片,其宽度为500像素,其余为100像素宽,例如可以在这里看到:

http://jsfiddle.net/Uu3aP/3/

只有位于div中间,滑动或不滑动的图像应该有500px。图像应该在div的中间居中时改变。例如,如果你给列表中的id为1/7,那么中间的那个会改变大小,如果它是id 1或其他的

$(document).ready(function() {

var EASING = 0.05,
    FPS = 60,
    $paneTarget = $('#scroll'),
    $paneContainer = $('#scrollContainer'),
    windowWidth = $(window).width(),
    containerWidth = 0,
    maxScroll = 0,
    posX = 0,   // Keep track of the container position with posX
    targetX = 0,
    animInterval = false;   // No interval is set by default

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

// Set maximum amount the container can scroll
// negative because we're gonna scroll to left
maxScroll = -(containerWidth - windowWidth);

// This gets called from the setInterval and handles the animating of the scroll container
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});

    // When end target is reached stop the interval
    if (Math.round(posX) === targetX) {
        clearInterval(animInterval);
        animInterval = false;
    }
}

$paneTarget.on('mousemove', function(event) {
    // Calculate the new x position for the scroll container
    targetX = Math.round((event.pageX / windowWidth) * maxScroll);

    // Only start animation interval when it's not already running
    if (!animInterval) {
        animInterval = setInterval(animationLoop, 1000 / FPS);
    }
});

});

1 个答案:

答案 0 :(得分:0)

将Addclass添加到您的<li>节点之一并更改此类的宽度。

其他HTML

<li class="special"><img  src="http://www.luxuryselfcatering.uk.com/wp-content/uploads/2013/04/placeholder-1-500x350.jpg" alt="" /></li>

其他CSS

#scrollContainer li.special {
    width: 500px;
}

这是http://jsfiddle.net/Uu3aP/14/