2017-06-05 65 views
0

可以说我有那些固定的位置,看起来像这样在一个窗口2个元素:如何在左侧打开(展开)块元素?

-------------- 
|  xxx | 
|  xxx | 
|   | 
|  xxx | 
|  xxx | 
|   | 
-------------- 

我想要的元素扩展到左侧上悬停,像这样:

-------------- 
|  xxx | 
|  xxx | 
|   | 
|  xxxxxx | 
|  xxxxxx | 
|   | 
-------------- 

但是,现在它向右扩展。工作示例可以在这里看到:https://jsfiddle.net/eeuho5vq/2/

任何解决方案将不胜感激。

+0

顺便说一句,你并不需要JS来扩大它悬停。只需使用伪':hover'css选择器 – Stalinko

+0

@Stalinko是的我知道,它只是在我的原始代码中,我需要在悬停时做其他事情,但需要js :) –

回答

2

在父级上使用display: flex; flex-direction: column; align-items: flex-end;,并将元素向右对齐。

\t $(".container").each(function(){ 
 
\t \t $(this).hover(function(){ 
 
\t \t \t $(this).toggleClass("show"); 
 
\t \t }) 
 
\t })
#wrapper { 
 
    position: fixed; 
 
    right: 1em; 
 
    display: flex; 
 
    flex-direction: column; 
 
    align-items: flex-end; 
 
} 
 

 
.container { 
 
    width: 2em; 
 
    height: 2em; 
 
    background: yellow; 
 
    margin: 1em 0; 
 
} 
 

 
.container.show { 
 
    width: 4em; 
 
    height: 2em; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="wrapper"> 
 
    <div class="container"> 
 
    <!--element 1--> 
 
    </div> 
 
    
 
    <div class="container"> 
 
    <!--element 2--> 
 
    </div> 
 
    
 
</div>