點擊html內任意圖片放大,再點擊關閉放大圖片

HTML 代碼

<div id="outerdiv" style="text-align: center;position: fixed;z-index: 1000;top: 0;left: 0;
    width: 100%;height: 100%;background-color: rgba(255,255,255,.9);">
    <div>關閉</div>
    <img id="bigimg" style="height: auto;width: 46.6325%;border: 0;
        margin: auto;position: absolute;top: 0;bottom: 0;left: 0;right: 0;" src="" />
</div>

JS代碼

<script>
    //圖片放大
    $("#outerdiv").hide();
    $(function(){
        $("img").mouseover(function(){
            $(this).css("cursor","pointer");
        });

        $("img").click(function(){
            var _this = $(this);//將當前的pimg元素作爲_this傳入函數
            imgShow("#outerdiv", "#bigimg", _this);
        });
    });

    function imgShow(outerdiv, bigimg, _this) {
        var src = _this.attr("src");//獲取當前點擊的pimg元素中的src屬性
        $("#outerdiv").attr("display", "block");
        $("#bigimg").attr("src", src);//設置#bigimg元素的src屬性
        $("#outerdiv").fadeIn("fast");

        $("#outerdiv").click(function () {//再次點擊淡出消失彈出層
            $(this).fadeOut("fast");
        });
    }
</script>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章