- 做者:陳大魚頭
- github: KRISACHAN
在以前某一個前端技術羣裏,有一個羣友說他面試的時候遇到了一個問題,就是面試官讓他用純 CSS 來實現一個根據鼠標移動位置以爲物體移動方向的 DEMO。css
給出的初始結構以下:html
<style> body { padding: 2em; text-align: center; } .block { position: relative; display: inline-block; width: 10em; height: 10em; vertical-align: middle; } .block_content { position: absolute; top: 0; left: 0; width: 100%; height: 100%; text-align: center; line-height: 10em; background: #333; color: #FFF; } </style>
<p class="text">從不一樣方向使鼠標指針移過下面的內容</p>
<p>↓</p>
<span>→ </span>
<div class="block">
<div class="block_content">
Hover me!
</div>
</div>
<span> ←</span>
<p>↑</p>
複製代碼
效果圖以下:前端
淨會問這種不實用又跟業務沒啥關係的問題,氣抖冷,中國前端何時才能真正的站起來。git
謝謝面試官提出的好問題,我會努力實現出來的。github
因此這個功能真的能用純 CSS 實現嗎?面試
答案是能夠的,首先咱們來分解下思路。微信
首先根據題幹,咱們知道這題是須要用到鼠標操做的,JS 裏咱們有各類mouse
事件,但一樣的,CSS 咱們也有:hover
。ui
這題咱們須要利用到的選擇器就是:hover
了spa
判斷方向 的功能即是本題的核心。指針
從題圖上來看,其實已經給了咱們方向的指引,就是告訴咱們鼠標要經過四個箭頭的方向進入。
而後就是若是要純 CSS 來實現,就是咱們的鼠標必需要觸碰到某個關鍵節點,並且這個節點的某個表現必定是能夠表明這個方位的。
這就是題目給出的兩個隱藏條件。
因此咱們來嘗試下實現。
首先要經過:hover
來觸碰到這個關鍵節點,並且是要在箭頭指向的方向下觸碰觸發,那麼咱們能夠在箭頭所指的方向都加上一個能被觸碰到的物體,例如這樣:
<style> .block_hoverer { position: absolute; width: 100%; height: 100%; z-index: 1; } .block_hoverer:nth-child(1) { background: red; } .block_hoverer:nth-child(2) { background: lime; } .block_hoverer:nth-child(3) { background: orange; } .block_hoverer:nth-child(4) { background: blue; } </style>
<div class="block">
<div class="block_hoverer">上</div>
<div class="block_hoverer">下</div>
<div class="block_hoverer">左</div>
<div class="block_hoverer">右</div>
<div class="block_content">
Hover me!
</div>
</div>
複製代碼
效果以下:
咱們能夠發現,除了 右塊 以外,都被遮住了,嗯,正常現象。
接下來咱們只須要讓這幾個塊退到邊緣便可。
代碼以下:
.block_hoverer {
position: absolute;
z-index: 1;
width: 100%;
height: 100%;
transition: all 0.3s ease;
}
.block_hoverer:nth-child(1) {
background: red;
top: -90%;
}
.block_hoverer:nth-child(2) {
background: lime;
top: 90%;
}
.block_hoverer:nth-child(3) {
background: orange;
left: -90%;
}
.block_hoverer:nth-child(4) {
background: blue;
left: 90%;
}
複製代碼
效果以下:
而後咱們加上過渡:
.block_hoverer {
transition: all 0.3s ease;
}
.block_hoverer:hover {
opacity: 1;
top: 0;
left: 0;
}
複製代碼
效果以下:
一步就是隱藏起來:
.block {
position: relative;
display: inline-block;
overflow: hidden;
width: 10em;
height: 10em;
vertical-align: middle;
}
.block_hoverer {
opacity: 0;
}
.block_hoverer:hover {
opacity: 1;
}
複製代碼
效果以下:
因此咱們有完整代碼以下:
<style> body { padding: 2em; text-align: center; } .block { position: relative; display: inline-block; overflow:hidden; width: 10em; height: 10em; vertical-align: middle; transform: translateZ(0); } .block_hoverer { position: absolute; z-index: 1; width: 100%; height: 100%; opacity: 0; transition: all .3s ease; } .block_hoverer:nth-child(1) { background: red; top: -90%; } .block_hoverer:nth-child(2) { background: lime; top: 90%; } .block_hoverer:nth-child(3) { background: orange; left: -90%; } .block_hoverer:nth-child(4) { background: blue; left: 90%; } .block_hoverer:hover { opacity: 1; top: 0; left: 0; } .block_content { position: absolute; top: 0; left: 0; width: 100%; height: 100%; text-align: center; line-height: 10em; background: #333; color: #FFF; } </style>
<body>
<p class="text">從不一樣方向使鼠標指針移過下面的內容</p>
<p>↓</p>
<span>→ </span>
<div class="block">
<div class="block_hoverer">1</div>
<div class="block_hoverer">2</div>
<div class="block_hoverer">3</div>
<div class="block_hoverer">4</div>
<div class="block_content">
Hover me!
</div>
</div>
<span> ←</span>
<p>↑</p>
</body>
複製代碼
完整效果能夠查看魚頭的codepen
雖然沒什麼軟用,可是應付面試官應該是夠用了。
感謝面試官提出的問題,讓我實現了這個功能,對 CSS 有了更深的理解。
若是你喜歡探討技術,或者對本文有任何的意見或建議,很是歡迎加魚頭微信好友一塊兒探討,固然,魚頭也很是但願能跟你一塊兒聊生活,聊愛好,談天說地。 魚頭的微信號是:krisChans95 也能夠掃碼關注公衆號,訂閱更多精彩內容。