layer.photos放大缩小以及旋转

layer.photos放大缩小以及旋转

由于在网上搜到的旋转都多少有些问题,要么是旋转后图片转底层的背景大小不改变导致图片展示不全,要么是连背景一起旋转用起来很不方便,所以我把他调整成了旋转图片并且根据图片旋转后的长宽调整背景层的长宽。

事先准备

替换layer.js中最后一行的content

content: '<input type="hidden" id="current"><div class="layui-layer-phimg"><img id="imglayer" src="' + u[d].src + '" alt="' + (u[d].alt || "") + '" layer-pid="' + u[d].pid + '"><div class="layui-layer-imgbar" style="width:100%;height: 100%; background-color:rgba(0, 0, 0, 0)"></div><div class="layui-layer-imgsee">' + (u.length > 1 ? '<span class="layui-layer-imguide"><a href="javascript:;" class="layui-layer-iconext layui-layer-imgprev"></a><a href="javascript:;" class="layui-layer-iconext layui-layer-imgnext"></a></span>' : "") + '<div class="layui-layer-imgbar" style="display:' + (a ? "block" : "") + '"><span class="layui-layer-imgtit"><a href="javascript:;">' + (u[d].alt || "") + "</a><em>" + s.imgIndex + "/" + u.length + "</em></span></div></div></div>"

调用layer.photos

layer.photos({
		        photos: { "data": [{"src":"https://sjbz-fd.zol-img.com.cn/t_s320x510c5/g6/M00/03/0E/ChMkKmDZLWGIDHstAC1U9uUHfekAARQfQAAAAAALVUO339.jpg"},{"src":"https://sjbz-fd.zol-img.com.cn/t_s320x510c5/g6/M00/03/0E/ChMkKmDZLWGIDHstAC1U9uUHfekAARQfQAAAAAALVUO339.jpg"}] ,"start": 1}
		        ,anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机
		        ,closeBtn: 1,
		        tab:function () {
		            num=0;
		            $("#layui-layer-photos").append('<div class="icon" style="position:absolute;width:100%;text-align:center;bottom:0px;cursor:pointer;">\n' +
		                '\t\t<img src="images/xz.png" style="width:30px;height30px;">\n' +
		                '\t</div>');
		        }
	
		  });

旋转

