html5图片点击放大

姜业
2023-12-01

第一种:

css:
<style type="text/css">
    *
    {
        margin:0px;
        padding:0px;
    }
    .zhezhao
    {
        width:100%;
        height:100%;
        background-color:#000;
        filter:alpha(opacity=50);
        -moz-opacity:0.5;
        opacity:0.8;
        position:absolute;
        left:0px;
        top:0px;
        display:none;
        z-index:4;
    }
</style>

html:

<div class="zhezhao" id="zhezhao" onclick="bg();"></div>
<div class="image_large" onclick="hideImg();" hidden>
</div>


<img src='"+data.userHead+"' onclick="imgshow('" + data.userHead + "');" height='30px' class="logoImg amplifyImg"/>


<script type="text/javascript">
    var zhezhao=document.getElementById("zhezhao");
    var login=document.getElementById("login");
    function imgshow(src){
        var large_image = '<img src= '+ src +'></img>';
        $('.image_large').show();
        $('.image_large').html($(large_image).attr("style","display:block;height:90%;position:absolute;left:50%;top:50%;transform:translate(-50%,-50%);z-index:5;"));
        zhezhao.style.display="block";
        login.style.display="block";
    }
    function hideImg() {
        $('.image_large').hide();
        zhezhao.style.display="none";
        login.style.display="none";
    }
    function bg() {
        $('.image_large').hide();
        zhezhao.style.display="none";
        login.style.display="none";
    }
</script>

第二种:

1.html 代码
<div id="imgEnlargeDiv" style="display: none; text-align: center;position: fixed;z-index: 1000;top: 0;left: 0;
    width: 100%;height: 100%;background-color: rgba(255,255,255,.9);">
    <img id="bigimg" style="height: auto;width: 40%;border: 0; 
        margin: auto;position: absolute;top: 0;bottom: 0;left: 0;right: 0;" src="" />
</div>

2.js 代码 
<script src="assets/js/jquery-1.10.2.min.js"></script> <!--引入jquery -->

<script type="text/javascript">

//图片放大  
$(function(){  
     $("#imgEnlargeDiv").click(function(){//再次点击淡出消失弹出层    
            $(this).fadeOut("fast");    
        });
});

function imgShow(outerdiv, bigimg, _this){  
    var src = _this.attr("src");//获取当前点击的pimg元素中的src属性    
    $(bigimg).attr("src", src);//设置#bigimg元素的src属性    
    $(outerdiv).fadeIn("fast");  //图片放大的div快速淡入显示层
} 

function imgEnlarge() {
    $("img[type ='showImg']").mouseover(function(){
          $(this).css("cursor","pointer");//鼠标移动到图片,鼠标箭头变为手势
      });
    $("img[type ='showImg']").click(function(){  
       var _this = $(this);//将当前的pimg元素作为_this传入函数    
       imgShow("#imgEnlargeDiv", "#bigimg", _this);    
     }); 
}


</script>

 

3.使用
调用imgEnlarge();  则会对$("img[type ='showImg']")的图片添加点击事件。

点击图片后则会弹出图片放大层。
 类似资料: