根据窗口大小改变悬浮窗显示方式并自动刷新页面

如果一个页面有个浮动的二维码,当页面窗口缩小时二维码会遮盖住页面内容,这时候可以根据浏览器大小来决定显示方式:

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();
    }

  }

}

转载于:https://www.cnblogs.com/Man-Dream-Necessary/p/6251712.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值