两个学习型的网页图片代码

代码如下

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>鼠标移过,改变图片路径</title>
<style>
body,ul,li{margin:0;padding:0;}
body{background:#000;}
img{border:0;border-radius:3px;}
ul{list-style-type:none;}
#box{212px;border:5px solid #fff;overflow:hidden;zoom:1;background:#fff;border-radius:5px;margin:10px auto;padding:0 0 3px 3px;}
#box li,#box li img{float:left;50px;height:50px;}
#box li{margin:3px 3px 0 0;}
#box li.first{position:relative;}
#box li.first,#box li.first img{156px;height:156px;}
#box li.first div{position:absolute;top:0;left:0;156px;height:156px;display:none;opacity:0.5;filter:alpha(opacity=50);background:#fff url(img/loading.gif) 50% 50% no-repeat;}
</style>
<script type="text/javascript">
window.onload = function ()
{
    var oImg = document.getElementById("box").getElementsByTagName("img");
    var oDiv = document.getElementsByTagName("div")[0];
    for (var i = 1; i < oImg.length; i++)
    {
        oImg[i].onmouseover = function ()
        {                        
            var img = new Image();
            img.src = oImg[0].src = this.src.replace(/small/,"big");
            oDiv.style.display = "block";
            img.complete ? oDiv.style.display = "none" : (oImg[0].onload = function() {oDiv.style.display = "none"})
        }    
    }
};
</script>
</head>
<body>
<ul id="box">
    <li class="first"><img src="img/big_1.jpg"><div></div></li>
    <li><a href="javascript:;"><img src="img/small_1.jpg"></a></li>
    <li><a href="javascript:;"><img src="img/small_2.jpg"></a></li>
    <li><a href="javascript:;"><img src="img/small_3.jpg"></a></li>
    <li><a href="javascript:;"><img src="img/small_4.jpg"></a></li>
    <li><a href="javascript:;"><img src="img/small_5.jpg"></a></li>
    <li><a href="javascript:;"><img src="img/small_6.jpg"></a></li>
    <li><a href="javascript:;"><img src="img/small_7.jpg"></a></li>
    <li><a href="javascript:;"><img src="img/small_8.jpg"></a></li>
    <li><a href="javascript:;"><img src="img/small_9.jpg"></a></li>
    <li><a href="javascript:;"><img src="img/small_10.jpg"></a></li>
    <li><a href="javascript:;"><img src="img/small_11.jpg"></a></li>
</ul>
</body>
</html>

另外使用JQ代码的图片查看器

<!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>jquery 图片展示</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
    <style type="text/css">
            /*影像显示CSS*/
        #xzy_zoom{
            border: 10px solid #777;
            350px;
            height:300px;
            display: none;
            top:0;left:0;
            overflow: hidden;
            position: absolute;
            box-shadow:1px 0 6px 0 rgba(0, 0, 0, 0.6);
            background-color: #e5e5e5;
            cursor: move;
            z-index: 1000
        }
        #xzy_zoom_title{
            position: absolute;
            z-index: 1002;
            height: 22px;
            overflow: hidden;
            border-bottom: 1px solid #ddd;
            line-height: 22px;
            350px;
            font-size: 12px;
            font-family: "宋体";
            padding-left: 4px;
            background-color: #125fad;
            color: #fff
        }
        #xzy_zoom_title ul{
            float: right;margin: 0;padding: 0;
        }
        #xzy_zoom_title ul li{
            margin: 0;padding: 0;
            float: left;
            list-style: none;
            height:22px;
             20px;
            cursor: pointer;
            background-image: url("zoom_close.gif");
            background-repeat: no-repeat;
            background-position: center 0;
        }
        #xzy_zoom_title #xzy_zoom_close{background-position: center -36px}
        #xzy_zoom_title #xzy_zoom_right{background-position: center -18px}
        #xzy_zoom_pic{
            position: absolute;
            z-index: 1001;
            cursor: move;
            border: 1px solid #ddd;
        }
        .zoom{
            150px;
            float: left;
        }
        .zoom_pic{
            110px;
            height:80px;
            margin:10px 20px;
            overflow: hidden;
            cursor: pointer;
            border: 1px solid #ddd;
            text-align: center
        }
        .zoom_pic:hover{border: 1px solid #f93;}
        .zoom_name{
            text-align: center;
            font-size: 12px;
            font-weight: bold;
            color: #069
        }
    </style>
</head>
<body>
<div style="margin:50px auto">
    <div class="zoom"><div class="zoom_pic"><img src="http://www.scscms.com/FUploadFile/image/2010-9/Kerem_6.jpg" class="z_pic" width="110" alt="名称"/> </div><div class="zoom_name">名称</div></div>
    <div class="zoom"><div class="zoom_pic"><img src="http://www.scscms.com/FUploadFile/image/2010-9/kc1.jpg" class="z_pic" width="110" alt="孔雀"/> </div><div class="zoom_name">孔雀</div></div>
    <div class="zoom"><div class="zoom_pic"><img src="http://www.scscms.com/FUploadFile/image/2010-9/gc2.jpg" class="z_pic" width="110" alt="国庆"/> </div><div class="zoom_name">国庆</div></div>
    <div class="zoom"><div class="zoom_pic"><img src="http://www.scscms.com/FUploadFile/image/2010-9/2008781032576_2.jpg" class="z_pic" width="110" alt="可爱"/> </div><div class="zoom_name">可爱</div></div>
    <div class="zoom"><div class="zoom_pic"><img src="http://www.scscms.com/FUploadFile/image/2010-9/2988027_140201300113_2.jpg" class="z_pic" width="110" alt="蝴蝶"/> </div><div class="zoom_name">蝴蝶</div></div>
</div>
<script type="text/javascript">
    $(document).ready(function() {
        setTimeout(function(){
            $.each($(".z_pic"),function(){
                var i=$(this).height();
                if(i>80){
                    $(this).css({height:"80px","auto"});
                }else{
                    $(this).css({marginTop:parseInt(40-i/2)+"px"});
                }
            });
        },1000);
        /*弹出div的宽,高,缩放倍数,最小的缩放尺寸*/
        $(".z_pic").xzy_zoom(600,500,5,200);
    });
    jQuery.fn.extend({
        xzy_zoom:function (w,h,unit,min){
            w=w||300;
            h=h||300;
            unit=unit||5;
            min=min||200;
            var g_l,g_t,g_id="xzy_zoom_pic";
            $('body').append('<div id="xzy_zoom"><div id="xzy_zoom_title"><ul><li id="xzy_zoom_left" title="左转90度"></li><li id="xzy_zoom_right" title="右转90度"></li><li id="xzy_zoom_close" title="点击关闭"></li></ul><span></span></div><img src="" id="'+g_id+'" /></div>');
            $("#xzy_zoom_title").width(w-4);
            $("#xzy_zoom").css({height:h-22,w});
            $(this).click(function(){
                rotate_pic("restore");
                var url=$(this).attr("src");
                var t=$(this).offset().top;
                var img = new Image();
                img.onload=function(){
                    g_l=Math.floor((w-this.width)/2);
                    g_t=Math.floor((h-this.height)/2);
                    $("#"+g_id).attr("src",url).css({"auto",height:"auto",left:g_l,top:g_t,marginLeft:0,marginTop:0});
                    $("#xzy_zoom").css({display:"block",left:Math.floor($(window).width()/2-w/2),top:t});
                };
                img.src=url;
                $("#xzy_zoom_title span").html($(this).attr("alt"));
            });
            function move_div(id){
                $(id).mousedown(function (e) {
                    var oe=e||window.event;
                    var $this = document.getElementById($(this).attr("id"));
                    var startX = oe.clientX - $this.offsetLeft+parseFloat($(this).css("marginLeft"));
                    var startY = oe.clientY - $this.offsetTop+parseFloat($(this).css("marginTop"));
                    document.onmousemove = function (e) {
                        var oe = e || window.event;
                        $this.style.left = oe.clientX - startX + "px";
                        $this.style.top = oe.clientY - startY + "px";
                    };
                    document.onmouseup = function () {
                        document.onmousemove = document.onmouseup = null;
                        if ($this.releaseCapture)$this.releaseCapture();
                    };
                    if ($this.setCapture)$this.setCapture();
                    return false;
                });
            }
            function scrollMouse(e) {
                var direct = 0;
                e = e || window.event;
                if (e.wheelDelta) {
                    direct = e.wheelDelta;
                    e.returnValue=false
                } else if (e.detail) {
                    direct = e.detail;
                    e.preventDefault();
                }
                var W = $(this).width();
                var ratio=$(this).height()/W;
                var _t =$(this).position().top;
                var _l =$(this).position().left;
                if (direct < 0) {
                    W += unit;
                    _t-= unit/2*ratio;
                    _l-= unit/2;
                } else {
                    W -= unit;
                    _t+= unit/2*ratio;
                    _l+= unit/2;
                }
                if(W>min){
                    if(i_rotate%180==0)
                        $(this).css({height:"auto",W,top:_t,left:_l});
                    else{
                        if(isIE)
                            $(this).css({height:W,"auto",top:_t,left:_l});
                        else
                            $(this).css({W});//非IE下难以居中缩放
                    }
                }
                return false;
            }
            $("#"+g_id).mouseover(function(){
                if (document.addEventListener) {
                    this.addEventListener('DOMMouseScroll', scrollMouse, false);
                }
                this.onmousewheel = scrollMouse;
            }).dblclick(function(){
                rotate_pic("restore");
                $(this).css({"auto",height:"auto",left:g_l,top:g_t});
            });
            move_div("#"+g_id);
            move_div("#xzy_zoom");
            $("#xzy_zoom_close").click(function(){
                $("#xzy_zoom").hide();
            });
            $("#xzy_zoom_left").click(function(){rotate_pic(-90)});
            $("#xzy_zoom_right").click(function(){rotate_pic(90)});
            var i_rotate=0,
                userAgent = navigator.userAgent,
                isIE = /msie/i.test(userAgent) && !window.opera,
                isWebKit = /webkit/i.test(userAgent),
                isFirefox = /firefox/i.test(userAgent);
            function rotate_pic(n) {
                if(isNaN(n)){
                    i_rotate=0;
                }else{
                    i_rotate+=n;
                }
                var degree=i_rotate%360;
                var temp=Math.abs(degree);
                var target=document.getElementById(g_id);
                if (isWebKit){
                    target.style.webkitTransform = "rotate("+degree+"deg)";
                } else if (isFirefox){
                    target.style.MozTransform = "rotate("+degree+"deg)";
                } else if (isIE) {
                    degree = degree / 180 * Math.PI;
                    var sinDeg = Math.sin(degree);
                    var cosDeg = Math.cos(degree);
                    target.style.filter = "progid:DXImageTransform.Microsoft.Matrix(M11="+cosDeg+",M12="+(-sinDeg)+",M21="+sinDeg+",M22="+cosDeg+",SizingMethod='auto expand')";
                    //解决旋转居中
                    var i_w=target.width,i_h=target.height,i_m=Math.max(i_w,i_h);
                    if(temp%180==0){
                        $("#"+g_id).css({marginLeft:0,marginTop:0});
                    }else{
                        $("#"+g_id).css({marginLeft:(i_m-i_h)/2,marginTop:(i_h-i_m)/2});
                    }
                } else {
                    target.style.transform = "rotate("+degree+"deg)";
                }
            }
        }
    });
</script>
</body>
</html>
原文地址:https://www.cnblogs.com/laonanren/p/2975813.html