JavaScript点小图弹出放大图片的代码

代码简介:

类似于点击小图预览图片的功能,在淘宝网可以见到这种图片展示技巧,当用鼠标点击小图的时候,大图会从小图的位置突然弹出来,而且慢慢放大,至于这么大到什么程序,这个需要由您来设定,实际上本特效还是非常有用的。

代码内容:

View Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JavaScript点小图弹出放大图片的代码 - www.webdm.cn</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<style type="text/css">
*
{margin:0;padding:0;}
img
{border:0;border:1px #ccc solid;}
body
{font-size:12px;background:#fff;}
a
{color:#333;text-decoration:none;}
a:hover
{text-decoration:none;}
.main
{position:relative;width:640px;margin:20px auto;}
.left
{float:left;width:220px;}
/*www.webdm.cn*/
.left li
{float:left;margin-right:8px;list-style:none;}
.right
{float:right;width:400px;}
.right a
{float:left;margin-right:10px;padding:5px 10px;}
.right a:hover
{background:#ff9000;color:#fff;}
.left a:hover
{position:relative;top:1px;left:1px;}
#imglink
{float:none;margin:0;padding:0;}
#links,#more
{margin-top:400px;}
.fxImg
{position:absolute;left:0;top:0;background:#fff;display:none;cursor:pointer;padding:5px;}
</style>
<script type="text/javascript">
ldh
={
    $:
function(o){return typeof o=="string"?document.getElementById(o):o},
    $$:
function (o,p){return (p==undefined?document:this.$(p)).getElementsByTagName(o)},
    on:
function(o,type,fn){o.attachEvent?o.attachEvent('on'+type,function(){fn.call(o)}):o.addEventListener(type,fn,false);return this},
css:
function(who,key){
if(who.style[key]!='')return who.style[key];
if(!!window.ActiveXObject)return who.currentStyle[key];
return document.defaultView.getComputedStyle(who,"")
.getPropertyValue(key.replace(
/([A-Z])/g,"-$1").toLowerCase()); 
    },
    pos:
function(who){
        
var p=ldh.$(who),x=0,y=0;
        
while(p&&!(/html|body/i.test(p.tagName))){x+=p.offsetLeft;y+=p.offsetTop;p=p.offsetParent;};
        
return {'x':x,'y':y};
    },
    move:
function(who,attr,val,s,fn){
        who
=this.$(who);
        
var isbg=/backgroundPosition/i.test(attr)
            ,fm
=parseInt(this.css(who,attr))||0
            ,by
=isbg?((who.getAttribute('bg_y')||0)+'px'):'';
        
if(this.css(who,attr)==undefined&&isbg)fm=200;
        
var iFx=this.fx(fm,val,s);
        clearInterval(who[
'timer_'+attr]);
        who[
'timer_'+attr]=setInterval(function (){
            
var v=iFx();
            who.style[attr]
=v+'px '+by;            
            
if(v==val){    fn&&fn();clearInterval(who['timer_'+attr]);};
        },
18);return this;
    },
    fx:
function(form,to,s){
        
var m=Math[form<to?'ceil':'floor'];
        
return function (){return form+=m((to-form)*(s||0.4))};
    }
};
ldh.on(window,
'load',function (){
    
var fxImg=document.createElement('IMG');
    fxImg.className
='fxImg';
    document.body.appendChild(fxImg);
    
var pos=ldh.pos('preview');
    
var links=ldh.$$('A',ldh.$('menuPic'));
    
for (var i=0;i<links.length;++i ) {
        
var $=links[i],$p=ldh.$$('IMG',$)[0],mypos=ldh.pos($p),url=$.href
        ,attr
={href:'javascript:void(0);',imgsrc:$p.src,x:mypos.x,y:mypos.y,'url':url};
        
for (var Id in attr){$[Id]=attr[Id]}
        ldh.on($,
'click',function (){
            fxImg.src
=this.imgsrc;
            
var $$=fxImg.style,This=this;
            $$.left
=this.x+'px';
            $$.top
=this.y+'px';
            $$.display
='block';    
            $$.height
='140px';
            fxImg.onclick
=function (){location=This.url};
            ldh.move(fxImg,
'left',pos.x).move(fxImg,'top',pos.y,0.4,function(){ldh.move(fxImg,'height',400)});
        });
    }
})
</script>
</head>
<body>
<div class="main">
    
<div class="left" id="menuPic">
        
<ul>
            
<li><href="http://www.webdm.cn" ><img src="http://www.webdm.cn/images/wall1_s.jpg" /></a></li>
            
<li><href="http://www.webdm.cn"><img src="http://www.webdm.cn/images/wall2_s.jpg" /></a></li>
            
<li><href="http://www.webdm.cn"><img src="http://www.webdm.cn/images/wall3_s.jpg" /></a></li>
        
</ul>
    
</div>
    
</div>
</body>
</html>
<br />
<p><href="http://www.webdm.cn">网页代码站</a> - 最专业的网页代码下载网站 - 致力为中国站长提供有质量的网页代码!</p>
代码来自:http://www.webdm.cn/webcode/8f9b3ef7-bca1-42ef-b0af-ac43e83e9fc6.html
原文地址:https://www.cnblogs.com/webdm/p/2165359.html