$(document).on("click", ".icon img", function (e) {
		    /*var current = $("#current").val();
		    current = (current+90)%360;
		    $("#current").val(current);
		    document.getElementById('imglayer').style.transform = 'rotate('+current+'deg)';*/
		 	// 图片地址 后面加时间戳是为了避免缓存
		 	
		    var img_url = $("#imglayer").attr("src");
		      
		    // 创建对象
		    var img = new Image();
		      
		    // 改变图片的src
		    img.src = img_url;
		    var width = img.width;
		    var height = img.height;
		    var imagep = $(".layui-layer-phimg").parent().parent();
		    var image = $(".layui-layer-phimg").parent();
		    var h = image.height();
		    var w = image.width();
		    var current = $("#current").val();
		    if (current == 0) {
		        current = 90;
		        $(".layui-layer.layui-layer-page.layui-layer-photos").css("width",height);
		        $(".layui-layer.layui-layer-page.layui-layer-photos").css("height",width);
		        imagep.css("top", (window.innerHeight - width) / 2);
			    imagep.css("left", (window.innerWidth - height) / 2);
		        $("#layui-layer-photos").css("width",height);
		        $("#layui-layer-photos").css("height",width);
		        $('#imglayer').css("margin-top",-(height-width)/2);
		        if(height<width){
		        	$('#imglayer').css("margin-left",(height-width)/2);
		        }
		        $('#imglayer').css("width",width);
		    }else if (current == 90) {
		        current = 180;
		        $(".layui-layer.layui-layer-page.layui-layer-photos").css("height",height);
		        $(".layui-layer.layui-layer-page.layui-layer-photos").css("width",width);
		        imagep.css("top", (window.innerHeight - height) / 2);
			    imagep.css("left", (window.innerWidth - width) / 2);
		        $("#layui-layer-photos").css("height",height);
		        $("#layui-layer-photos").css("width",width);
		        $('#imglayer').css("margin-top",0);
		        if(height<width){
		        	$('#imglayer').css("margin-left",0);
		        }
		        $('#imglayer').css("width",width);
		    }else if(current == 180) {
		        current = 270;
		        $(".layui-layer.layui-layer-page.layui-layer-photos").css("width",height);
		        $(".layui-layer.layui-layer-page.layui-layer-photos").css("height",width);
		        imagep.css("top", (window.innerHeight - width) / 2);
			    imagep.css("left", (window.innerWidth - height) / 2);
		        $("#layui-layer-photos").css("width",height);
		        $("#layui-layer-photos").css("height",width);
		        $('#imglayer').css("margin-top",-(height-width)/2);
		        if(height<width){
		        	$('#imglayer').css("margin-left",(height-width)/2);
		        }
		        $('#imglayer').css("width",width);
		    }else if (current == 270) {
		        current = 0;
		        $(".layui-layer.layui-layer-page.layui-layer-photos").css("height",height);
		        $(".layui-layer.layui-layer-page.layui-layer-photos").css("width",width);
		        imagep.css("top", (window.innerHeight - height) / 2);
			    imagep.css("left", (window.innerWidth - width) / 2);
		        $("#layui-layer-photos").css("height",height);
		        $("#layui-layer-photos").css("width",width);
		        $('#imglayer').css("margin-top",0);
		        if(height<width){
		        	$('#imglayer').css("margin-left",0);
		        }
		        $('#imglayer').css("width",width);
		    }

		    document.getElementById('imglayer').style.transform = 'rotate(' + current + 'deg)';
		    $("#current").val(current);
		});

缩放

$(document).on("mousewheel DOMMouseScroll", ".layui-layer-phimg", function (e) {
		    var delta = (e.originalEvent.wheelDelta && (e.originalEvent.wheelDelta > 0 ? 1 : -1)) || // chrome & ie
		        (e.originalEvent.detail && (e.originalEvent.detail > 0 ? -1 : 1)); // firefox
		    var imagep = $(".layui-layer-phimg").parent().parent();
		    var image = $(".layui-layer-phimg").parent();
		    var h = image.height();
		    var w = image.width();
		    var img_url = $("#imglayer").attr("src");
		      
		    // 创建对象
		    var img = new Image();
		      
		    // 改变图片的src
		    img.src = img_url;
		    var width = img.width;
		    var height = img.height;
		    debugger;
		    if (delta > 0) {
		        if (h < (window.innerHeight)) {
		            h = h * 1.05;
		            w = w * 1.05;
		            width = width*1.05;
		            height = height*1.05;
		        }
		    } else if (delta < 0) {
		        if (h > 100) {
		            h = h * 0.95;
		            w = w * 0.95;
		            width = width*0.95;
		            height = height*0.95;
		        }
		    }
		    imagep.css("top", (window.innerHeight - h) / 2);
		    imagep.css("left", (window.innerWidth - w) / 2);
		    image.height(h);
		    image.width(w);
		    imagep.height(h);
		    imagep.width(w);
		    var current = $("#current").val();
		    if((current/90)%2==1){
		    	$('#imglayer').css("width",h);
		    	$('#imglayer').css("margin-top",-(w-h)/2);
		        if(height<width){
		        	$('#imglayer').css("margin-left",(w-h)/2);
		        }
		    }else{
		    	$('#imglayer').css("width",w);
		    	$('#imglayer').css("margin-top",0);
		        if(height<width){
		        	$('#imglayer').css("margin-left",0);
		        }
		    }
		});

这样就实现了正常旋转和缩放功能,效果还能看。
效果图
在这里插入图片描述
旋转后
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

君已知悉

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值