DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> WEB網站前端 >> 網頁特效代碼
jQuery 圖片牆:滑鼠移入滑出遮罩文字
編輯:網頁特效代碼     

圖片列表要怎麼在滑鼠移上去時浮出一層半透明資訊卡?做法是每個圖片格裡都預先藏一個絕對定位的遮罩層,再用 animate() 改它的 top 與 opacity,讓它從上方滑下來。

線上體驗

https://hovertree.com/texiao/jquery/2.htm

版面結構

整個圖片牆用 dl / dt / dd 搭建:左側是一張大圖(dt),右側再切成兩個群組,一組放三張小方圖,另一組放五張長條圖,最後靠 CSS 浮動拼成一個 981×382 的拼貼畫面。每個圖片格內都有一個 <div class="hovertreebox">,裡面放標題、簡介與「查看詳細」連結。

樣式

*{margin:0;padding:0;list-style-type:none;}
a,img{border:0;}
body{color:#898989;font:12px/180% Arial, Helvetica, sans-serif, "新宋體";}
a{color:#898989;text-decoration:none}
a:hover{cursor:pointer;color:#9f8054;}
/* hovertree_img */
.hovertree_img{width:981px;height:382px;overflow:hidden;border:1px solid #dcdcdc;padding:3px;margin:40px auto 0 auto;}
.hovertree_img dt{margin-right:3px;float:left;display:inline;width:315px;height:382px;position:relative;overflow:hidden;}
.hovertree_img dd{float:left;position:relative;overflow:hidden;}

.hovertree_img_dd1{width:196px;height:382px;float:left;margin-right:3px;display:inline;}
.hovertree_img_dd1 dd{height:125px;overflow:hidden;float:left;}
.hovertree_img_dd1 #dd2{margin:3px 0px 4px 0;}

.hovertree_img_dd2{float:left;width:464px;height:382px;overflow:hidden;}
.hovertree_img_dd2 dd{margin-right:3px;height:207px;overflow:hidden;display:inline;}
.hovertree_img_dd2 dd#dd3, .hovertree_img_dd2 dd#dd4{margin:0;}
.hovertree_img_dd2 dd#dd3, .hovertree_img_dd2 dd#dd5{margin-bottom:3px;height:172px;}

.hovertreebox{width:100%;height:100%;background:url(2/images/sgw_bg.jpg) no-repeat left top #020000;position:absolute;left:0;line-height:18px;color:#e69714;top:-382px;filter:alpha(Opacity=80);-moz-opacity:0.8;opacity: 0.8;text-align:center;padding-top:30px;}
.hovertreebox h3{color:#fff;font-size:14px;font-weight:bold;}
.hovertreebox a{display:block;margin-top:5px;}
.hovertree_img dt .hovertreebox{padding-top:100px;top:-482px;}

腳本

<script type="text/javascript" src="https:///keleyi/pmedia/jquery/jquery-1.11.2.min.js"></script>
<script type="text/javascript">
$(function(){
 $('#.hove'+'rtree_img dt').hover(function(){
 $(this).children('.hovertreebox').stop(true,true).delay(100).animate({'top':0,opacity:0.8},300);
 },function(){
 $(this).children('.hovertr'+'eebox').stop(true,true).animate({'top':-482,opacity:0},200);
 })
 $('.hovertree_img dd').hover(function(){
 $(this).children('.hovertreebox').stop(true,true).animate({'top':0,opacity:0.8},300);
 },function(){
 $(this).children('.hovertreebox').stop(true,true).animate({'top':-382,opacity:0},200);
 })
})
</script>

HTML 主體

<div style="width:768px;margin:2px auto;"><h3>jQuery圖片列表鼠標經過遮罩顯示文字</h3>
<a href="https://hovertree.com">首頁</a> <a href="https://hovertree.com/hvtart/bjae/74cc61ed089a2991.htm">原文</a> <a href="https://hovertree.com/texiao/">更多特效</a>
<br />

請移動光標到圖片上</div>
<dl class="hovertree_img">
 <dt>
 <img src="https://hovertree.com/texiao/jquery/2/images/t1.png" width="315" height="382"/>
 <div class="hovertreebox">
 <h3>揭秘化妝水四大隱藏功效</h3>
 <p>介紹...</p>
 <a href="https://hovertree.com/">查看詳細</a>
 </div>
 </dt>
 <div class="hovertree_img_dd1">
 <dd>
 <img src="https://hovertree.com/texiao/jquery/2/images/t2.png" width="196" height="125"/>
 <div class="hovertreebox">
 <h3>標題</h3>
 <p>介紹...</p>
 <a href="https://hovertree.com/">查看詳細</a>
 </div>
 </dd>
 <dd id="dd2">
 <img src="https://hovertree.com/texiao/jquery/2/images/t3.png" width="196" height="126"/>
 <div class="hovertreebox">
 <h3>標題</h3>
 <p>介紹...</p>
 <a href="https://hovertree.com/">查看詳細</a>
 </div>
 </dd>
 <dd>
 <img src="https://hovertree.com/texiao/jquery/2/images/t4.png" width="196" height="125"/>
 <div class="hovertreebox">
 <h3>標題</h3>
 <p>介紹...</p>
 <a href="https://hovertree.com/">查看詳細</a>
 </div>
 </dd>
 </div>
 <div class="hovertree_img_dd2">
 <dd id="dd5">
 <img src="https://hovertree.com/texiao/jquery/2/images/t5.png" width="284" height="172"/>
 <div class="hovertreebox">
 <h3>標題</h3>
 <p>介紹...</p>
 <a href="https://hovertree.com/">查看詳細</a>
 </div>
 </dd>
 <dd id="dd3">
 <img src="https://hovertree.com/texiao/jquery/2/images/t6.png" width="176" height="172"/>
 <div class="hovertreebox">
 <h3>標題</h3>
 <p>介紹...</p>
 <a href="https://hovertree.com/">查看詳細</a>
 </div>
 </dd>
 <dd>
 <img src="https://hovertree.com/texiao/jquery/2/images/t7.png" width="152" height="207"/>
 <div class="hovertreebox">
 <h3>標題</h3>
 <p>介紹...</p>
 <a href="https://hovertree.com/">查看詳細</a>
 </div>
 </dd>
 <dd>
 <img src="https://hovertree.com/texiao/jquery/2/images/t8.jpg" width="152" height="207"/>
 <div class="hovertreebox">
 <h3>標題</h3>
 <p>介紹...</p>
 <a href="https://hovertree.com/">查看詳細</a>
 </div>
 </dd>
 <dd id="dd4">
 <img src="https://hovertree.com/texiao/jquery/2/images/t9.png" width="152" height="207" />
 <div class="hovertreebox">
 <h3>標題</h3>
 <p>介紹...</p>
 <a href="https://hovertree.com/">查看詳細</a>
 </div>
 </dd>
 </div>
</dl>

幾個關鍵細節

遮罩為什麼要藏在圖片上方?因為父容器設了 position:relative; overflow:hidden,遮罩用 top:-382px 停在可视範圍外,滑入時把 top 動畫到 0,就出現「從上往下蓋下來」的效果。大圖的遮罩高度較高,所以起始值改成 -482px。

為什麼要寫 stop(true,true)?滑鼠快速來回移動時,動畫會堆疊導致遮罩卡住或閃爍,stop 會先清掉佇列中的舊動畫並跳到最終狀態,讓每次移入移出都乾淨俐落。

為什麼要 delay(100)?刻意留一點延遲,可以避開滑鼠只是路過就觸發的誤判,體驗上更從容。

半透明怎麼處理?這裡同時寫了 opacity、-moz-opacity 與 IE 專用的 filter:alpha(Opacity=80),是照顧舊版瀏覽器的老三段寫法。現代的專案直接用 opacity 或 rgba() 背景色就好。

相容性

適用瀏覽器:IE8、360、FireFox、Chrome、Safari、Opera、傲游、搜狗、世界之窗。

來源:何問起

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