简体   繁体   English

从头开始制作jquery旋转木马

[英]Making a jquery carousel from scratch

I know there are a ton of questions on here about jquery image carousel, but they all refer to a plugin. 我知道这里有很多关于jquery图像轮播的问题,但它们都引用了一个插件。 I would like to make one from scratch. 我想从头做一个。 It's a pretty simple one, there are 2 buttons, one left and one right. 这是一个非常简单的,有2个按钮,一个左,一个右。 when you click the left button, the position of the overall container that has all the images shifts to the left, and the right makes it go right. 当您单击左按钮时,包含所有图像的整个容器的位置向左移动,右侧使其向右移动。

This is what I have so far... Right now the problem is only the left button works. 这就是我到目前为止......现在问题只是左键有效。 (the right button works only once you slide the image to the left once) And I also want it to animate across all the images, and go to the last image when you get to the end of the set of images (右侧按钮仅在您将图像向左滑动一次后才起作用)我还希望它在所有图像上设置动画,并在到达图像集末尾时转到最后一个图像

JS: JS:

total_entries = $("image-entry").length;
var current_index = 0;
var slider_entries = $('#slider-entries');

$('#home-slider #left').click(function(){
    go_to_index(current_index-1);
    return false;
});

$('#home-slider #right').click(function(){
    go_to_index(current_index+1);
    return false;
});



var go_to_index = function(index){
    if(index < 0)
        index = total_entries - 1;
    if(index > total_entries - 1)
        index = 0;
    if(current_index == index)
        return;



    var left_offset = -1 * index * 720;
    slider_entries.stop().animate({"left": left_offset}, 250);
    //description_container.stop().animate({"left":left_offset}, 250);
    current_index = index;
};

HTML: HTML:

<div id="slider">
    <div id="slider-entries">
        <div class="image-entry">
            <img src="http://placekitten.com/720/230" />
        </div>
        <div class="image-entry">
            <img src="http://placedog.com/720/230" />
        </div>
        <div class="image-entry">
            <img src="http://placedog.com/720/230" />
        </div>
    </div>
</div>

total width of each image is 720px total with of slider-entries is 每个图像的总宽度为720px,滑块条目为

Thanks 谢谢

You have a problem with the total_entries variable. 你有一个total_entries变量的问题。 First of all you need a "var" in front, to define that it's a new variable. 首先,你需要一个“var”,以定义它是一个新的变量。 Second, you forgot a "." 其次,你忘了“。” (dot) to search for the class in your HTML code.. (点)在HTML代码中搜索类。

your first line should be: 你的第一行应该是:

var total_entries = $(".image-entry").length;

Hope it works ;-) 希望它有效;-)

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

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