DIV CSS 佈局教程網

用 onmouseover 與 onmouseout 實現圖片懸停放緩放效果
編輯:JavaScript綜合知識     

為頁面增添一些細膩的交互,往往能顯著提升體驗。本文分享一個小技巧:當鼠標移到圖片上時自動放大,移開後恢復原狀。

原理是給 img 元素綁定 onmouseover 與 onmouseout 事件,分別在移入與移出時調整圖片的寬高樣式。

<!DOCTYPE html> <html> <head> <script> function bigImg(x) { x.style.height="64px"; x.style.width="64px"; } function normalImg(x) { x.style.height="32px"; x.style.width="32px"; } </script> </head> <body> <img onmouseover="bigImg(this)" onmouseout="normalImg(this)" border="0" src="smiley.gif" alt="Smiley" width="32" height="32"> <p>The function bigImg() is triggered when the user moves the mouse pointer over the image.</p> <p>The function normalImg() is triggered when the mouse pointer is moved out of the image.</p> </body> </html>

這裡通過 this 把當前圖片元素傳給函數,bigImg() 放大尺寸、normalImg() 還原尺寸。把同樣的思路套用到文字、卡片等元素上,就能做出豐富的懸停反饋效果。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved