2017-02-18 47 views
0

我不知道如何解決這個問題:在'a'元素懸停時,div放大,另一個'a'移動。修復懸停展開元素

感謝您的幫助

https://jsfiddle.net/0r2v2qyp/

這裏的html:

<a id="btn_agency" class="btn_presentation">Agenzia</a> 
<a id="btn_student" class="btn_presentation">Studente</a> 
<a id="btn_prof" class="btn_presentation">Docente</a> 
<a id="btn_admin" class="btn_presentation">Segreteria</a> 

和CSS:

.btn_presentation { 
    float: left; 
    margin: 10px; 
    color: royalblue; 
    font-size: 20px; 
    position: relative; 
    letter-spacing: 0; 
    text-transform: uppercase; 
    transition: all .2s ease-out; 
} 

.btn_presentation:after { 
    content: ''; 
    position: relative; 
    display: block; 
    margin: 0 auto; 
    width: 0; 
    height: 0; 
    background: red; 
    transition: all .2s ease-out; 
} 

.btn_presentation:hover { 
    letter-spacing: 5px; 
    transition: all .3s ease-in; 
} 

.btn_presentation:hover:after { 
    width: 100%; 
    height: 2px; 
    transition: all .2s ease-in-out; 
} 
+1

應該怎樣,而不是發生? – LGSon

+0

他希望文字在懸停時增加,而其他文字不會被推開。 – Omnitored

+0

正是,受監控 – Toma

回答

1

爲了能夠動畫字母間距,你必須給div的寬度固定足以彌補擴展後的文本。

div { 
 
    display: inline-block; 
 
    margin: 10px; 
 
    width: 120px; 
 
} 
 

 
.btn_presentation { 
 
    display: inline-block; 
 
    color: royalblue; 
 
    font-size: 20px; 
 
    position: relative; 
 
    letter-spacing: 0; 
 
    text-transform: uppercase; 
 
    transition: all .2s ease-out; 
 
} 
 

 
.btn_presentation:after { 
 
    content: ''; 
 
    position: relative; 
 
    display: block; 
 
    margin: 0 auto; 
 
    width: 0; 
 
    height: 0; 
 
    background: red; 
 
    transition: all .2s ease-out; 
 
} 
 

 
.btn_presentation:hover { 
 
    letter-spacing: 5px; 
 
    transition: all .3s ease-in; 
 
} 
 

 
.btn_presentation:hover:after { 
 
    width: 100%; 
 
    height: 2px; 
 
    transition: all .2s ease-in-out; 
 
}
<div> 
 
    <a id="btn_agency" class="btn_presentation">Agenzia</a> 
 
</div> 
 
<div> 
 
    <a id="btn_student" class="btn_presentation">Studente</a> 
 
</div> 
 
<div> 
 
    <a id="btn_prof" class="btn_presentation">Docente</a> 
 
</div> 
 
<div> 
 
    <a id="btn_admin" class="btn_presentation">Segreteria</a> 
 
</div>


的選項可以替代地使用到它的規模transform

div { 
 
    display: inline-block; 
 
    margin: 10px; 
 
} 
 

 
.btn_presentation { 
 
    display: inline-block; 
 
    color: royalblue; 
 
    font-size: 20px; 
 
    position: relative; 
 
    letter-spacing: 0; 
 
    text-transform: uppercase; 
 
    transition: transform .2s ease-out; 
 
} 
 

 
.btn_presentation:after { 
 
    content: ''; 
 
    position: relative; 
 
    display: block; 
 
    margin: 0 auto; 
 
    width: 0; 
 
    height: 2px; 
 
    background: red; 
 
    transition: width .2s ease-out; 
 
} 
 

 
.btn_presentation:hover { 
 
    transform: scale(1.2); 
 
    transition: transform .3s ease-in; 
 
} 
 

 
.btn_presentation:hover:after { 
 
    width: 100%; 
 
    transition: width .2s ease-in-out; 
 
}
<div> 
 
    <a id="btn_agency" class="btn_presentation">Agenzia</a> 
 
</div> 
 
<div> 
 
    <a id="btn_student" class="btn_presentation">Studente</a> 
 
</div> 
 
<div> 
 
    <a id="btn_prof" class="btn_presentation">Docente</a> 
 
</div> 
 
<div> 
 
    <a id="btn_admin" class="btn_presentation">Segreteria</a> 
 
</div>


更新基於評論

這一段話只有一側方式

div { 
 
    display: inline-block; 
 
    margin: 10px; 
 
} 
 

 
.btn_presentation { 
 
    display: inline-block; 
 
    color: royalblue; 
 
    font-size: 20px; 
 
    position: relative; 
 
    letter-spacing: 0; 
 
    text-transform: uppercase; 
 
    transition: transform .2s ease-out; 
 
} 
 

 
.btn_presentation:after { 
 
    content: ''; 
 
    position: relative; 
 
    display: block; 
 
    margin: 0 auto; 
 
    width: 0; 
 
    height: 2px; 
 
    background: red; 
 
    transition: width .2s ease-out; 
 
} 
 

 
.btn_presentation:hover { 
 
    transform: scaleX(1.2); 
 
    transition: transform .3s ease-in; 
 
} 
 

 
.btn_presentation:hover:after { 
 
    width: 100%; 
 
    transition: width .2s ease-in-out; 
 
}
<div> 
 
    <a id="btn_agency" class="btn_presentation">Agenzia</a> 
 
</div> 
 
<div> 
 
    <a id="btn_student" class="btn_presentation">Studente</a> 
 
</div> 
 
<div> 
 
    <a id="btn_prof" class="btn_presentation">Docente</a> 
 
</div> 
 
<div> 
 
    <a id="btn_admin" class="btn_presentation">Segreteria</a> 
 
</div>

+0

謝謝你的答案!即使文本產生了奇怪的效果,我也更喜歡第二種選擇。你知道如何解決它嗎? – Toma

+0

@Toma你的意思是文本得到什麼奇怪的效果? – LGSon

+0

在動畫結束時,這個詞起起來 – Toma