關於圖片不變形適應屏幕的解決方法(vue爲例)

(此處背景圖寬:高=1.92)css

1.在html中設置兩種可選擇的類:html

<div class="login-container" v-bind:class="[{'cal-backgound-size-width': !calBgImage },{'cal-backgound-size-height' : calBgImage }]">瀏覽器

2.css樣式:this

寬度自適應:url

.cal-backgound-size-width{spa

background: url('/../../../static/bg.png') no-repeat;htm

background-size: auto 100%;事件

}io

高度自適應:function

.cal-backgound-size-height{

background: url('/../../../static/bg.png') no-repeat;

background-size: 100% auto;

}

3.js控制

mounted() {

this.calBgImage = (document.body.clientWidth / document.body.clientHeight) > 1.92

// 而後監聽window的resize事件.在瀏覽器窗口變化時再設置下背景圖高度.

const that = this

window.onresize = function temp() {

that.calBgImage = (document.body.clientWidth / document.body.clientHeight) > 1.92

}

},

相關文章
相關標籤/搜索