简体   繁体   English

jQuery Animate和定位元素

[英]jQuery Animate and positioning elements

I current have a div that has around 5 images floating left so they are side by side. 我目前有一个div,左边有5张左右的图像,所以它们是并排的。 Now i have this div inside a parent div with overflow:hidden and the width so that it can only fit 4 images in. 现在我在父div中有这个div,溢出:隐藏和宽度,以便它只能容纳4个图像。

Everything works fine apart from the animation, i have two arrows for left and right, but lets say i press right twice it moves across as it would normally but then when i press left once it resets the divs normal position and continues, 除了动画之外,一切都很好,我左右两个箭头,但是让我说我​​按两次它会像正常情况一样向右移动然后当我按下左键一旦它重置div正常位置并继续,

$(".next").click(function(){
    $(".item_wrap").animate({right: "+=118px"}, 1500 );
    return false;
});
$(".prev").click(function(){
    $(".item_wrap").animate({left: "+=118px"}, 1500 );
    return false;
});

this is the code i am currently using. 这是我目前正在使用的代码。

...but lets say i press right twice it moves across as it would normally but then when i press left once it resets the divs normal position and continues ...但是让我说我​​向右按两次它会像往常一样移动但是当我按下它一旦它重置div正常位置并继续

Well that's because you can't animate both left and right on the same element unless you actually want the element to be stretching (it won't stretch in your case though since you've probably set a static element width.) Instead you animate just one of them by adding and subtracting: 嗯,这是因为你不能既动画leftright相同元素上,除非你真正想要的元素被拉伸(它不会在你的情况舒展虽然因为你可能已经设置静态元素宽度)。相反,你动画只需添加和减去其中一个:

$(".next").click(function(){
    $(".item_wrap").animate({right: "+=118px"}, 1500 );
    return false;
});
$(".prev").click(function(){
    // Subtract from 'right' instead of adding to 'left'
    $(".item_wrap").animate({right: "-=118px"}, 1500 );
    return false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM