【问题标题】:Making a resizable icon stick to the bottom of a dynamically sized div使可调整大小的图标粘在动态大小的 div 的底部
【发布时间】:2018-11-26 22:45:51
【问题描述】:

我正在尝试处理一个由鼠标悬停信息填充的 div。

它填充到的 div 既可调整大小又可拖动,而且效果很好。我的小问题是,当盒子弯曲时,可调整大小的图标保持在原来的位置。

我想看看是否可以让可调整大小的图标在 div 变大时保持在底部?

$(".btn").mouseenter(function() {
  $("#thepoem").html($(this).val());

});
$(".btn").mouseleave(function() {
  $("#thepoem").empty();
});

$("#superresizablediv").resizable();
$("#superresizablediv").draggable();
#superresizablediv {
  position: absolute;
  top: 15%;
  left: 5%;
  min-width: 250px;
  min-height: 50px;
  background-color: #5fc0e3;
  border-top-left-radius: 10px;
  border-bottom-left-radius: 10px;
  border-top-right-radius: 10px;
  z-index: 1
}

#thepoem {
  display: flex;
  display: inherit;
  min-height: 50px;
  min-width: 250px;
  background-color: #5fc0e3;
  border-top-left-radius: 10px;
  border-bottom-left-radius: 10px;
  border-top-right-radius: 10px;
}
<link href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js" type="text/javascript"></script>
<button class="btn" value="Once upon a midnight dreary, while I pondered, weak and weary, Over many a quaint and curious volume of forgotten lore— While I nodded, nearly napping, suddenly there came a tapping, As of some one gently rapping, rapping at my chamber door. “’Tis some
      visitor,” I muttered, “tapping at my chamber door— Only this and nothing more.">The raven verse 1</button>
<button class="btn" value="Ah, distinctly I remember it was in the bleak December;
And each separate dying ember wrought its ghost upon the floor.
    Eagerly I wished the morrow;—vainly I had sought to borrow
    From my books surcease of sorrow—sorrow for the lost Lenore—
For the rare and radiant maiden whom the angels name Lenore—
            Nameless here for evermore.">The raven verse 2</button>
<div id="superresizablediv" class="ui-resizable-se">
  <div id="thepoem"></div>
</div>

【问题讨论】:

    标签: javascript jquery html css resize


    【解决方案1】:

    你需要在 mouseenter 上更新#superresizablediv 的高度,在 mouseleave 上重置高度

    var height = '';
    
    $(".btn").mouseenter(function() {
      height = $("#superresizablediv").outerHeight();
      $("#thepoem").html($(this).val());
      $("#superresizablediv").height($("#thepoem").outerHeight())
    
    });
    $(".btn").mouseleave(function() {
      $("#thepoem").empty();
      $("#superresizablediv").height(height)
    });
    
    $("#superresizablediv").resizable();
    $("#superresizablediv").draggable();
    #superresizablediv {
      position: absolute;
      top: 15%;
      left: 5%;
      min-width: 250px;
      min-height: 50px;
      background-color: #5fc0e3;
      border-top-left-radius: 10px;
      border-bottom-left-radius: 10px;
      border-top-right-radius: 10px;
      z-index: 1
    }
    
    #thepoem {
      display: flex;
      display: inherit;
      min-height: 50px;
      min-width: 250px;
      background-color: #5fc0e3;
      border-top-left-radius: 10px;
      border-bottom-left-radius: 10px;
      border-top-right-radius: 10px;
    }
    <link href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js" type="text/javascript"></script>
    <button class="btn" value="Once upon a midnight dreary, while I pondered, weak and weary, Over many a quaint and curious volume of forgotten lore— While I nodded, nearly napping, suddenly there came a tapping, As of some one gently rapping, rapping at my chamber door. “’Tis some
          visitor,” I muttered, “tapping at my chamber door— Only this and nothing more.">The raven verse 1</button>
    <button class="btn" value="Ah, distinctly I remember it was in the bleak December;
    And each separate dying ember wrought its ghost upon the floor.
        Eagerly I wished the morrow;—vainly I had sought to borrow
        From my books surcease of sorrow—sorrow for the lost Lenore—
    For the rare and radiant maiden whom the angels name Lenore—
                Nameless here for evermore.">The raven verse 2</button>
    <div id="superresizablediv" class="ui-resizable-se">
      <div id="thepoem"></div>
    </div>

    【讨论】:

    • 谢谢!有用。我接受了你的回答!它引起了另一个意想不到的问题,即 div 当然会返回到原始大小$("#superresizablediv").height(height),现在我想弄清楚如果我调整框高度,它会将最小高度设置为不小于我拖过去。宽度不会缩小,但高度会。提示?
    • 我不确定我是否理解您想要实现的目标。目前该框将返回到高度 - 即使调整大小 - 它是在填充文本之前。
    • 是的,它有点复杂。这个想法是可以通过单击并拖动手柄来调整框的大小,但是当框为空时,它会恢复为 min-height 属性。我想知道,通过手动将调整大小手柄拖动到一个新的高度,比如说 500px,它会设置 css,所以在我再次拖动它之前,框不会缩小到 500px 以下
    猜你喜欢
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多