简体   繁体   English

放大一张图片onclick

[英]Enlarge one pic onclick

I'm making a website with images on it, and if you click on one of the images it should enlarge. 我正在制作一个上面有图片的网站,如果您单击其中一张图片,它应该会放大。 I did that by using the toggleClass function in jquery 我通过在jquery中使用toggleClass函数来做到这一点

I enlarged the selected image like so: 我将所选图像放大如下:

$(".img1, .img2").on('click',function(){
    $(this).toggleClass('enlarged');
});

the used class: 使用的类:

.enlarged{
position:absolute;
z-index:2;
width:500px;
height:600px;
top:-10%;
left:300px;
}

it's hard to explain but right now what happens is, when you click an image it enlarges. 很难解释,但是现在发生的是,当您单击图像放大时。 when you click another image, it enlarges too but overlaps/stays hidden behind the other image that's enlarged. 当您单击另一张图像时,它也会放大,但是在放大的另一张图像后面会重叠/保持隐藏状态。

What I would like to happen is when img1 is enlarged and the user selects img2, it should "close" img1 and enlarge img2. 我想发生的是,当img1放大并且用户选择img2时,它应该“关闭” img1并放大img2。

Thank you :) 谢谢 :)

UPDATE 更新

it now works barely, it opens after spamclicking and once enlarged I can minimize it again. 它现在几乎无法工作,在点击spamclick之后会打开,一旦放大,我可以再次将其最小化。 but then I can't enlarge it anymore. 但后来我不能再扩大了。

Can anybody help me with this? 有人可以帮我吗?

<script>
    $(document).ready(function() {
        $("#header").load("header.html .header");
        $("#footer").load("footer.html .footer");

        $("body").on('click', function(){

            if(!$(".img1, .img2").hasClass('enlarged')){

                $(".img1, .img2").on('click',function(){
                    $(this).addClass('enlarged');
                });
            }else{
                $("body").on('click', '.enlarged', function(){
                    $(this).removeClass('enlarged');
                });
            }

        });
    });
</script>

Add a generic class to all clickable images, like 'gallery' 向所有可点击图片添加通用类,例如“图库”

then 然后

$(".gallery").on('click',function(){
    //removes enlarged class from all images
    $(".gallery.enlarged").removeClass('enlarged');
    //adds enlarged class to clicked image
    $(this).addClass('enlarged');
});

You could do something like the following. 您可以执行以下操作。 This will remove the enlarged class. 这将删除enlarged类别。 Then add it to the image that you have clicked on. 然后将其添加到您单击的图像。 (See jsfiddle here ) 在这里查看jsfiddle

$(".img1, .img2").on('click', function() {

  $(".img1, .img2").removeClass("enlarged");
  $(this).toggleClass('enlarged');

});

See working example below: 请参见下面的工作示例:

 $(".img1, .img2").on('click', function() { $(".img1, .img2").removeClass("enlarged"); $(this).toggleClass('enlarged'); }); 
 .enlarged { position: absolute; z-index: 2; width: 500px; height: 600px; top: -10%; left: 300px; } label { font-weight: bold; } input[type=text] { width: 20em } p { margin: 1em 0 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <img class="img1" src="https://placehold.it/350x150" alt="image" /> <img class="img2" src="https://placehold.it/350x150/F2F5A9" alt="image" /> 

If you want to have only one "enlarged" class for the img you click on, I think the simpliest is to remove all "enlarged" class then add the class for the right element : 如果您只希望为单击的img提供一个“放大”类,那么我认为最简单的方法是删除所有“放大”类,然后为正确的元素添加该类:

$(".img1, .img2").on('click',function(){
    $(".img1, .img2").removeClass('enlarged');
    $(this).addClass('enlarged');
});

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

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