前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >手机横屏和竖屏情况下的图片旋转

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

作者头像
tianyawhl
发布2019-07-25 15:23:37
1.8K0
发布2019-07-25 15:23:37
举报
文章被收录于专栏:前端之攻略前端之攻略

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

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

代码语言:javascript
复制
max-height:100%;max-width:100%;position:absolute;left:50%;top:50%;transform:translate(-50%,-50%)"

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

代码语言:javascript
复制
// 获取图片的实际宽度和高度
var picWidth = $("#showPicContent").width(); 
var picHeight = $("#showPicContent").height();
if( picWidth > picHeight) {}

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

代码语言:javascript
复制
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%),否则会影响居中的效果

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

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

代码语言:javascript
复制
//判断手机横竖屏状态:
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);

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

代码语言:javascript
复制
    <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>
代码语言:javascript
复制
        $("#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");
            }
        })

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档