如果一個頁面有個浮動的二維碼,當頁面窗口縮小時二維碼會遮蓋住頁面內容,這時候可以根據瀏覽器大小來決定顯示方式:
1.當頁面寬度足夠大時,完全顯示二維碼,
2.當頁面窗口縮小時,這時候需要顯示一個按鈕,點擊按鈕才顯示二維碼
這時候需要添加onresize來監聽窗口變化,以此來刷新頁面:
window.onresize = function() { //監聽窗口變化
window.location.reload(); //兼容chrome safari
window.location.href = ""; //兼容火狐
}
至於上面二維碼的顯示方式,在 vue 里使用 v-show 實現非常方便:
HTML:
<!-- 按鈕 -->
<div class="qr-btn" v-show="qrBtn" @click="showQr"><img src="../assets/images/tangulunyin.jpg" alt="談股論銀"></div>
<!-- 二維碼 -->
<div class="qr-code" v-show="qrCode">
<ul>
<li><img src="../assets/images/yinruyi.png" alt="銀如意" title="銀如意"><span>掃描下載銀如意app</span></li>
<li><img src="../assets/images/tangulunyin.jpg" alt="談股論銀"><span>掃描關注談股論銀公眾號</span></li>
</ul>
</div>
CSS:
.qr-btn {
width: 30px;
height: 30px;
background-color: #064491;
display: block;
position: absolute;
left: 20px;
bottom: 50px;
border-radius: 50%;
box-shadow: 2px 2px 8px rgba(0, 0, 0, .6);
cursor: pointer
}
.qr-btn>img {
width: 20px;
height: 20px;
position: relative;
top: 5px;
display: block;
padding: 0 auto;
margin: 0 auto;
}
.qr-code {
position: fixed;
bottom: 10%;
left: 1%;
background-color: aliceblue;
font-size: 12px;
text-align: center;
}
.qr-code>ul>li {
padding: .8em;
}
.qr-code>ul>li:last-child {
margin: 0 0 1em 0;
}
.qr-code>ul>li>img {
width: 130px;
height: 130px;
display: block;
}
VUE:
export default {
data:()=>({
qrCode: false,
qrBtn: false
})
},
methods:{
showQr() {
if (this.qrCode == false) {
this.qrCode = true;
} else {
this.qrCode = false;
}
}
},
ready(){
let w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
let h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
if (w < 1330) { //根據屏幕寬度決定是顯示二維碼還是按鈕
this.qrCode = false;
this.qrBtn = true;
} else {
this.qrCode = true;
this.qrBtn = false;
}
window.onresize = function() { //監聽窗口變化
window.location.reload(); //兼容chrome safari
window.location.href = ""; //兼容火狐
}
}
-------------------------------------------------------------------
以上的方法比較耗性能,而且不安全,另一個方法:
VUE:
export default {
data:()=>({
qrCode: false,
qrBtn: false
})
},
methods:{
showQr() {
if (this.qrCode == false) {
this.qrCode = true;
} else {
this.qrCode = false;
}
},
listen() {
let w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
if (w < 1330) { //根據屏幕寬度決定是顯示二維碼還是按鈕
this.qrCode = false;
this.qrBtn = true;
} else {
this.qrCode = true;
this.qrBtn = false;
}
}
},
ready(){
this.listen();
window.onresize = () => { //監聽窗口變化
this.listen();
}
}
}