基於jquery實現一個滾動的分步註冊嚮導-附源碼_jquery

來源:互聯網
上載者:User

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

查看示範        下載源碼

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="下一步»" />   </div>  </div>  <div class="page">   -----任意html內容-----   <div class="btn_nav">   <input type="button" class="prev" style="float:left" value="«上一步" />   <input type="button" class="next right" value="下一步»" />   </div>  </div>  <div class="page">   -----任意html內容-----   <div class="btn_nav">   <input type="button" class="prev" style="float:left" value="«上一步" />   <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); });

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.