2012-04-22 76 views
-3

我有一些缩略图下面的代码:关于jQuery的图像缩放效果

<img onclick="if($(this).hasClass('zoom'{ 
     this.src='http://24.media.tumblr.com/tumblr_m2uql6rJsI1r9fv99o1_100.gif'; 
     $(this).animate({'width':'100','height':'50'}).removeClass('zoom') 
    }else{ 
     this.src='http://27.media.tumblr.com/tumblr_m2uql6rJsI1r9fv99o1_500.gif'; 
     $(this).animate({'width':'500','height':'268'}).addClass('zoom') 
    }" 
    style="width: 100px; height: 50px;" 
    src="http://24.media.tumblr.com/tumblr_m2uql6rJsI1r9fv99o1_100.gif" > 

它的工作原理,但我不希望这种风格,我的网站,我想是这样的:

<script type="text/javascript">#$^%@^&*$#$^#@%</script> 

但太复杂了,我不能写......所以谁能帮助我?

SOURCE在这里:http://jsfiddle.net/My39T/

的另一个问题是,我需要动画,所以这些图像需要的宽度/高度。我能做到这一点,但我没有图像宽度/高度,所以也许需要这样的:

$('img').each(function() { 
$(this).attr('height',$(this).height()); 
$(this).attr('width',$(this).width()); 
}); 
+1

你的问题不清楚,这是什么意思#$ ^%@ ^&* $#$ ^#@%?你想要哪种风格? – Peeyush 2012-04-22 06:32:17

+0

@Peeyush:我认为一串无法辨认的人物是他不懂JavaScript的沟通方式。 – 2012-04-22 06:47:55

+0

#$ ^%@ ^&* $#$ ^#@%===== 有些像这样 – metalbug 2012-04-22 07:36:15

回答

3

我想这你想要做什么,而是因为你没有清楚地说明你想什么发生,这是基于阅读在线onclick处理程序的最佳猜测。

$(function() { 
    $(".thumb > li > a").click(function() { 
     var that = $(this), 
      img = that.find('img'), 
      src = that.href, 
      h = img.height(), 
      w = img.width(); 

     img.attr({ 
      'height' : h, 
      'width' : w, 
      'src' : src 
     }); 
     if (that.hasClass('zoom')){ 
      img.animate(
       { 
        'height' : 67, 
        'width' : 100 
       },1000); 
     } 
     else { 
      img.animate(
       { 
        'height' : 268, 
        'width' : 500 
       },1000); 
     } 
     that.toggleClass('zoom'); 
     return false; 
    }); 
});​ 

JS Fiddle demo

+0

是的,谢谢你,但你的源只是放大这个拇指图片,而不是缩小到这个大图片... – metalbug 2012-04-22 07:08:29

+0

这是非常奇怪的,因为右键单击(在Chrome/Firebug中)和'检查元素'显示' src'已更新为新值。 =/ – 2012-04-22 07:14:32

+0

好吧,这看起来很奇怪。如果你在其他地方将[src]设置为另一个图像的字符串,[它可以工作](http://jsfiddle.net/davidThomas/My39T/3/)。如果将'a'的'href'更改为指向同一图像(如前例)[失败](http://jsfiddle.net/davidThomas/My39T/4/)。我不知道为什么。 – 2012-04-22 07:27:59