CSS:从右向左展开<div>以显示文本

时间:2018-09-06 20:22:04

标签: jquery css flexbox hover expand

我想设置一个固定的<div>,其中包含一个图标,将鼠标悬停在该图标上时会从右向左展开(<div>会展开,而不是图标),显示出一个图标左侧的文本。图标的位置保持不变。这是一个草图,可以帮助说明我想做什么:

enter image description here

在悬停时更改<div>的大小是没有问题的,我只是添加了一个具有较大宽度的类(包括用于扩展动画的过渡)。但是我为元素的定位而苦苦挣扎。我将图标和文本(放在<span>中)放置在单独的<div>中,并尝试使用flex来安排所有内容。在展开/悬停状态下,事物看起来应该是应该的样子,但是一旦<div>再次缩小时,包含图标和文本的两个div会尝试共享剩余的少量空间,因此图标被切断并文本被压缩。

如果有人能帮助我弄清楚如何按照我的意愿进行设置,将不胜感激。

这是我到目前为止所了解的内容:Button-GrowOnHover

加上代码本身:

$(document).ready(function() {
  var button = $("#myButton");

  button.mouseenter(function() {
    $(this).addClass("grow");
  });
  button.mouseleave(function() {
    $(this).removeClass("grow");
  });
});
.button-container {
  position: fixed;
  top: 5%;
  right: 5%;
  width: 100px;
  padding: 5px;
  background-color: tomato;
  display: flex;
  justify-content: space-between;
  overflow: hidden;
  transition: width 1s;
}

.button-container.grow {
  width: 300px
}

.button-text-container {
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: lightblue;
}

.button-icon-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="button-container" id="myButton">

  <div class="button-text-container">
    <span>Here comes some text.</span>
  </div>

  <div class="button-icon-container">
    <img src="https://placeholder.pics/svg/100x100/7AFFA6/000000/ICON">
  </div>

</div>

2 个答案:

答案 0 :(得分:5)

如果您依赖flexbox order,并且可以将宽度更改移至文本而不是父容器,则可以仅使用 CSS进行此操作:

.button-container {
  position: fixed;
  top: 5%;
  right: 5%;
  padding: 5px;
  background-color: tomato;
  display: flex;
  justify-content: space-between;
  overflow: hidden;
}


.button-text-container {
  order:-1;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: lightblue;
  white-space:nowrap; /*Keep text always one line*/
  overflow:hidden;
  width:0;
  transition: width 1s;
}

.button-icon-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.button-icon-container:hover + .button-text-container {
  width:200px;
}
<div class="button-container" id="myButton">
  <div class="button-icon-container">
    <img src="https://placeholder.pics/svg/100x100/7AFFA6/000000/ICON">
  </div>
  
  <div class="button-text-container">
    <span>Here comes some text.</span>
  </div>
</div>

答案 1 :(得分:1)

css有更好的选择,但是如果您想使用javascript可以做到这一点。让需要增长的实际容器更改宽度,而不是包装容器的宽度。

public function __construct() {
    $this->middleware('both', ['except' => [ 'methodOne', 'methodTwo' ]]);
}
$(document).ready(function() {
  var button = $("#myButton");

  button.mouseenter(function() {
    $('.button-text-container').addClass("grow");
  });
  button.mouseleave(function() {
    $('.button-text-container').removeClass("grow");
  });
});
.button-container {
  position: fixed;
  top: 5%;
  right: 5%;
  padding: 5px;
  background-color: tomato;
  display: flex;
  justify-content: space-between;

}

.button-text-container.grow {
  width: 300px;
}

.button-text-container {
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: lightblue;
  overflow: hidden;
  width: 0px;
  transition: width 1s;
}

.button-icon-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}