javascript - 带有幻灯片动画的 jQuery-UI 可调整大小错误

标签 javascript jquery css jquery-ui

我似乎无法弄清楚 jQueryUI 可调整大小函数正在做什么来导致我创建的“聊天框”div 元素的 anchor 。问题是,当您通过拖动右上角来调整此元素的大小时,它确实会正确调整大小,但是当您按下关闭按钮播放 jQuery 动画以折叠它时,它会在错误的方向上折叠。如果您根本不调整框的大小,则此折叠动画会正常工作。

似乎还有另一个问题,调整它的大小会导致页面上的框跳得更高,但这似乎只发生在 Google Chrome 上,Firefox 工作正常,但不确定为什么!

尝试调整框的大小然后关闭它以查看问题:

$(document).ready(function() {
  // controls resizing of the chat box
  $('.chat_box').resizable({
    handles: 'n, e, ne',
    minWidth: 300,
    minHeight: 100,
    maxWidth: 700,
    maxHeight: 500,
  });
});

function minimize(chatId) {
  var bottom_bar = document.getElementById("bottom_bar");
  var box = bottom_bar.getElementsByClassName("chat_box")[chatId];
  var bar = bottom_bar.getElementsByClassName("chat_bar")[chatId];
  bar.className = "chat_bar chat_box_minimized";

  $(box).stop().animate({
      height: "0px",
      width: bar.offsetWidth,
    },
    'normal', function() {
      $(box).hide();
    }
  );
}
#bottom_bar {
  position: fixed;
  z-index: 10;
  bottom: 0px;
  left: 0px;
  right: 0px;
  max-height: 40px;
  background-color: #0042b3;
  padding: 2px 20px;
}
div.chat_box {
  position: fixed;
  width: 350px;
  height: 180px;
  margin: 0px 4px;
  bottom: 45px;
}
div.close_btn {
  position: absolute;
  top: 0px;
  right: 0px;
  width: 30px;
  height: 100%;
}
div.close_btn:before {
  content: 'x';
  display: block;
  text-align: center;
  vertical-align: middle;
  line-height: 25px;
  font-weight: bold;
  font-family: Arial, sans-serif;
  pointer-events: none;
}
div.close_btn:hover {
  background-color: rgba(0, 9, 26, 0.8);
  cursor: pointer;
}
div.chat_box_maximized {
  background-color: white;
  width: 350px;
  margin: 5px 0px;
  padding: 2px;
  border: 3px solid #0045cc;
  border-radius: 5px;
  display: inline-block;
}
div.chat_box_maximized input {
  width: 100%;
  border: none;
}
div.chat_box_maximized p {
  display: none;
}
div.chat_box_minimized {
  background-color: #002266;
  ;
  max-width: 200px;
  min-width: 80px;
  margin: 5px 0px;
  padding: 2px;
  border: 3px solid #002266;
  ;
  border-radius: 5px;
  display: inline-block;
}
div.chat_box_minimized:hover {
  background-color: #3378ff;
  border: 3px solid #3378ff;
  cursor: pointer;
}
div.chat_box_minimized form {
  display: none;
}
div.chat_box_minimized p {
  margin: 0px 5px;
  font-size: 10pt;
  color: white;
  font-weight: bold;
  pointer-events: none;
}
.light_container,
.dark_container {
  -webkit-box-shadow: 0px 0px 4px 2px rgba(0, 0, 0, 0.57);
  -moz-box-shadow: 0px 0px 4px 2px rgba(0, 0, 0, 0.57);
  box-shadow: 0px 0px 4px 2px rgba(0, 0, 0, 0.57);
  border: 1px solid #005eff;
  padding: 1px;
}
.light_container {
  background-color: rgba(0, 34, 102, 0.9);
}
.dark_container {
  background-color: rgba(0, 9, 26, 0.9);
}
.light_container .body,
.dark_container .body {
  padding: 5px;
}
div.basic_title {
  position: relative;
  width: 100%;
  background-color: #005eff;
  padding: 4px;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
}
div.basic_title p {
  margin: 0px;
  pointer-events: none;
}
div.basic_panel div.basic_title {
  font-size: 14px;
  font-weight: bold;
  text-align: center;
  text-transform: uppercase;
}
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<div id="bottom_bar">
  <div class="chat_box dark_container">
    <div class="basic_title">
      <p>Chat Box</p>
      <div class="close_btn" onclick="minimize(0)"></div>
    </div>
    <div class="body"></div>
  </div>
  <div class="chat_bar chat_box_maximized">
    <p>Chat Box</p>
    <form>
      <input type="text" placeholder="send a message">
    </form>
  </div>
</div>

最佳答案

当您从顶部 handle 调整大小时,它会更改顶部 坐标和高度。由于您使用 bottom 设置位置,通常在动画中高度会改变,但不会改变 bottom 坐标。但是,一旦 resizable 设置了 top 坐标,就会制作动画,但会保留 top 坐标。

