用div+css+html+js 实现图片放大

上一篇我已经写到,实现图片放大效果图。
https://blog.csdn.net/weixin_45932157/article/details/104397913

不是很详细,现在我主要实现可以进行图片切换效果。
当然,你也可以加一些效果图:

css部分:

.over {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 100;
}

.tempContainer {
    position: fixed;
    width: 100%;
    margin-right: 0px;
    margin-left: 0px;
    text-align: center;
    z-index: 101;
    height: 100%;
    
}

.body-left {
    width: 5%;
    height: 100%;
    background-color: black;
    float: left;
    cursor: pointer;
    border: 1px solid #C3BBB8;
}

    .body-left img {
        width: 100%;
        z-index: 102;
        display: block;
        border: 1px solid #C3BBB8;
        margin-top: 400%;
    }

.body-right {
    height: 100%;
    width: 5%;
    background-color: black;
    float: left;
    cursor: pointer;
    border: 1px solid #C3BBB8;
}
.body-center {
    height: 100%;
    width: 90%;
    float: left;
   
}
.body-right img {
    z-index: 102;
    width: 100%;
    display: block;
    border: 1px solid #C3BBB8;
    margin-top: 400%;
}

html部分:

<div class="over"  style="display:none;"></div><!--背景层-->
<div id="hualang">

    @foreach (var item in Model)
    {
        <img src="~/img/@item.detailsImg" alt="Alternate Text" />
    }
    <div id="demo1"></div>
</div>

阐述一下:主要调用<div id="hualang">里的图片
js部分:

<script>
    $(document).ready(function () {
        var imgsObj = $('#hualang img');//需要放大的图像 
             if (imgsObj) {
            $.each(imgsObj, function () {
                $(this).click(function () {
                      var currImg = $(this);
                     contentshow(currImg);//初始点击状态
               });
  
            });
          }
        else {
            return false;
        }
        
        function contentshow(currImg) { 
        
                    coverLayer(1);
                    var tempContainer = $('<div class=tempContainer></div>');//图片容器
                    with (tempContainer) {//width方法等同于$(this)
                        appendTo("body");
                        var windowWidth = $(window).width();//窗口的宽度
                        var windowHeight = $(window).height();//窗口的高度
                        //获取图片原始宽度、高度
                        var orignImg = new Image();
                        orignImg.src = currImg.attr("src");
                        var currImgWidth = orignImg.width;
                        var currImgHeight = orignImg.height;
                        if (currImgWidth < windowWidth) {//为了让图片不失真,当图片宽度较小的时候,保留原图
                            if (currImgHeight < windowHeight) {
                                
                                var topHeight = (windowHeight - currImgHeight) / 2;
                                //if (topHeight > 35) {/*此处为了使图片高度上居中显示在整个手机屏幕中:因为在android,ios的微信中会有一个title导航,35为title导航的高度*/
                                //    topHeight = topHeight - 35;
                                //    css('top', topHeight);
                                //} else {
                                //    css('top', 0);
                                //}
                                //图片自适应高度
                                css('top', topHeight);//上面是适应安卓的,这是适应pc端的
                                //alert(topHeight + "|" + windowHeight);
                                html('<div  class="body-left" style="height:' + windowHeight + 'px;  margin-top:'+-topHeight+'px "><img src="/images/ico_left.png"   /></div><div class="body-center"><img border=0 src=' + currImg.attr('src') + '></div ><div class="body-right" style=" height:' + windowHeight + 'px; margin-top:'+-topHeight+'px " ><img src="/images/ico_right.png" /></div>');
                            } else {
                                
                                css('top', 0);
                                //alert(windowWidth + "|" + windowHeight + "|" + currImgWidth);
                                html('<div  class="body-left" style="height:' + windowHeight + 'px"  ><img src="/images/ico_left.png"   /></div><div class="body-center"><img border=0 src=' + currImg.attr('src') + '></div ><div class="body-right" style=" height:' + windowHeight + 'px"><img src="/images/ico_right.png" /></div>');
                            }
                        } else {
                            
                            var currImgChangeHeight = (currImgHeight * windowWidth) / currImgWidth;
                            if (currImgChangeHeight < windowHeight) {
                                
                                var topHeight = (windowHeight - currImgChangeHeight) / 2;//让图片居中显示
                                //if (topHeight > 35) {

                                //    topHeight = topHeight - 35;
                                //    css('top', topHeight);
                                //} else {

                                //    css('top', 0);
                                //}
                                css('top', topHeight);//上面是适应安卓的,这是适应pc端的
                               html('<div  class="body-left" style="height:' + windowHeight + 'px " ><img src="/images/ico_left.png"   /></div><div class="body-center"><img border=0 src=' + currImg.attr('src') + ' width=' + windowWidth + '></div ><div class="body-right" style=" height:' + windowHeight + 'px" ><img src="/images/ico_right.png" /></div>');
                            } else {
                                
                                css('top', 0);
                               html('<div  class="body-left" style="height:' + windowHeight + 'px " ><img src="/images/ico_left.png"   /></div><div class="body-center"><img border=0 src=' + currImg.attr('src') + ' width=' + windowWidth + '   height=' + windowHeight + '></div ><div class="body-right" style=" height:' + windowHeight + 'px" ><img src="/images/ico_right.png" /></div>');
                            }
                        }
            }
            tempContainer.on("click",".body-center",function () {
                        $(this).remove();
                        coverLayer(0);
                  });
            tempContainer.click(function () {
                        $(this).remove();
                         
                  });
         // alert(currImg.parents("#hualang").children().length);//快速获取所有的图片数量
            console.log(currImg.parents("#hualang").children());
            showleft(eval(currImg.index()+"-" +1));
            showright(eval(currImg.index()+"+" +1),currImg.parents("#hualang").children().length-1);//减一的原因是我上面里面多了一串代码( <div id="demo1"></div>)

        }
        //使用禁用蒙层效果
        function coverLayer(tag) {
            with ($('.over')) {
                if (tag == 1 ) {
                    css('height', $(document).height());
                    css('display', 'block');
                    css('opacity', 1);
                    css("background-color", "#FFFFFF");
                    css("background-color", "rgba(0,0,0,0.8)");  //蒙层透明度
                }
                else {
                    css('display', 'none');
                }
            }
        }

        function showleft(id) {//图标左
            $(".tempContainer").on("click", ".body-left ", function () {
                
                coverLayer(1);
                if (id == -1) {//判断是否为第一张,如果为第一张则停止
                    
                    layer.alert('已经是第一张图 ,点击中间部分即可退出页面', {icon: 6,time:2000});
                    return false;
                }
                contentshow(imgsObj.eq(id));//调用点击左箭头传过来的id,进行图片筛选。
               
                 

            });
           

        }

        
        function showright(id,len) {//图标右
            $(".tempContainer").on("click", ".body-right ", function () {
                 coverLayer(1);
                 if (id == len) {//判断是否为最后一张,如果为最后一张则停止
                     //alert("已经是最后一张图");
                     layer.alert('已经是最后一张图 ,点击中间部分即可退出页面', {icon: 6,time:2000});
                    return false;
                }
                contentshow(imgsObj.eq(id));//调用点击右箭头传过来的id,进行图片筛选。
                 

            });
        }

    });
</script>

运行效果:

初始页面显示:

在这里插入图片描述

点击进去后,看到的效果为:

在这里插入图片描述

点击左箭头显示为:
在这里插入图片描述

点击右箭头显示为:
在这里插入图片描述

你还可以加一些特效

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你的美,让我痴迷

你的好,我会永远记住你的。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值