1. 程式人生 > >移動端日歷控件 mobiscroll 的簡單使用、參數設置

移動端日歷控件 mobiscroll 的簡單使用、參數設置

wid 窗口 s/4 The show read int ons length

mobiscroll 在性能方面比較好,可選用多種效果,滑動效果也比較順暢。

提供樣式文件和js文件,直接點擊下載,該版本是 mobiscroll 2.13的 官方地址 :https://docs.mobiscroll.com/2-17-2 下載地址: http://note.youdao.com/yws/public/redirect/share?id=95fd7bf95aa4f13bd6ba0f9ed16b6f6b&type=false 需引入jquery。該插件很強大,本文只是簡單的引用日歷。 var theme="ios"; $("#id").mobiscroll().date({//這裏是date,還有time,datetime不在本文範圍。 theme: theme,//樣式,可根據操作系統不同設置不一樣的樣式 lang: "zh", cancelText: "取消", dateFormat: ‘yyyy-mm-dd‘, onBeforeShow: function (inst) { }, endYear: 2016,//可根據當前年份設置 dayText: ‘日‘, monthText: ‘月‘, yearText: ‘年‘, headerText: function(valueText) { var array = valueText.split(‘-‘); return array[0] + "年" + array[1] + "月" + array[2] + "日"; }, onBeforeShow:function(inst){//展示前的事件 inst.settings.readonly=true;//只讀屬性 }, onSelect: function (valueText, inst) {//選擇時事件(點擊確定後),valueText 為選擇的時間, var selectedDate = valueText; } }); 配置裏的theme參數,提供多種樣式供參考: android android-holo android-holo-light android-ics android-ics-light ios(窗口底部劃出) ios7(窗口底部劃出) jqm(感覺類似透明的效果,自己去試試效果) sense-ui wp 更多請參考官方網站 https://docs.mobiscroll.com/2-17-2

mobiscroll : 滑動選擇

2.13.2版本免費,官網(mobiscroll.com)收費

