據說,這是一個最近很流行的效果,新浪微博,騰訊微博,都用到了這種效果,本博開博時也追隨潮流用上了這種效果,使用方法:
在你的大圖鏈接中加入class=”miniImg artZoom”例:
<a class="miniImg artZoom" href="http://www.amostoys.com/images/460/graphics/hey.jpg"><img title="Mr.Think" src="http://www.amostoys.com/images/460/graphics/hey.jpg" alt="Zoom" /></a>
核心jQuery代碼:
- //原作者:唐斌www.planeart.cn/,由Mr.Think整理分享.感謝唐斌同學:)
- $(function(){
- var imgID = 0;
- $('a.artZoom').click(function(){
- var id = $(this).attr('rel');
- if(id == ''){
- id = imgID += 1;
- $(this).attr('id','artZoomLink-' +id);
- $(this).attr('rel',id)
- };
- var url = $(this).attr('href');
- $(this).append('<div class="loading" title="loading.."></div>');//loading
- function getImageSize(url,fn){
- var img = new Image();
- img.src = url;
- if (img.complete){
- fn.call(img);
- }else{
- img.onload = function(){
- fn.call(img);
- };
- };
- };
- getImageSize(url,function(){
- $('#artZoomLink-' +id+ ' .loading').remove();
- $('#artZoomLink-' +id).hide();
- if (id != '' && $('#artZoomBox-' +id).length == 0){
- var html = '';
- html += '<div class="artZoomBox" id="artZoomBox-' +id+ '" style="display:none">';
- html += ' <div class="tool"><a class="hideImg" href="#" rel="' +id+ '">收起</a><a class="imgLeft" href="#" rel="' +id+ '">向左轉</a><a class="imgRight" href="#" rel="' +id+ '">向右轉</a><a class="viewImg" href="#" rel="' +id+ '">新窗口打開</a></div>';
- html += ' <a href="' +url+ '" class="maxImgLink" id="maxImgLink-' +id+ '" rel="' +id+ '"> <img class="maxImg" width="' +this.width+ '" height="' +this.height+ '" id="maxImg-' +id+ '" src="' +url+ '" /> </a>';
- html += '</div>';
- $('#artZoomLink-' +id).after(html);
- };
- $('#artZoomBox-' +id).show('fast');
- });
- return false;
- });
- //讓IE8在圖片旋轉後高度能被包含
- function IE8height(id){
- var w = $('#maxImg-' +id).outerWidth();
- var h = $('#maxImg-' +id).outerHeight();
- $('#artZoomBox-' +id+ ' a.maxImgLink').css('height','');
- if ($.browser.version == '8.0' && w > h) {
- var maxHeight = Math.max(w, h);
- $('#artZoomBox-' +id+ ' a.maxImgLink').css('height',maxHeight+ 'px');
- };
- };
- $('.artZoomBox a').live('click', function(){
- var id = $(this).attr('rel');
- //收起
- if($(this).attr('class') == 'hideImg' || $(this).attr('class') == 'maxImgLink') {
- $('#artZoomBox-' +id).hide('fast',function(){
- $('#artZoomLink-' +id).show();
- });
- };
- //左旋轉
- if($(this).attr('class') == 'imgLeft') {
- IE8height(id);
- $('#maxImg-' +id).rotateLeft(90);
- };
- //右旋轉
- if($(this).attr('class') == 'imgRight') {
- IE8height(id);
- $('#maxImg-' +id).rotateRight(90);
- };
- //新窗口打開
- if($(this).attr('class') == 'viewImg') window.open($('#maxImgLink-' +id).attr('href'));
- return false;
- });
- });
- jQuery.fn.rotate = function(an