天天看点

layer弹出窗口的位置,随浏览器的窗口的大小而改变位置

方案一:采用监听resize实现,需要防抖,效果一般。

<!DOCTYPE html>
<html >

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../jquery-2.1.1/jquery.min.js"></script>
  <script src="layer/layer.js"></script>
  <script src="layui/layui.js"></script>
  <link rel="stylesheet" href="layui/css/layui.css" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >
  <style>
    #abcd {
      position: relative;
      padding: 20px;
      top: 50px;
      left: 50px;
      font-size: 20px;
    }
/* 
    @media screen and (min-width:1000px) {
      #layui-layer100001 {
        left: 200px;
        color: red;
      }
    }

    @media screen and (min-width:1400px) {
      #layui-layer100001 {
        left: 400px;
      }
    }

    @media screen and (min-width:2000px) {
      #layui-layer100001 {
        left: 600px;
      }
    } */
  </style>
</head>

<body>
  <!-- 点击按钮,弹出窗口,高度不变,左右在某个位置,随窗口的改变而改变位置 -->
  <button id="abcd">abcd</button>

</body>
<script>
  //防抖函数
  function Debounce(fn, delay) {
    var ctx;
    var args;
    var timer = null;

    var later = function () {
      fn.apply(ctx, args);
      timer = null;
    };

    return function () {
      ctx = this;
      args = arguments;
      if (timer) {
        clearTimeout(timer);
        timer = null;
      }
      timer = setTimeout(later, delay);
    };
  }

  function layerCss() {
    this.idxLayer = null;
  }
  layerCss.prototype = {
    init: function () {
      var self = this;
      self.event();
    },
    event: function () {
      var self = this;
      $(document).on("click", "#abcd", function () {
        var winWidth = $(window).width(),
          winHeight = $(window).height();
        self.idxLayer = layer.open({
          title: '在线调试'
          , content: '<div id="div1">配置各种参数,试试效果</div>'
          , offset: [winHeight - 200, winWidth / 3]  // 固定位置,不随窗口移动
          , type: 1
          , id: 'abc123'
        });
      });
      $(window).on('resize1', function () {
        // 由于抖动问题,不移动
        var winWidth = $(window).width();
        winWidth = parseInt(winWidth / 3) + "px";
        layer.style(self.idxLayer, {
          left: winWidth
        });
        console.log(winWidth);
      });
      // 防抖,不过效果还是不行
      $(window).on("resize", Debounce(function () {
        var winWidth = $(window).width();
        winWidth = parseInt(winWidth / 3) + "px";
        console.log(winWidth);
        layer.style(self.idxLayer, {
          left: winWidth
        });
        console.log(123);
      }, 500))
    },
  };

  var layerCssVar = new layerCss();
  layerCssVar.init();
</script>

</html>
           

方案二:css响应式处理

<!DOCTYPE html>
<html >

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../jquery-2.1.1/jquery.min.js"></script>
  <script src="layer/layer.js"></script>
  <script src="layui/layui.js"></script>
  <link rel="stylesheet" href="layui/css/layui.css" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >
  <style>
    #abcd {
      position: relative;
      padding: 20px;
      top: 50px;
      left: 50px;
      font-size: 20px;
    }
    #layui-layer100001 {
        left: 50px !important;
        color: red;
      }
    @media screen and (min-width:500px) {
      #layui-layer100001 {
        left: 150px !important;
        color: red;
      }
    }
    @media screen and (min-width:1000px) {
      #layui-layer100001 {
        left: 300px !important;
        color: red;
      }
    }

    @media screen and (min-width:1500px) {
      #layui-layer100001 {
        left: 600px !important;
      }
    }

    @media screen and (min-width:2000px) {
      #layui-layer100001 {
        left: 800px !important;
      }
    } 
  </style>
</head>

<body>
  <!-- 点击按钮,弹出窗口,高度不变,左右在某个位置,随窗口的改变而改变位置 -->
  <button id="abcd">abcd</button>

</body>
<script>
  //防抖函数
  function Debounce(fn, delay) {
    var ctx;
    var args;
    var timer = null;

    var later = function () {
      fn.apply(ctx, args);
      timer = null;
    };

    return function () {
      ctx = this;
      args = arguments;
      if (timer) {
        clearTimeout(timer);
        timer = null;
      }
      timer = setTimeout(later, delay);
    };
  }

  function layerCss() {
    this.idxLayer = null;
  }
  layerCss.prototype = {
    init: function () {
      var self = this;
      self.event();
    },
    event: function () {
      var self = this;
      $(document).on("click", "#abcd", function () {
        var winWidth = $(window).width(),
          winHeight = $(window).height();
        self.idxLayer = layer.open({
          title: '在线调试'
          , content: '<div id="div1">配置各种参数,试试效果</div>'
          , offset: [winHeight - 200, winWidth / 3]  // 固定位置,不随窗口移动
          , type: 1
          , id: 'abc123'
        });
      });
      
    },
  };

  var layerCssVar = new layerCss();
  layerCssVar.init();
</script>

</html>
           

方案三:layui的自动居中,offset只设置高度,左右自动居中

<!DOCTYPE html>
<html >

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../jquery-2.1.1/jquery.min.js"></script>
  <script src="layer/layer.js"></script>
  <script src="layui/layui.js"></script>
  <link rel="stylesheet" href="layui/css/layui.css" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >
  <style>
    #abcd {
      position: relative;
      padding: 20px;
      top: 50px;
      left: 50px;
      font-size: 20px;
    }
  </style>
</head>

<body>
  <!-- 点击按钮,弹出窗口,高度不变,左右在某个位置,随窗口的改变而改变位置 -->
  <button id="abcd">abcd</button>

</body>
<script>
  //防抖函数
  function Debounce(fn, delay) {
    var ctx;
    var args;
    var timer = null;

    var later = function () {
      fn.apply(ctx, args);
      timer = null;
    };

    return function () {
      ctx = this;
      args = arguments;
      if (timer) {
        clearTimeout(timer);
        timer = null;
      }
      timer = setTimeout(later, delay);
    };
  }

  function layerCss() {
    this.idxLayer = null;
  }
  layerCss.prototype = {
    init: function () {
      var self = this;
      self.event();
    },
    event: function () {
      var self = this;
      $(document).on("click", "#abcd", function () {
        var winWidth = $(window).width(),
          winHeight = $(window).height();
        self.idxLayer = layer.open({
          title: '在线调试'
          , content: '<div id="div1">配置各种参数,试试效果</div>'
          , offset: [winHeight - 200] 
          , type: 1
          , id: 'abc123'
        });
      });
    },
  };
  var layerCssVar = new layerCss();
  layerCssVar.init();
</script>

</html>