首頁  >  文章  >  web前端  >  基於jquery實作一個滾動的分步註冊精靈-附源碼_jquery

基於jquery實作一個滾動的分步註冊精靈-附源碼_jquery

WBOY
WBOY原創
2016-05-16 15:42:201447瀏覽

先給大家展示效果圖,需要的朋友可以下載原始碼哦~

查看示範        下載來源碼

HTML

首先要載入jquery庫和捲動插件scrollable.js

<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript" src="scrollable.js"></script> 

接著構造html主體結構。

 <form action="#" method="post"> 
 <div id="wizard"> 
 <ul id="status"> 
 <li class="active"><strong>1.</strong>创建账户</li> 
 <li><strong>2.</strong>填写联系信息</li> 
 <li><strong>3.</strong>完成</li> 
 </ul> 
 
 <div class="items"> 
 <div class="page"> 
  -----任意html内容----- 
  <div class="btn_nav"> 
  <input type="button" class="next right" value="下一步&raquo;" /> 
  </div> 
 </div> 
 <div class="page"> 
  -----任意html内容----- 
  <div class="btn_nav"> 
  <input type="button" class="prev" style="float:left" value="&laquo;上一步" /> 
  <input type="button" class="next right" value="下一步&raquo;" /> 
  </div> 
 </div> 
 <div class="page"> 
  -----任意html内容----- 
  <div class="btn_nav"> 
  <input type="button" class="prev" style="float:left" value="&laquo;上一步" /> 
  <input type="button" class="next right" id="sub" value="确定" /> 
  </div> 
 </div> 
 </div> 
 </div> 
</form>

以上是一個註冊表單,注意在三個.page裡可以填入任何你想要的html表單內容。限於篇幅本文沒有列出表單具體內容。

CSS

#wizard {border:5px solid #789;font-size:12px;height:450px;margin:20px auto; 
width:570px;overflow:hidden;position:relative;} 
#wizard .items{width:20000px; clear:both; position:absolute;} 
#wizard .right{float:right;} 
#wizard #status{height:35px;background:#123;padding-left:25px !important;} 
#status li{float:left;color:#fff;padding:10px 30px;} 
#status li.active{background-color:#369;font-weight:normal;} 
.input{width:240px; height:18px; margin:10px auto; line-height:20px; 
border:1px solid #d3d3d3; padding:2px} 
.page{padding:20px 30px;width:500px;float:left;} 
.page h3{height:42px; font-size:16px; border-bottom:1px dotted #ccc; margin-bottom:20px; 
 padding-bottom:5px} 
.page h3 em{font-size:12px; font-weight:500; font-style:normal} 
.page p{line-height:24px;} 
.page p label{font-size:14px; display:block;} 
.btn_nav{height:36px; line-height:36px; margin:20px auto;} 
.prev,.next{width:100px; height:32px; line-height:32px; background:url(btn_bg.gif) 
repeat-x bottom; border:1px solid #d3d3d3; cursor:pointer}

您可以依照實際應用環境修改css,來體現不同的外觀。

jQuery

毋庸置疑,和其他插件一樣,直接呼叫方法。

$(function(){ 
 $("#wizard").scrollable(); 
}); 

就是這麼簡單,現在可以透過點擊下一步切換步驟了,但有問題是導航的步驟標題tab樣式沒有同時切換,點擊下一步時應該驗證目前表單輸入的合法性。值得慶幸的是,兩個方法讓問題變得簡單了。

onSeek:function();當捲動目前page後發生的事情,本例我們要切換tab樣式。
onBeforeSeek:function();捲動之前發生的事情,本例我們要驗證表單。

請看代碼:

$(function(){ 
 $("#wizard").scrollable({ 
 onSeek: function(event,i){ //切换tab样式 
  $("#status li").removeClass("active").eq(i).addClass("active"); 
 }, 
 onBeforeSeek:function(event,i){ //验证表单 
  if(i==1){ 
  var user = $("#user").val(); 
  if(user==""){ 
   alert("请输入用户名!"); 
   return false; 
  } 
  var pass = $("#pass").val(); 
  var pass1 = $("#pass1").val(); 
  if(pass==""){ 
   alert("请输入密码!"); 
   return false; 
  } 
  if(pass1 != pass){ 
   alert("两次密码不一致!"); 
   return false; 
  } 
  } 
 } 
 }); 
}); 

最後,要提交表單,取得表單的值,你可以在最後一步「確定」按鈕上綁定submit()方法,透過action提交表單。本文為了示範,採用以下方法取得輸入的內容:

$("#sub").click(function(){ 
 var data = $("form").serialize(); 
 alert(data); 
});

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn