简体   繁体   English

为什么动画无法在我的滑块中正常播放?

[英]why the animation do not work properly in my slider?

I have a jQuery simple slider it has 15 picture each five show in a slide. 我有一个jQuery简单滑块,每张幻灯片中有15张图片,每5张显示一次。 I have a previous button and next button. 我有一个上一个按钮和一个下一个按钮。

Each next click generate a left movement by 855px with a slider animation. 每次单击都会产生一个855px的向左移动, 855px带有滑块动画。 Each previous click generate a right movement by 855px with a slider animation. 每次单击都会产生带有滑块动画的855px

This is my jQuery code : 这是我的jQuery代码:

$(document).ready(function(){
    $(".prev_button").click(function(){
        $("ul.slider").animate({
            left: "+=855"
        }, 3000, function(){
            $("ul.slider").css('left', '0');
            li_no = 0;
            $("ul.slider").find("li").each(function(){
                li_no = li_no + 1;
            });
            slide_after_this = li_no - 6;
            $('ul.slider li:gt('+slide_after_this+')').prependTo('ul.slider'); // << line changed
        });
    });

    $(".next_button").click(function(){
        //alert($("ul.slider").css("right"));
        $("ul.slider").animate({
            right: "+=855"
        }, 3000, function(){
        //alert($("ul.slider").css("right"));
            $("ul.slider").css('right', '0');
            $('ul.slider li:not(:nth-child(n+6))').appendTo('ul.slider');
        });
    });
});

Now I have two problems : 现在我有两个问题:

First one is with the previous button (left arrow) When I click it the animation shows and the elements changed but they do not wrapped with each other (I mean does not show the last elements immidiatly before the first element). 第一个是使用上一个按钮(向左箭头)的,当我单击它时,动画会显示并且元素已更改,但它们之间没有相互包裹(我的意思是没有在最后一个元素之前紧紧显示最后一个元素)。 I can not find the reason of this. 我找不到原因。

Second problem is with both right and left arrows it is like following : 第二个问题是左右箭头,如下所示:

If I click just the right arrow the slider working fine it animates and change the elements but If I click the both button in order (I mean right then left or left then right ) the elements change but the animation does not show. 如果仅单击向右箭头,则滑动器可以正常工作并对其进行动画处理并更改元素,但是如果按顺序单击两个按钮(我的意思是从右向左,然后从左向右,然后从右向右),则元素会发生变化,但动画不会显示。 but I check if the routine go inside the animate function by some alerts and it is going inside but does not animate on the screen . 但是我通过某些警报检查例程是否进入了动画函数,并且它正在进入但不在屏幕上进行动画处理。

This is a link that may help you: 这是一个可以帮助您的链接:

http://jsfiddle.net/mpx83tpv/18/ http://jsfiddle.net/mpx83tpv/18/

you are really close try overflow:hidden for .slider_container 您真的很接近尝试overflow:hidden.slider_container

div.slider_container
{
    height:380px;
    width:855px;
    margin-left:auto;
    margin-right:auto;
    overflow:hidden;
}

edit js: 编辑js:

also use below code as you are using both right and left in the end the slider has both of them one of them is always zero. 也要使用下面的代码,因为在滑块的最后都同时使用左右两个,其中之一始终为零。

$(document).ready(function(){
    $(".prev_button").click(function(){
        $("ul.slider").animate({
            left: "+=855"
        }, 3000);
    });

    $(".next_button").click(function(){
        //alert($("ul.slider").css("right"));
        $("ul.slider").animate({
            left: "-=855"
        }, 3000);
    });
});

if you want a infinite scrolling you need to use right and left in this case replace your $("ul.slider").css('right', '0'); 如果要进行无限滚动,则在这种情况下需要使用向左和向右替换$("ul.slider").css('right', '0'); line to $("ul.slider").css('right', ''); 行到$("ul.slider").css('right', ''); do same for left as well, as you need the remove them. 对左侧也执行相同的操作,因为您需要将其删除。

for adding the next visible div implement you logic before the animation as you callbacks do it after the animation. 用于添加下一个可见div的实现,请在动画之前实现逻辑,因为在动画之后进行回调。

the tricky part would be the prev button for this after calculation of the div count you also need the set new left without animation and then call left move animation. 最棘手的部分是在计算div计数后的prev按钮,您还需要设置没有动画的new left,然后调用left move animation。

hope these make sense. 希望这些有道理。

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

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