手机横屏和竖屏情况下的图片旋转

图片渲染要解决的几个主要问题

 

1、图片默认是水平方向,要设置图片居中

max-height:100%;max-width:100%;position:absolute;left:50%;top:50%;transform:translate(-50%,-50%)"

 

2、需要旋转的情况是:图片的宽度大于高度,这样旋转后图片显示的就大些

// 获取图片的实际宽度和高度
var picWidth = $("#showPicContent").width(); 
var picHeight = $("#showPicContent").height();
if( picWidth > picHeight) {}

 

3、在旋转之前要确认好图片的大小,因为旋转后依然是以旋转前的图片的大小

var picRate = picWidth / picHeight;
var windowRate = $(window).height() / $(window).width();
console.log(picRate, windowRate)
if (picRate <= windowRate) {
    PicMaxWidth = $(window).width() * picRate * 0.95
} else {
    PicMaxWidth = $(window).height()
}
$("#showPicContent").css("max-width", PicMaxWidth)

 

4、旋转的代码 要包含样式中设定的 translate(-50%,-50%),否则会影响居中的效果

// 旋转的角度 顺时针为正,逆时针为负
$("#showPicContent").css({ "transform": "translate(-50%,-50%) rotate(90deg)" })

 

5、判断手机横屏与竖屏状态

//判断手机横竖屏状态:
function hengshuping() {
    //alert("hii")
    // window.orientation 只有在手机上才有,网页测试看不出效果
    console.log(window.orientation);
    //$("#showPicContent").css({"transform":"translate(-50%,-50%) rotate(0deg)"})
    if (window.orientation == 180 || window.orientation == 0) {
        //alert("竖屏状态!")
        $("#showPicContent").css("max-width", PicMaxWidth)
        $("#showPicContent").css({ "transform": "translate(-50%,-50%) rotate(90deg)" })
    }
    if (window.orientation == 90 || window.orientation == -90) {
        //alert("横屏状态!")
        $("#showPicContent").css("max-width", "100%")
        $("#showPicContent").css({ "transform": "translate(-50%,-50%) rotate(0deg)" })
    }
}
window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", hengshuping, false);

广州品牌设计公司https://www.houdianzi.com

完整的代码:实现点击一个图片显示蒙层,蒙层里面有一个图片 与一个关闭按钮

<div style="position:fixed;background:rgba(0,0,0,1.0);top:0;right:0;bottom:0;left:0;z-index:1000;font-size:20px;color:#fff;display:none;" class="backdrop">
    <div style="position:absolute;right:10px;padding-top:5px;color:#f46608;cursor:pointer;z-index:100;" class="closePic">关闭</div>
    <img src="../../dist/img/QQ图片20190604084934.jpg" id="showPicContent" style="max-height:100%;max-width:100%;position:absolute;left:50%;top:50%;transform:translate(-50%,-50%)" />
</div>
<script>
	$("#showPic1").click(function() {
		if($(".backdrop").css("display") == "none") {
			$(".backdrop").css("display", "block");
		}
		var picWidth = $("#showPicContent").width(); // 获取图片的实际宽度和高度
		var picHeight = $("#showPicContent").height();
		//var picWidth = $("#showPicContent").css("width")// 获取图片样式的宽度与高度
		//var picHeight = $("#showPicContent").css("height")

		console.log(picWidth, picHeight)

		if($(window).width() < 700) {
			if(picWidth > picHeight) {
				var PicMaxWidth
				var picRate = picWidth / picHeight;
				var windowRate = $(window).height() / $(window).width();
				console.log(picRate, windowRate)
				if(picRate <= windowRate) {
					PicMaxWidth = $(window).width() * picRate * 0.95
				} else {
					PicMaxWidth = $(window).height()
				}
				$("#showPicContent").css("max-width", PicMaxWidth)
				$("#showPicContent").css({
					"transform": "translate(-50%,-50%) rotate(90deg)"
				})
				//判断手机横竖屏状态:
				function hengshuping() {
					//alert("hii")
					// window.orientation 只有在手机上才有,网页测试看不出效果
					console.log(window.orientation);
					//$("#showPicContent").css({"transform":"translate(-50%,-50%) rotate(0deg)"})
					if(window.orientation == 180 || window.orientation == 0) {
						//alert("竖屏状态!")
						$("#showPicContent").css("max-width", PicMaxWidth)
						$("#showPicContent").css({
							"transform": "translate(-50%,-50%) rotate(90deg)"
						})
					}
					if(window.orientation == 90 || window.orientation == -90) {
						//alert("横屏状态!")
						$("#showPicContent").css("max-width", "100%")
						$("#showPicContent").css({
							"transform": "translate(-50%,-50%) rotate(0deg)"
						})
					}
				}
				window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", hengshuping, false);
			}
		}
	})

	$("#showPicContent, .closePic").click(function() {
		if($(".backdrop").css("display") == "block") {
			$(".backdrop").css("display", "none");
		}
	})
</script>
posted @ 2020-11-25 14:18  酷儿q  阅读(289)  评论(0)    收藏  举报