简体   繁体   English

有关如何修复内容滑块的上一个和下一个按钮的建议

[英]advice on how to fix the prev and next buttons of content slider

I'm trying to add in previous and next buttons to my content slider but seem to be having problems, what I would really like to do is move the $slideCtn left or right by the width slideWidth each time the previous or next button is clicked but i'm unsure how to increment each click by the value of slideWidth. 我正在尝试在内容滑块中添加上一个和下一个按钮,但是似乎有问题,我真正想做的是每次单击上一个或下一个按钮时,将$ slideCtn左右移动widthSlideWidth但是我不确定如何通过slideWidth的值增加每次点击。 I've tried ++ and -- etc but with no results, would anyone be able to show me the best way to do something like this? 我已经尝试过++和-等,但是没有结果,有人能向我展示执行此类操作的最佳方法吗? Also any other advice very welcome! 也有任何其他建议非常欢迎!

Or should I create a global index variable that gets set at 0, then as the pagination x's are clicked or the prev/next arrows are clicked update this global variable? 还是应该创建一个设置为0的全局索引变量,然后在单击分页x或单击上一个/下一个箭头时更新此全局变量?

JS Snippet JS代码段

//Add previous + next arrows
$dirArrows.on('click', function(e){

    var arrDir = $(this).data('dir');

    $slideCtn.css('left', ( arrDir === 'prev' ) ? -(slideWidth) : +(slideWidth));

    e.preventDefault();
});

JS Fiddle http://jsfiddle.net/SG5ad/10/ JS小提琴 http://jsfiddle.net/SG5ad/10/

At the moment, you're telling it to move to an absolute position - either -200px or +200px (never 0px, 400px, 600px, etc). 目前,您是在告诉它移动到绝对位置--200px或+ 200px(永远不要为0px,400px,600px等)。

You'll need to take into account its current position as well as how much you want to adjust it: http://jsfiddle.net/SG5ad/12/ 您需要考虑其当前位置以及要调整的位置: http : //jsfiddle.net/SG5ad/12/

var arrDir = $(this).data('dir')
    iLeft = parseInt( $slideCtn.css('left') );

$slideCtn.css('left', ( arrDir === 'prev' ) ? iLeft - slideWidth : iLeft + slideWidth);

A bug you'll want to fix as well is that the Next/Prev buttons do nothing until you've already jumped to a specific slide with the "x" navigation. 您还需要修复的一个错误是,“下一个/上一个”按钮将不执行任何操作,直到您已经通过“ x”导航跳到特定的幻灯片。


As an entirely separate issue, about 6 months ago I wrote something like this as part of a project at work (it had a few more bells and whistles, but nothing drastically different), and there's one important thing I'd say is worth changing. 作为一个完全独立的问题,大约六个月前,我在工作中的项目中写了这样的内容(它有许多风吹草动,但没有太大的不同),我想说的一件重要的事情值得改变。

In order to go from slide a to slide d at the moment, you animate slides a , b , c and d , which means that 为了此时从幻灯片a转到幻灯片d ,请对幻灯片abcd动画处理,这意味着
a) 4 slides are animating instead of 1 (plus all their child elements) a)4张幻灯片正在制作动画,而不是1张(加上其所有子元素)
b) you have to pass through slides b and c even though they're not relevant b)即使幻灯片bc不相关,也必须通过它们

I'd have a look at changing the base position of all your slides to be stacked on top of each other using z-index, then simply animating the top slide off to one side to reveal the one underneath it. 我将看看如何使用z-index将所有幻灯片的基本位置更改为彼此堆叠,然后简单地将顶部幻灯片动画化为一侧,以露出其下方的一侧。 It requires a bit of code to track which slides are where ( $.data() may help there) but gives you a much more performant slider at the end of it. 它需要一些代码来跟踪哪些幻灯片在哪里( $.data()可能对您有所帮助),但在结尾处为您提供了性能更高的滑块。

You've gotta do it with .animate() 您必须使用.animate()

$dirArrows.on('click', function(e){

    var arrDir = $(this).data('dir');

    if (arrdir == left){
       $slideCtn.animate({
         left: "+=250"
       });
    }
});

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

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