在css中vw和vh分別表明所佔的百分比,能夠設置最外部的容器的寬高,可是都要結合百分比一塊兒使用。javascript
回到相關文章 » css
圖片寬度(目前1024像素): 128 1024java
點擊我出現圖片彈框web
代碼
CSS代碼:
.dialog_container {
display: none;
width: 100%;
width: 100vw;
height: 100%;
height: 100vh;
background-color: rgba(0,0,0,.35);
text-align: center;
position: fixed;
top: 0;
left: 0;
z-index: 10;
}
.dialog_container:after {
display: inline-block;
content: '';
width: 0;
height: 100%;
vertical-align: middle;
}
.dialog_box {
display: inline-block;
border: 1px solid #ccc;
text-align: left;
vertical-align: middle;
position: relative;
}瀏覽器
.dialog_title {
line-height: 28px;
padding-left: 5px;
padding-right: 5px;
border-bottom: 1px solid #ccc;
background-color: #eee;
font-size: 12px;
text-align: left;
}wordpress
.dialog_close {
position: absolute;
top: 5px;
right: 5px;
}this
.dialog_body {
background-color: #fff;
}spa
.demo_image {
-webkit-transition: width .3s;
-moz-transition: width .3s;
transition: width .3s;
}
HTML代碼:
<div id="dialogContainer" class="dialog_container">
<div id="dialogBox" class="dialog_box">
<div id="dialogTitle" class="dialog_title">尺寸動態可變圖片</div>
<a href="javascript:" id="dialogClose" class="dialog_close">[關閉]</a>
<div id="dialogBody" class="dialog_body">
<img src="http://image.zhangxinxu.com/image/study/s/s512/mm1.jpg" class="demo_image" />
</div>
</div>
</div>
JS代碼:
(function() {
if (typeof window.screenX === "number") {
var $ = function(selector) {
return document.querySelector(selector);
};
// 元素們
var eleWidth = $("#imageWidth"), eleRange = $("input[type='range']"), eleBtn = $("#testButton"),
eleDialog = $("#dialogContainer");
eleBtn.addEventListener("click", function() {
$("#dialogBody img").style.width = eleRange.value + "px";
eleDialog.style.display = "inline";
});
eleRange.addEventListener("click", function() {
eleWidth.innerHTML = this.value;
$("#dialogBody img").style.width = this.value + "px";
});
$("#dialogClose").addEventListener("click", function() {
eleDialog.style.display = "none";
return false;
});
} else {
alert("您如今使用的瀏覽器內力不足,爲防止走火入魔,建議使用IE9+或Chrome 20+瀏覽器~~");
}
})();圖片