简体   繁体   English

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

[英]CSS: Expanding a <div> from right to left to reveal text

I'd like to set up a fixed <div> , containing an icon, which expands from right to left when being hovered over (the <div> expands, not the icon), revealing a piece of text on the left side of the icon. 我想设置一个固定的<div> ,其中包含一个图标,当它悬停时会从右向左扩展( <div>展开,而不是图标),在左侧显示一段文字。图标。 The icon's position stays unchanged. 图标的位置保持不变。 Here's a sketch to help illustrate what I'd like to do: 这是一个草图,以帮助说明我想做的事情:

在此输入图像描述

Changing the size of the <div> on hover is no issue, I simply add a class with a larger width (including a transition to animate the expansion). 在悬停时更改<div>的大小没有问题,我只需添加一个宽度更大的类(包括转换为扩展设置动画)。 But I struggle with the positioning of the elements. 但我对元素的定位感到困惑。 I placed both the icon and the text (inside a <span> ) in separate <div> and tried arranging everything using flex . 我将图标和文本(在<span> )放在单独的<div>并尝试使用flex安排所有内容。 In the expanded/hover state things look the way they should, but once the <div> shrinks down again, the two divs containing the icon and the text try sharing the little space that is left and hence the icon gets cut off and the text gets squished. 在扩展/悬停状态下,事情看起来应该是这样,但是一旦<div>再次缩小,包含图标和文本的两个div会尝试共享剩下的小空间,因此图标会被截断并且文本被删除被压扁了。

Would appreciate if someone could help me figure out how set this up the way I'd like it to be. 如果有人能帮助我弄清楚如何按照我希望的方式设置它,我将不胜感激。

Here's a jsFiddle of what I got so far: Button-GrowOnHover 这是我到目前为止所获得的jsFiddleButton-GrowOnHover

Plus the code itself: 加上代码本身:

 $(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> 

You can do this with only CSS if you rely on flexbox order and you can move the width changes to the text instead of the parent container: 如果您依赖于flexbox 顺序 ,则可以仅使用 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> 

There are better options with css but this does the trick if you want to do it with javascript. 有更好的css选项,但如果你想用javascript做这个就行了。 Let the actual container that needs to grow change width instead of the one that wraps it. 让需要增长的实际容器改变宽度而不是包装它的那个。

 $(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; } 
 <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM