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

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

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 = ""; //兼容火狐
  }

}

时间: 2024-08-01 22:48:05

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

gulp+browserSync自动刷新页面

BrowserSync "Browsersync能让浏览器实时.快速响应您的文件更改(html.js.css.sass.less等)并自动刷新页面.更重要的是 Browsersync可以同时在PC.平板.手机等设备下进项调试.您可以想象一下:"假设您的桌子上有pc.ipad.iphone.android等设备,同时打开了您需要调试的页面,当您使用browsersync后,您的任何一次代码保存,以上的设备都会同时显示您的改动".无论您是前端还是后端工程师,使用它将提高您30%的

Servlet自动刷新页面

以下内容引用自http://wiki.jikexueyuan.com/project/servlet/auto-refresh.html: 假设一个Web页面,显示了现场比赛得分或股票市场状况或货币兑换率.对于所有这些类型的页面,都需要使用浏览器中的Refresh或Reload按钮来定期刷新Web页面. Java Servlet提供一个机制使这项工作变得简单,可以使得Web页面在给定的时间间隔自动刷新. 刷新一个Web页面最简单的方式是使用响应对象的方法setIntHeader().以下是这种方

js自动刷新页面代码

<script language="JavaScript">function myrefresh(){window.location.reload();}setTimeout('myrefresh()',1000); //指定1秒刷新一次</script> 页面自动刷新代码大全1) 自动刷新页面 在 <body> 与 </body> 之间加入: <META http-equiv=refresh content=”10;url=../

基于JavaScript并毫无技术含量的超级无敌说书人 悬浮窗版 斗鱼自动挡弹幕发射器

let div1 = document.createElement('div');//默认悬浮窗 let div2 = document.createElement('div');//控制台 let css1 = 'background: #1A59B7;color:#ffffff;overflow: hidden;z-index: 998;position: fixed;padding:5px;text-align:center;width: 75px;height: 22px;border-

使用node-livereload自动刷新页面

1. 安装node 2. 安装python 3. 安装connect, serve-static和node-livereload (以下都假设命令行当前目录为e:\WebSite) e:\WebSite>npm install connect e:\WebSite>npm install serve-static e:\WebSite>npm install livereload 如果出现类似:MSB8036: The Windows SDK version 8.1 was not fo

Ajax+php实现自动刷新页面

前端代码: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript"> var xmlHttp; var count = 1; function createXMLHttpRequest() { if (window.ActiveXObject) { xmlHttp =

4.4 创建自动刷新页面

<1>使用Ajax,用户就可以不必反复点击刷新按钮,而实现网页内容的自动刷新 <2>例程采用单击按钮后开始执行,实际中一般以onload事件代替 <3>setTimeout方法,允许以固定的时间间隔(单位为毫秒)执行给定的方法 <4>createRow()方法使用DOM动态创建内容:refreshTime()用于刷新定时器的值 页面代码: <!DOCTYPE html> <html> <head> <meta cha

bootstrap表单按回车会自动刷新页面的问题

想给form表单增加回车自动提交的功能 $('#password').keydown(function(event){ if (event.keyCode == 13) $('#login').click(); }); 然而,并没有达到预期的效果,而是自动刷新了表单,并将表单参数放在了url后面,如图 发现是是form标签的问题,将其改为div 问题解决,但原因未知

如何实现自动刷新页面

如果需要自动刷新一个页面,可以用一个简单的HTML实现. <meta http-equiv="refresh" content="10" /> <title>refresh this page every 10 seconds. close this page to stop.</title> <iframe src="http://www.cnblogs.com/bitzhuwei/p/CSharpGL-an-e