DIV CSS 佈局教程網

jQuery 卡片特效:鼠標懸浮時四邊框依次生長的實現
編輯:JQuery入門技巧     

這是一個視覺細節很不錯的懸浮效果:鼠標移入卡片時,四條邊的彩色線條按順序「長」出來圍成一圈,同時底部浮出信息條;移開後線條依次收回。

效果預覽

實現原理

關鍵在於把「一條邊」拆成一個細長的 div,用 border 上色,然後通過改變它的 width 或 height 來實現生長動畫。四個 div 分別定位在卡片的四個角,配合 delay() 製造錯落的節奏感。

作者提到原始版本是用 addClass 配合 CSS 過渡來實現的,這裡改成 slideUp 式的 animate 寫法,純屬學習性質的改編。

完整代碼

 <!DOCTYPE html>
 <html lang="en">
 <head>
 <meta charset="UTF-8">
 <title>Document</title>
 </head>
 <body>
 <style type="text/css">
 *{margin:0;padding:0;list-style-type:none;}
 a,img{border:0;}
 a{color:#000;text-decoration: none;font-weight: 600}
 body{font:12px/180% Arial, Helvetica, sans-serif, "新宋體";}

 .sph{ width: 1300px; margin: 0 auto; padding: 100px 0; }

 .spbq{
   width: 250px;
   height: 250px;
   float: left;
   line-height: 2;
   padding: 10px 10px 0 20px;
   margin: 20px;
   position: relative;
   overflow: hidden;
 }
 .spbq h2{
   color: #14191e;
   font: bold 14px/18px "Microsoft Yahei","Hiragino Sans GB",Helvetica,"微軟雅黑",Tahoma,Arial,sans-serif;
   margin-top: 18px;
 }
 .spbq span{
   display: block;
   color: #b4bbbf;
   font: 12px/18px "Microsoft Yahei","Hiragino Sans GB",Helvetica,"微軟雅黑",Tahoma,Arial,sans-serif;
   margin: 8px 0;
 }
 .spbq b{
   color: #787d82;
   font: 12px/18px "Microsoft Yahei","Hiragino Sans GB",Helvetica,"微軟雅黑",Tahoma,Arial,sans-serif;
 }

 /* 四條邊:初始尺寸都接近 0 */
 .biankuang{
   width: 3px;
   height: 3px;
   position: absolute;
   z-index: 99;
   border-radius:10px;
 }
 .biankuang_1{
   height: 3px;
   top: -6px;
   left:0px;
   border-left: 3px solid #EB5858;
 }
 .biankuang_2 {
   width: 0px;
   bottom:-3px;
   left: 0px;
   border-top: 3px solid #EB5858;
 }
 .biankuang_3{
   height: 0px;
   bottom:0px;
   right:0px;
   border-right: 3px solid #EB5858;
 }
 .biankuang_4{
   width:0px;
   top:-3px;
   right:0px;
   border-bottom: 3px solid #EB5858;
 }

 .text_gobuy {
   position: absolute;
   z-index: 9;
   bottom: 0;
   left: 0px;
   width: 280px;
   height: 50px;
   overflow: hidden;
   background-color: rgba(32, 32, 33,0.5);
   cursor: pointer;
   display: none;
   text-align: center;
 }
 .text_gobuy_show{
   padding: 20px 15px ;
   opacity: 1;
 }
 .spbq p{
   position: absolute;
   bottom:10px;
   left:110px;
   line-height: 33px;
   color: #fff
 }
 </style>
 </head>
 <body>
 <div class="sph">

 <div class="spbq">
 <div class="biankuang biankuang_1"></div>
 <div class="biankuang biankuang_2"></div>
 <div class="biankuang biankuang_3"></div>
 <div class="biankuang biankuang_4"></div>
 <a href="#">
 <div class="zst"><img src="https://www.jq22.com/demo/jQuery-hover20161208/images/0-135.jpg" alt=""></div>
 <h2>jQuery基礎課程</h2>
 <span>讓jQuery帶您進入網頁動態交互世界,為...</span>
 <b>更新完畢    15783人學習</b>
 </a>
 <div class="text_gobuy">
 <br/>
 <p>9小時17分鐘 | 初級</p>
 <br/>
 </div>
 </div>

 <!-- 重複若干個 .spbq 結構相同的卡片,此處省略 -->

 </div>

 <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script>
 <script type="text/javascript">
 function biankuang(obj) {
   $(obj).find('.biankuang_1').stop(true).animate({ height: '305px' }, 300)
   $(obj).find('.biankuang_2').stop(true).delay('300').animate({ width: '305px' }, 300)
   $(obj).find('.biankuang_3').stop(true).animate({ height: '305px' }, 300)
   $(obj).find('.biankuang_4').stop(true).delay('300').animate({ width: '305px' }, 300)
 }

 function biankuang1(obj) {
   $(obj).find('.biankuang_1').animate({ height: '0' }, 100)
   $(obj).find('.biankuang_2').animate({ width: '0' }, 100)
   $(obj).find('.biankuang_3').animate({ height: '0' }, 100)
   $(obj).find('.biankuang_4').animate({ width: '0' }, 100)
 }

 $('.spbq').hover(function() {
   var obj = $(this);
   obj.find('.text_gobuy').slideDown(300);
   biankuang(obj);
 }, function() {
   var obj = $(this);
   obj.find('.text_gobuy').slideUp(300);
   biankuang1(obj);
 })
 </script>
 </body>
 </html>

代碼要點

四條邊的初始狀態。 有意識地看一下 CSS:biankuang_1 和 _3 是豎線,靠 height 生長;_2 和 _4 是橫線,靠 width 生長。它們的初始尺寸被壓到接近 0,所以靜態時完全看不見。

stop(true) 必不可少。 鼠標快速進出時,動畫會堆隊列,若不清空就會出現「線條反覆抽搐」的現象。stop(true) 表示停止當前動畫並清空後續隊列,是懸浮類特效的標準防護寫法。

delay('300') 製造節奏。 橫線比豎線晚 300ms 啟動,而豎線動畫本身也是 300ms,於是形成「上下 → 左右」的接力效果,比四條線同時生長要靈動得多。

305px 這個魔術數字。 它等於卡片高度 250px 加上上下 padding 與邊距的總和,是硬編碼的。這意味著一旦調整卡片尺寸,四個地方都得同步改。更健壯的寫法是在 JS 裡動態獲取:var h = $(obj).outerHeight(); 然後傳給動畫函數。

可以進一步優化的方向

如果目標瀏覽器支持,用 CSS 的 transform: scale() 配合 transition-delay 能實現同樣效果,而且性能更好(走 GPU 合成)、代碼更短,也不需要依賴 jQuery。此外示例中引用的圖片與 jQuery 均來自外部站點,接入正式項目時建議替換為自有資源,避免熱鏈失效。

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