JQUERY简单实现点击图片放大效果

本地新建个html,复制以下代码,浏览器打开看效果

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <title>Lightbox</title>
    <meta name="description" lang="en" content="Lightbox is a script used to overlay images on the current page. It's a snap to setup and works on all modern browsers."/>
    <meta name="author" content="Lokesh Dhakar">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <style type="text/css">
.fillbg { background-color: rgba(0, 0, 0, 0.6); bottom: 0; height: 100%; left: 0; opacity: 0; position: fixed; right: 0; top: 0; width: 100%; z-index: 1100; display:none; }
.fillbg-active { opacity: 1; display:block; }
    </style>
</head>
<body>

<img class="comment_pics" style="margin-top:500px;margin-left:500px;" width="50px" height="50px" src="http://www.yyyweb.com/demo/lightbox/img/demopage/image-1.jpg"/>
<div class="bg">
    <img class="bgImg" style="max-width: 100%; max-height: 100%; position: fixed;" src="">
</div>

</body>
</html>

<script>
    var newImg;
    var clientH=$(window).height();
    var clientW=$(window).width();
    var w = '250';
    var h = '250';
    $(document).ready(function(){
        $(".comment_pics").bind("click", function(){
            newImg = $(this)[0].src;
            $("body").append('<div class="fillbg"></div>');
            $(".fillbg").addClass("fillbg-active");
            $('.bgImg').css({'width': w+"px",'height': h+"px",'top':(clientH-h)/2+"px",'left':(clientW-w)/2+"px",'z-index':1101});
            $('.bgImg').attr("src",newImg);
        });

        $(".bgImg").bind("click", function(){
            $(".fill-input").removeClass("fill-input-active");
            setTimeout(function(){
                $(".fillbg-active").removeClass("fillbg-active");
                $(".fillbg").remove();
            },300);
            $('.bgImg').css({'width': '0px','height': '0px'});
            $('.bgImg').attr("src",'');
        });
    });
</script>


版权声明:本文为sinat_40751643原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。