先從官方下載2.13.2體驗版下來,查看例子結合官方API學習( http://docs.mobiscroll.com/2-13-2 )

另外官方還有在線例子:

http://demo.mobiscroll.com/mobile/datetime/date/#display=modal&theme=mobiscroll&lang=en&language=zh

http://demo.mobiscroll.com/select/countrypicker/#language=zh&display=modal

.net 可以在程序包管理控制臺輸入安裝:Install-Package Mobiscroll

下載完成後,保留mobiscroll-2.13.2.full.min.css,mobiscroll-2.13.2.full.min.js , 其它的css、js可刪除

.net視圖引擎可直接渲染mobiscroll控件

[csharp] view plaincopyprint?
  1. @using (Html.BeginForm())
  2. {
  3. @Html.LabelFor(m => m.Name)
  4. @Html.TextBoxFor(m => m.Name)
  5. <br />
  6. @Html.LabelFor(m => m.Birthday)
  7. <!-- Generate a date scroller for the birthday model property-->
  8. @Html.Mobiscroll().DateFor(m => m.Birthday)
  9. <br />
  10. @Html.LabelFor(m => m.Gender)
  11. <!-- create the selectlist used for the select scroller -->
  12. IEnumerable<SelectListItem> genders = new SelectList(new List<string>(){"male", "female"});
  13. @Html.Mobiscroll().SelectFor(m => m.Gender, genders)
  14. <br />
  15. @Html.LabelFor(m => m.FavoriteBook)
  16. <!-- create the selectlist for the books grouped by author -->
  17. Dictionary<string, IEnumerable<SelectListItem>> books = new Dictionary<string, IEnumerable<SelectListItem>>();
  18. books.Add("Adams", new SelectList(new List<string>() {
  19. "The Hitchhiker‘s Guide to the Galaxy",
  20. "The Restaurant at the End of the Universe",
  21. "So Long, and Thanks for All the Fish",
  22. "Life, the Universe and Everything"
  23. }));
  24. books.Add("Asimov", new SelectList(new List<string>() {
  25. "I, Robot",
  26. "The Caves of Steel",
  27. "Foundation"
  28. }));
  29. books.Add("Herbert", new SelectList(new List<string>() {
  30. "Dune",
  31. "God Emperor of Dune",
  32. "Dune Messiah",
  33. "Children of Dune"
  34. }));
  35. @Html.Mobiscroll().SelectFor(m => m.FavoriteBook, books)
  36. <br />
  37. <button type="submit">Send</button>
  38. }


詳情:http://docs.mobiscroll.com/2-14-3/mvc-helpers

以下是本人看了一下API後隨意寫的幾個例子,其實用select去做會更好,此處只是演示,就隨便啦!

自定義年月(去掉年月日的"日"滾輪布局):

[csharp] view plaincopyprint?技術分享圖片技術分享圖片
  1. @{
  2. ViewBag.Title = "taste mobiscroll";
  3. }
  4. @section styles{
  5. <link href="~/Content/mobiscroll-2.13.2.full.min.css" rel="stylesheet" />
  6. <style>
  7. </style>
  8. }
  9. <div class="container">
  10. <input id="date" />
  11. </div>
  12. @section scripts{
  13. <script src="~/Scripts/jquery-1.8.2.min.js"></script>
  14. <script src="~/Scripts/mobiscroll-2.13.2.full.min.js"></script>
  15. <script>
  16. $(function () {
  17. $("#date").mobiscroll().date({
  18. theme: "android-ics light",
  19. lang: "zh",
  20. cancelText: null,
  21. dateFormat: ‘yy/mm‘, //返回結果格式化為年月格式
  22. // wheels:[], 設置此屬性可以只顯示年月,此處演示,就用下面的onBeforeShow方法,另外也可以用treelist去實現
  23. onBeforeShow: function (inst) { inst.settings.wheels[0].length>2?inst.settings.wheels[0].pop():null; }, //彈掉“日”滾輪
  24. headerText: function (valueText) { //自定義彈出框頭部格式
  25. array = valueText.split(‘/‘);
  26. return array[0] + "年" + array[1] + "月";
  27. }
  28. });
  29. })
  30. </script>
  31. }

效果如下圖:

技術分享圖片

treelist 示例一:

[csharp] view plaincopyprint?技術分享圖片技術分享圖片
  1. <style>
  2. .mbsc-android-holo .dwv { text-align:left;text-indent:.8em; }
  3. </style>
  4. <ul id="treelist">
  5. <li>普通班</li><li>VIP班</li><li>特色班</li><li>至尊班</li><li>女子特訓班</li>
  6. </ul>
  7. <script>
  8. $(function () {
  9. $("#treelist").mobiscroll().treelist({
  10. theme: "android-ics light",
  11. lang: "zh",
  12. defaultValue: [Math.floor($(‘#treelist li‘).length/2)],
  13. cancelText: null,
  14. headerText: function (valueText) { return "選擇班級"; }
  15. });
  16. })
  17. </script>


效果如下圖:

技術分享圖片

treelist 示例二:

[csharp] view plaincopyprint?技術分享圖片技術分享圖片
  1. <style>
  2. .mbsc-android-holo .dwv { text-align:left;text-indent:.8em; }
  3. </style>
  4. <ul id="treelist">
  5. <li>
  6. <span>奧迪</span>
  7. <ul>
  8. <li>奧迪A3</li>
  9. <li>奧迪A4L</li>
  10. <li>奧迪A6L</li>
  11. <li>奧迪Q3</li>
  12. <li>奧迪Q5</li>
  13. <li>奧迪A4</li>
  14. <li>奧迪A6</li>
  15. <li>奧迪A1</li>
  16. <li>奧迪A3(進口)</li>
  17. </ul>
  18. </li>
  19. <li>
  20. <span>寶馬</span>
  21. <ul>
  22. <li>寶馬X1</li>
  23. <li>寶馬i3</li>
  24. <li>寶馬1系</li>
  25. <li>寶馬3系</li>
  26. <li>寶馬5系</li>
  27. </ul>
  28. </li>
  29. <li>
  30. <span>奔馳</span>
  31. <ul>
  32. <li>奔馳A級</li>
  33. <li>奔馳C級</li>
  34. <li>奔馳E級</li>
  35. <li>奔馳S級</li>
  36. <li>奔馳GLK級</li>
  37. <li>奔馳CLA級</li>
  38. <li>奔馳CLS級</li>
  39. </ul>
  40. </li>
  41. </ul>
  42. <script>
  43. $(function () {
  44. var i = Math.floor($(‘#treelist>li‘).length / 2),
  45. j = Math.floor($(‘#treelist>li‘).eq(i).find(‘ul li‘).length / 2);
  46. $("#treelist").mobiscroll().treelist({
  47. theme: "android-ics light",
  48. lang: "zh",
  49. defaultValue: [i,j],
  50. cancelText: null,
  51. placeholder: ‘選擇車型‘,
  52. headerText: function (valueText) { return "選擇車型"; },
  53. formatResult: function (array) { //返回自定義格式結果
  54. return $(‘#treelist>li‘).eq(array[0]).children(‘span‘).text() +‘ ‘+ $(‘#treelist>li‘).eq(array[0]).find(‘ul li‘).eq(array[1]).text().trim(‘ ‘);
  55. }
  56. });
  57. })
  58. </script>


效果如圖:

技術分享圖片

移動端日歷控件 mobiscroll 的簡單使用、參數設置