DIV CSS 佈局教程網

用 jQuery 實現感應鼠標自動伸長的輸入框動畫
編輯:JQuery特效代碼     

本篇筆記說明如何用 jQuery 做出一個會隨鼠標移入而以動畫自動伸長的輸入框,並附上完整程式碼供參考。

核心概念是透過 mouseover 與 mouseout 事件搭配 animate(),在鼠標移入時把輸入框寬度動畫放大,移出時再縮回原本尺寸。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery實現感應鼠標動畫效果自動伸長的輸入框</title>
<script type="text/javascript" src="/images/jquery-1.4.2.min.js"></script>
<style type="text/css">
body
{
margin:0; padding:0; font-size:12px;
}
a:link {color:#1553a9; text-decoration: none;}
a:visited {text-decoration:none; color: #1553a9;}
a:hover {text-decoration:none; color: #f46662;}
a:active {text-decoration: none; color:#f46662;}
#main
{
width:500px; margin:0 auto; margin-top:100px;
}
#de
{
display:block;width:100px; height:30px; line-height:30px; float:right; border:#b7b7b7 1px solid; padding-left:5px; border-right:0;
}
#go
{
width:30px; float:right;height:22px; border:#b7b7b7 1px solid; border-left:0; padding-top:10px; padding-left:15px; background-color:#464646; cursor:pointer; color:#FFFFFF;
}
</style>
<script type="text/javascript">
$(function(){
$("#de").mouseover(function(){
 $("#de").animate({"width":"250px"});
}).mouseout(function(){
 $("#de").animate({"width":"100px"});
});});
</script>
</head>
<body>
<div id="main">
<div id="ss"><div id="go">→</div><input type="text" id="de"/></div>
</div>
</body>
</html>

透過 animate() 對寬度做動畫過渡,即可做出直覺有趣的輸入框伸長效果,希望對你的 jQuery 開發有所幫助。

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