js實現圖片預覽

<!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></title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="cache-control" content="no-cache" />
        <meta http-equiv="pragma" content="no-cache" />
        <meta http-equiv="expires" content="0" />
        <script type="text/javascript" src="jquery.js"></script>
        <script>
            $(function() {
                //定義X初始座標量
                var x = 10;
                //定義Y初始座標量
                var y = 20;
                $("a.tooltip").mouseover(function(e) {
                    //聲明變量存儲超鏈接TITLE
                    this.myTitle = this.title;
                    //清空超鏈接TITLE
                    this.title = "";
                    //封裝圖片TITLE
                    var imgTitle = this.myTitle ? "<br/>" + this.myTitle : "";
                    //聲明層對象
                    var tooltip = "<div id='tooltip' style='position:absolute'><img src='" + this.href + "' alt='產品預覽圖' />" + imgTitle + "</div>";
                    //將層追加到文檔中
                    $("body").append(tooltip);
                    //設置層樣式
                    $("#tooltip").css({
                        "top": (e.pageY + y) + "px",
                        "left": (e.pageX + x) + "px"
                    });
                }).mouseout(function() {
                    this.title = this.myTitle;
                    //移除層
                    $("#tooltip").remove();
                }).mousemove(function(e) {
                    $("#tooltip").css({
                        "top": (e.pageY + y) + "px",
                        "left": (e.pageX + x) + "px"
                    });
                });
            });
        </script>
        <style>
                .smallImg {
                width:200px;
                height:130px;
            }
        </style>
    </head>

<body>
<div class="div_img">
    <a href="004.jpg" class="tooltip" title="我的圖片">
        <img src="004.jpg"  alt="我的圖片" class="smallImg" >
        <span>我的圖片</span>
    </a>
</div>
        
</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章