2017-05-15 114 views
0

有3個街區向右擴展。如何使第三個元素向左擴展並避免移動塊本身(元素必須保持原位)?如何將元素展開到左側?

$(".block").hover(function() { 
 
    $(this).toggleClass('expanded'); 
 
});
.block { 
 
    width: 100px; 
 
    height: 100px; 
 
    border: 1px solid #000; 
 
    overflow: hidden; 
 
    display: inline-block; 
 
    position: absolute; 
 
} 
 
.test { 
 
    width: 100px; 
 
    height: 100px; 
 
    border: 1px solid red; 
 
} 
 
.left, .right { 
 
    display: inline-block; 
 
} 
 
.right { 
 
    background-color: red; 
 
} 
 
.expanded { 
 
    width: 220px; 
 
    position: absolute; 
 
    z-index: 10; 
 
} 
 

 
/* .block:hover { 
 
    width: 220px; 
 
} */ 
 
.block-second { 
 
    left: 120px; 
 
} 
 
.block-third { 
 
    left: 240px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="block"> 
 
    <div class="test left"> 
 
    <p>first</p> 
 
    </div> 
 
    <div class="test right"> 
 
    <p>111</p> 
 
    </div> 
 
</div> 
 

 
<div class="block block-second"> 
 
    <div class="test left"> 
 
    <p>second</p> 
 
    </div> 
 
    <div class="test right"> 
 
    <p>222</p> 
 
    </div> 
 
</div> 
 

 
<div class="block block-third"> 
 
    
 
    <div class="test left"> 
 
    <p>third</p> 
 
    </div> 
 
    <div class="test right"> 
 
    <p>333</p> 
 
    </div> 
 
</div>

試過的位置是:絕對的;頂部:0;右:0; 試圖將塊換成固定寬度的div。無法找到相似的例子。

+0

http://jsfiddle.net/J99uQ/2/ – athi

回答

1

您可以使用以下方法:

$(".block").hover(function() { 
 
     $(this).toggleClass('expanded'); 
 
    });
.block { 
 
     width: 100px; 
 
     height: 100px; 
 
     border: 1px solid #000; 
 
     overflow: hidden; 
 
     display: inline-block; 
 
     position: absolute; 
 
    } 
 
    .test { 
 
     width: 100px; 
 
     height: 100px; 
 
     border: 1px solid red; 
 
    } 
 
    .left, .right { 
 
     display: inline-block; 
 
    } 
 
    .right { 
 
     background-color: red; 
 
    } 
 
    .expanded { 
 
     width: 220px; 
 
     position: absolute; 
 
     z-index: 10; 
 
    } 
 
    
 
    .block-third.expanded{ 
 
    left:120px; 
 
    } 
 
    
 
    
 

 
    /* .block:hover { 
 
     width: 220px; 
 
    } */ 
 
    .block-second { 
 
     left: 120px; 
 
    } 
 
    .block-third { 
 
     left: 240px; 
 
    } 
 
    .block-third .right{ 
 
      display:none; 
 
      } 
 
    .block-third.expanded .right{ 
 
      display:inline-block; 
 
      } 
 
    .block-third.expanded .left{ 
 
     margin-left: 12px; 
 
      }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
    <div class="block"> 
 
     <div class="test left"> 
 
     <p>first</p> 
 
     </div> 
 
     <div class="test right"> 
 
     <p>111</p> 
 
     </div> 
 
    </div> 
 

 
    <div class="block block-second"> 
 
     <div class="test left"> 
 
     <p>second</p> 
 
     </div> 
 
     <div class="test right"> 
 
     <p>222</p> 
 
     </div> 
 
    </div> 
 

 
    <div class="block block-third"> 
 
     
 
     <div class="test right"> 
 
     <p>333</p> 
 
     </div> 
 
     
 
     <div class="test left"> 
 
     <p>third</p> 
 
     </div> 
 
    </div>

0

試試這個片段,這是你在找什麼?

$(".block").hover(function() { 
 
    $(this).toggleClass('expanded'); 
 
}); 
 
$(".block.block-third").hover(function() { 
 
    $(this).toggleClass('third'); 
 
});
.block { 
 
    width: 100px; 
 
    height: 100px; 
 
    border: 1px solid #000; 
 
    overflow: hidden; 
 
    display: inline-block; 
 
    position: absolute; 
 
} 
 
.test { 
 
    width: 100px; 
 
    height: 100px; 
 
    border: 1px solid red; 
 
} 
 
.left, .right { 
 
    display: inline-block; 
 
} 
 
.right { 
 
    background-color: red; 
 
} 
 
.expanded:not(.block-third) { 
 
    width: 220px; 
 
    position: absolute; 
 
    z-index: 10; 
 
} 
 
.expanded.third .right { 
 
    left: 10px; 
 
    position: absolute; 
 
    text-align: left; 
 
} 
 

 
.expanded.third { 
 
    width: 220px; 
 
    z-index: 10; 
 
    right: 0; 
 
    text-align: right; 
 
} 
 

 
.block-second { 
 
    left: 120px; 
 
} 
 
.block-third { 
 
    left: 240px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="block"> 
 
    <div class="test left"> 
 
    <p>first</p> 
 
    </div> 
 
    <div class="test right"> 
 
    <p>111</p> 
 
    </div> 
 
</div> 
 

 
<div class="block block-second"> 
 
    <div class="test left"> 
 
    <p>second</p> 
 
    </div> 
 
    <div class="test right"> 
 
    <p>222</p> 
 
    </div> 
 
</div> 
 

 
<div class="block block-third"> 
 
    
 
    <div class="test left"> 
 
    <p>third</p> 
 
    </div> 
 
    <div class="test right"> 
 
    <p>333</p> 
 
    </div> 
 
</div>

相關問題