头闻号

广州科珑化工有限公司

羧酸|烷烃|净水絮凝剂|三元醇|聚氨酯树脂

首页 > 新闻中心 > 科技常识:手机横屏和竖屏情况下的图片旋转
科技常识:手机横屏和竖屏情况下的图片旋转
发布时间:2024-12-24 00:37:27        浏览次数:4        返回列表

今天小编跟大家讲解下有关手机横屏和竖屏情况下的图片旋转 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了有关手机横屏和竖屏情况下的图片旋转 的相关资料,希望小伙伴们看了有所帮助。

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

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);

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

<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="http://www.aidi.net.cn/article/detial/4346/../../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 PicMaxWidthvar 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>

来源:爱蒂网