简体   繁体   English

照片库的Javascript功能

[英]Javascript function for photo gallery

I'm building a static page where I have an image and 2 arrows (one on the left of the image and one on the right). 我正在构建一个静态页面,其中有一个图像和2个箭头(一个在图像左侧,一个在右侧)。 By clicking the arrow another image would appear, like in a photo gallery. 通过单击箭头,将出现另一个图像,例如在照片库中。 There's only 4 images in total, which I would keep in the site folder on my computer. 总共只有4张图片,我会将该图片保存在计算机的站点文件夹中。 Basically the arrows should allow the user to cycle between the images, by pressing the right arrow or the left arrow. 基本上,箭头应允许用户通过按右箭头或左箭头在图像之间循环。

I have an array of images: 我有一系列图像:

var imageArray = new Array();
imageArray[0] = new Image(); imageArray[0].src = 'image1.png';
imageArray[1] = new Image(); imageArray[1].src = 'image2.png';
imageArray[2] = new Image(); imgArray[2].src = 'image3.png';
imageArray[3] = new Image(); imageArray[3].src = 'image4.png';

And the two arrows: 和两个箭头:

<div id="apDiv1"style="display:none"><img src="arrow left.png" width="125" height="74" onClick="SOMETHING">/div>

<div id="apDiv2"style="display:none"><img src="arrow right.png" width="125" height="74" onClick="SOMETHING">/div

I want to make onClick to call a function that shows the next (or previous) image. 我想让onClick调用一个显示下一个(或上一个)图像的函数。

How can I do this? 我怎样才能做到这一点? I though of a sort of toggle function but that would work poorly. 我虽然具有某种切换功能,但效果不佳。

And also, can I set up a starting image like: 另外,我可以设置一个起始图像吗?

<div id="apDiv3"style="display:none"><img src="image1.png" width="200" height="200">/div>

TO Move Forward (Forward Click) 前进(前进点击)

css: CSS:

.show {
    display: block;
}

.hide {
    display: none;
}

Now, suppose you have a parent div with all image divs like this 现在,假设您有一个包含所有图像div父div,如下所示

<div class='parent'>
    <div id='image-1' class='show'><img src="image1.png" width="200" height="200"></div>
    <div id='image-2' class='hide'><img src="image2.png" width="200" height="200"></div>
    <div id='image-3' class='hide'><img src="image3.png" width="200" height="200"></div>
    //and so on...
</div>

Javascript code: JavaScript代码:

onclick: function() {
    var images = $('.parent').find('div');
    for(var i = 0; i< images.length; i++) {
        if($(images[i]).hasClass('show')) {
            $(images[i]).removeClass('show');
            $(images[i]).addClass('hide');
            if(i === images.length -1) {
                $(images[0]).removeClass('hide');
                $(images[0]).addClass('show');
            } else {
                $(images[0]).removeClass('show');
                $(images[0]).addClass('hide');
            }
        }
    }
}

Similarly, implement for back arrow click 同样,实现后退箭头单击

Hope this helps 希望这可以帮助

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

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