圖片渲染要解決的幾個主要問題javascript
一、圖片默認是水平方向,要設置圖片居中css
max-height:100%;max-width:100%;position:absolute;left:50%;top:50%;transform:translate(-50%,-50%)"
二、須要旋轉的狀況是:圖片的寬度大於高度,這樣旋轉後圖片顯示的就大些html
// 獲取圖片的實際寬度和高度 var picWidth = $("#showPicContent").width(); var picHeight = $("#showPicContent").height(); if( picWidth > picHeight) {}
三、在旋轉以前要確認好圖片的大小,由於旋轉後依然是以旋轉前的圖片的大小java
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)
四、旋轉的代碼 要包含樣式中設定的 translate(-50%,-50%),不然會影響居中的效果測試
// 旋轉的角度 順時針爲正,逆時針爲負 $("#showPicContent").css({ "transform": "translate(-50%,-50%) rotate(90deg)" })
五、判斷手機橫屏與豎屏狀態code
//判斷手機橫豎屏狀態: 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);
完整的代碼:實現點擊一個圖片顯示蒙層,蒙層裏面有一個圖片 與一個關閉按鈕orm
<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"); } })