1. 程式人生 > >手機橫屏和豎屏情況下的圖片旋轉

手機橫屏和豎屏情況下的圖片旋轉

圖片渲染要解決的幾個主要問題

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="../../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>
        $("#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");
            }
        })