img图片在限定尺寸下等比缩放;点击查看原图

17 篇文章 0 订阅
10 篇文章 0 订阅

一,图片等比缩放

效果:

html

<div class="row" id="imgbox">
    <img id="image" src="">
</div>
<div class="row">
    <div id="showimg">查看原图</div>
</div>

css(对img外层div尺寸限制,img长宽自适应)

#imgbox{
    max-width:100%;
    max-height: 400px;
    text-align: center;
}
#image{
    width: auto;/*图片长宽自适应*/
    height: auto;
    max-width:100%;
    max-height: 400px;
}

二,查看原图

添加一个展示大图的div:(点击大图关闭大图)

<div id="bigimg">
    <img src="" onclick="closeImg()">
</div>

css(设置该div绝对定位在屏幕中间)

#bigimg{
    position: fixed;
    top:50%;
    left:50%;
    display: none;
    z-index: 20;
}

js

//查看大图
    $('#showimg').click(function(){
        var image = new Image(); //创建一个image对象
        var path=$("#image").attr("src");
        image.src=path;   //新创建的image添加src
        var width = image.width;  // 获取原始图片的宽
        var hight = image.height; // 获取原始图片高
        $("#bigimg img").attr('src',path);
        $("#bigimg").css({"margin-top":-hight/2,"margin-left":-width/2});

        $("#bigimg").show();
    });

    //关闭大图
    function closeImg(){
        $("#bigImg img").attr('src',"");
        $("#bigimg").hide();
    }

参考:https://blog.csdn.net/qq_30449567/article/details/80548489

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值