Disable Browser Window Resize

前端 未结 2 1893
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 05:52

For starters... I have no sinister intention of subjecting users to popups or anything like that. I simply want to prevent a user from resizing the browser window of a webpa

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 06:16

    I needed to do this today (for a panel opened by a chrome extension) but I needed to allow the user to change the window height, but prevent them changing the window width

    @Derek's solution got me almost there but I had to tweak it to allow height changes and because of that, an endless resizing loop was possible so I needed to prevent that as well. This is my version of Dereck's answer that is working quite well for me:

    var couponWindow = {
      width: $(window).width(),
      height: $(window).height(),
      resizing: false
    };
    var $w=$(window);
    $w.resize(function() {
      if ($w.width() != couponWindow.width && !couponWindow.resizing) {
        couponWindow.resizing = true;
        window.resizeTo(couponWindow.width, $w.height());
      }
      couponWindow.resizing = false;
    });
    

提交回复
热议问题