您可以做的是使用resize 回调来防止在调整大小时设置top 坐标。然后它会在动画上保持正确的方向,调整大小也会起作用。

$(document).ready(function() {
  // controls resizing of the chat box
  $('.chat_box').resizable({
    handles: 'n, e, ne',
    minWidth: 300,
    minHeight: 100,
    maxWidth: 700,
    maxHeight: 500,
    resize: function(event, ui) {
      ui.helper.css('top', '');
    }
  });
});

function minimize(chatId) {
  var bottom_bar = document.getElementById("bottom_bar");
  var box = bottom_bar.getElementsByClassName("chat_box")[chatId];
  var bar = bottom_bar.getElementsByClassName("chat_bar")[chatId];
  bar.className = "chat_bar chat_box_minimized";

  $(box).stop().animate({
      height: "0px",
      width: bar.offsetWidth,
    },
    'normal', function() {
      $(box).hide();
    }
  );
}
#bottom_bar {
  position: fixed;
  z-index: 10;
  bottom: 0px;
  left: 0px;
  right: 0px;
  max-height: 40px;
  background-color: #0042b3;
  padding: 2px 20px;
}
div.chat_box {
  position: fixed;
  width: 350px;
  height: 180px;
  margin: 0px 4px;
  bottom: 45px;
}
div.close_btn {
  position: absolute;
  top: 0px;
  right: 0px;
  width: 30px;
  height: 100%;
}
div.close_btn:before {
  content: 'x';
  display: block;
  text-align: center;
  vertical-align: middle;
  line-height: 25px;
  font-weight: bold;
  font-family: Arial, sans-serif;
  pointer-events: none;
}
div.close_btn:hover {
  background-color: rgba(0, 9, 26, 0.8);
  cursor: pointer;
}
div.chat_box_maximized {
  background-color: white;
  width: 350px;
  margin: 5px 0px;
  padding: 2px;
  border: 3px solid #0045cc;
  border-radius: 5px;
  display: inline-block;
}
div.chat_box_maximized input {
  width: 100%;
  border: none;
}
div.chat_box_maximized p {
  display: none;
}
div.chat_box_minimized {
  background-color: #002266;
  ;
  max-width: 200px;
  min-width: 80px;
  margin: 5px 0px;
  padding: 2px;
  border: 3px solid #002266;
  ;
  border-radius: 5px;
  display: inline-block;
}
div.chat_box_minimized:hover {
  background-color: #3378ff;
  border: 3px solid #3378ff;
  cursor: pointer;
}
div.chat_box_minimized form {
  display: none;
}
div.chat_box_minimized p {
  margin: 0px 5px;
  font-size: 10pt;
  color: white;
  font-weight: bold;
  pointer-events: none;
}
.light_container,
.dark_container {
  -webkit-box-shadow: 0px 0px 4px 2px rgba(0, 0, 0, 0.57);
  -moz-box-shadow: 0px 0px 4px 2px rgba(0, 0, 0, 0.57);
  box-shadow: 0px 0px 4px 2px rgba(0, 0, 0, 0.57);
  border: 1px solid #005eff;
  padding: 1px;
}
.light_container {
  background-color: rgba(0, 34, 102, 0.9);
}
.dark_container {
  background-color: rgba(0, 9, 26, 0.9);
}
.light_container .body,
.dark_container .body {
  padding: 5px;
}
div.basic_title {
  position: relative;
  width: 100%;
  background-color: #005eff;
  padding: 4px;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
}
div.basic_title p {
  margin: 0px;
  pointer-events: none;
}
div.basic_panel div.basic_title {
  font-size: 14px;
  font-weight: bold;
  text-align: center;
  text-transform: uppercase;
}
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<div id="bottom_bar">
  <div class="chat_box dark_container">
    <div class="basic_title">
      <p>Chat Box</p>
      <div class="close_btn" onclick="minimize(0)"></div>
    </div>
    <div class="body"></div>
  </div>
  <div class="chat_bar chat_box_maximized">
    <p>Chat Box</p>
    <form>
      <input type="text" placeholder="send a message">
    </form>
  </div>
</div>

关于javascript - 带有幻灯片动画的 jQuery-UI 可调整大小错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34976593/

相关文章:

javascript - javascript中命名函数与不命名函数有什么区别?

javascript - Firefox下canvas动画闪烁

javascript - "Parse Error: Header overflow"Node.js GET 请求期间

javascript - 为什么这个 jQuery 返回索引 3?

javascript - 如何使用 id 从 html 调用 javascript

javascript - 使用js ajax从html发布到php帖子

jquery - Bootstrap 4 scrollspy 不包括 id

javascript - 对话框中的 jquery 设置 block

javascript - 如何使用 python/scrapy 抓取网站上小部件的输出?

javascript - React - 如何通过 ref 应用样式