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


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

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

      }

    }

  • 相关阅读:
    (连通图 模板题 无向图求桥)Critical Links -- UVA -- 796
    (连通图 模板题)迷宫城堡--hdu--1269
    (连通图 模板题 无向图求割点)Network --UVA--315(POJ--1144)
    (线段树 区间合并更新)Tunnel Warfare --hdu --1540
    (线段树 区间查询更新) Can you answer these queries? -- hdu--4027
    (线段树)Balanced Lineup --POJ --3264
    (线段树) Count the Colors --ZOJ --1610
    (线段树)Just a Hook -- hdu -- 1689
    All about <httpRuntime targetFramework>
    ASP.NET 4.0升级至ASP.NET 4.5需要注意的地方 【转】
  • 原文地址:https://www.cnblogs.com/Man-Dream-Necessary/p/6251712.html
Copyright © 2020-2023  润新知