简体   繁体   English

jQuery放大和缩小单击图像

[英]jquery zoom in and zoom out on clicking an image

I have an image which has to behave the following way: When a user clicks on it the first time it the image should zoom-in and when clicked on the image again it should zoom-out. 我有一个图像,该图像必须具有以下行为:用户第一次单击它时,图像应放大;再次单击该图像时,则应放大。

Please help in figuring out how the two actions be performed by clicking on the image using jquery? 请通过使用jquery单击图片来帮助弄清楚如何执行这两个动作? Below is the code I have written for zooming in the image. 以下是我编写的用于放大图像的代码。

$('.tab1').click(function()
       {
           $(".GraphHolder").hide("slow");
           $("#top-button-container").hide("slow");
           $(".print").append('<button type="button">PRINT</button>');
          $(this).animate({width: "70%"}, 'slow');
       });

HTML: HTML:

<img id="pic" src="https://www.google.com//images/icons/product/chrome-48.png">

Jquery: jQuery:

$(function(){
    $('#pic').toggle(
          function() { $(this).animate({width: "100%"}, 500)},
           function() { $(this).animate({width: "50px"}, 500); }
    );
});  

Example at: http://jsfiddle.net/K9ASw/32/ 示例位于: http : //jsfiddle.net/K9ASw/32/

Have you tried using toggleClass ? 您是否尝试过使用toggleClass You can use the optional [duration] parameter to specify how long you want the transition to take. 您可以使用可选的[duration]参​​数来指定过渡要花费多长时间。

If really old or crappy browsers are'nt an issue, I'd go for CSS transitions, easier and smoother. 如果真的不是旧的或糟糕的浏览器不是问题,那么我会去CSS过渡,更轻松,更流畅。

FIDDLE 小提琴

   **here is the script**
   <script src="Script/jquery-1.7.1.min.js" type="text/javascript"></script>
  <script type="text/javascript">
    $(document).ready(function () {
        $('#Img1, #Img2, #Img3').mouseover(function () {
            $(this).animate({ width: "122px", height: "110px" }, 100);
        });
        $('#Img1, #Img2, #Img3').mouseout(function () {
            $(this).animate({ width: "118px", height: "106px" }, 100);
        });
    });
  </script>

 **Aspx code**
<img src="Images/home.jpg" id="Img1" width="118" height="106" border="0">
<img src="Images/machine.jpg" id="Img2" width="118" height="106" border="0">
<img src="Images/title_Mixie.jpg" id="Img3" width="118" height="106" border="0">
$(function () {
 $('.tab1').on('click', function()
   {
     if($(this).hasClass('zoomed')) {
       $(this).removeClass('zoomed')
       $(this).children('img').animate({width: "10%"}, 'slow');
     }
       $(this).addClass('zoomed')
       $(this).children('img').animate({width: "70%"}, 'slow');
     }
 });
});

I removed all the code not pertaining to the image resize. 我删除了所有与图像调整大小无关的代码。 Also, if you're dealing with an image, you have to target the image tag, not the outer div. 另外,如果要处理图片,则必须定位图片标签,而不是外部div。 If the image is applied to the .tab1 class as a background image then you're sol. 如果将图像作为背景图像应用于.tab1类,那么您就很容易。

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

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