DIV CSS 佈局教程網

用 jQuery 實現上下左右四向滑動切換效果
編輯:JQuery特效代碼     

本文分享一個以 jQuery 實現上下左右四向滑動切換的範例,透過 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>無標題文檔</title>
<script type="text/javascript" src="jquery-1.11.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btn").unbind().click(function(){
$("#first").hide();
$("#sec").css("left","-200px").animate({"left":"0px"},500).show();
});
$("#btn2").unbind().click(function(){
$("#sec").hide();
$("#first").css("left","200px").animate({"left":"0px"},500).show();
});
$("#btn3").unbind().click(function(){
$("#first").hide();
$("#sec").css("top","200px").animate({"top":"0px"},500).show();
});
$("#btn4").unbind().click(function(){
$("#sec").hide();
$("#first").css("top","-200px").animate({"top":"0px"},500).show();
});
});
</script>
</head>
<body>
<div style="width:200px; height:200px;">
<div id="first" style="text-align:center; width:200px; height:200px;position:absolute;">
 <p>第一頁</p>
 <p>第一頁</p>
 <p>第一頁</p>
 <p>第一頁</p>
 <p>第一頁</p>
 </div>
 <div id="sec" style="text-align:center; width:200px; height:200px; display:none;position:absolute;">
 <p>第二頁</p>
 <p>第二頁</p>
 <p>第二頁</p>
 <p>第二頁</p>
 <p>第二頁</p>
 </div>
</div>
<div style="width:200px; height:50px;">
<input type="button" value="向右滾動" id="btn"/>
 <input type="button" value="向左滾動" id="btn2"/>
 <input type="button" value="向上滾動" id="btn3"/>
 <input type="button" value="向下滾動" id="btn4"/>
</div>
</body>
</html>

四個按鈕分別對應四個方向:先把要離開的區塊 hide,再把要進場的區塊從指定偏移位置(left 或 top 的 ±200px)用 animate 在 500ms 內滑回 0,營造滑入效果。特別提醒:程式碼中的 position: absolute; 一定要加上,否則定位偏移不會生效、也就看不到滑動動畫。

希望本文所述對大家的 jQuery 程式設計有所幫助。

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