DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> CSS入門知識 >> CSS詳解 >> 利用img和div標簽實現圖像下拉列表的模擬
利用img和div標簽實現圖像下拉列表的模擬
編輯:CSS詳解     

dropdownlist本身不支持圖像列表,那麼我們利用jquery來實現下拉列表的模擬。

如圖

下拉列表 下拉列表 下拉列表 下拉列表

其實很簡單,不用自定義控件之類,因為我們在mvc下不能使用ASP.Net服務器控件,所以我們只用了div和img標簽實現這個功能

1.首先介紹一下數據庫表結構,這是下拉列表用到的數據

用到的表

下拉列表數據

src="https://www.divcss.online/divcssbuju/UploadFiles_7251/201612/2016123017110012.png"

下拉列表數據

2.創建 company,department的錄入窗體

如下圖,因為不是重點,不再介紹,只要注意一點,company.id為自增int,注意屬性窗口中auto Generated Value為true,Auto-Sync為OnInsert,這樣linq才可沒錯

屬性窗口

屬性窗口

  1. [AcceptVerbs(HttpVerbs.Post)]
  2. public ActionResult CreateCompany(string name, string brIEf)
  3. {
  4. string strResult = "失敗";
  5. ServiceDataContext db = new ServiceDataContext();
  6. Company com = new Company();
  7. com.Name = name;
  8. com.Brief = brIEf;
  9. try
  10. {
  11. db.CompanIEs.InsertOnSubmit(com);
  12. db.SubmitChanges();
  13. strResult = "成功";
  14. }
  15. catch
  16. {
  17. strResult = "失敗,名稱重復?";
  18. }
  19. return JSon(strResult);
  20. }

3.設計下拉菜單(這才是我想說的重點)

  1. <td>
  2. <img id="imgDefault" src='<%=ViewData["selectedUrl"]%>' class="imgCSS" alt="" /><img
  3. id="imgArrow" src="<%=VIEwData["arrowUrl"]%>" class="DropdownCSS" alt="" />
  4. <div id="divList" class="scrolldivHidden">
  5. <%=VIEwData["imageTable"]%>div>
  6. <%=Html.Hidden("head") %>
  7. td>

這裡selectedUrl為選擇的圖像,有默認值,arrowUrl為下拉圖標,VIEwData["imageTable"]是下拉列表框

這裡用到的CSS

  1. .Visible
  2. {
  3. visibility: visible;
  4. display: block;
  5. }
  6. .Hidden
  7. {
  8. visibility: hidden;
  9. display: none;
  10. }
  11. .imgCSS
  12. {
  13. cursor: hand;
  14. width: 18px;
  15. height: 18px;
  16. border: 1px solid #c3c3c3;
  17. }
  18. .DropdownCSS
  19. {
  20. cursor: hand;
  21. width: 9px;
  22. height: 9px;
  23. vertical-align: top;
  24. }
  25. .scrolldivHidden
  26. {
  27. position:absolute;
  28. width: 36px;
  29. height: 50px;
  30. z-index: 1;
  31. left: 0px;
  32. top: 0x;
  33. overflow-y: auto;
  34. overflow-x:hidden;
  35. display:none;
  36. }
  37. .scrolldivVisible
  38. {
  39. position:absolute;
  40. width: 36px;
  41. height: 50px;
  42. z-index: 1;
  43. left: 0px;
  44. top: 0x;
  45. overflow-y: auto;
  46. overflow-x:hidden;
  47. display:block;
  48. }

主要我們用到jquery來實現選擇等功能

這裡用到jquery一個選擇框的插件

  1. /*
  2. * Manipulation for Html SELECT with jQuery
  3. * Created by Baldwin (http://www.dnnsun.com)
  4. * version: 1.0 (02/03/2009)
  5. * @requires jQuery v1.2 or later
  6. */
  7. ; (function($) {
  8. /* Clear all options */
  9. $.fn.clearSelect = function() {
  10. return this.each(function() {
  11. if (this.tagName == 'SELECT') this.options.length = 0;
  12. });
  13. }
  14. /* Fill the options with the object array: [{'Text':'Hello','Value':'1'}]*/
  15. $.fn.fillSelect = function(data) {
  16. return this.clearSelect().each(function() {
  17. if (this.tagName == 'SELECT') {
  18. var dropdownList = this;
  19. if (data && data.length > 0) {
  20. $.each(data, function(index, optionData) {
  21. var option = new Option(optionData.Text, optionData.Value);
  22. if ($.browser.msIE)
  23. dropdownList.add(option);
  24. else
  25. dropdownList.add(option, null);
  26. });
  27. dropdownList.disabled = false;
  28. }
  29. else
  30. dropdownList.disabled = true;
  31. }
  32. });
  33. }
  34. /* loading when applying AJax fillSelect */
  35. $.fn.loadSelect = function(loadText) {
  36. var data = [{ 'Text': loadText, 'Value': ''}];
  37. this.fillSelect(data);
  38. }
  39. /* selected the target option with value */
  40. $.fn.selected = function(value) {
  41. return this.each(function() {
  42. if (this.tagName == 'SELECT') {
  43. var options = this.options;
  44. if (options.length > 0) {
  45. $.each(options, function(index, optionData) {
  46. // once match then exist loop
  47. if (optionData.value == value) {
  48. optionData.selected = true;
  49. return false;
  50. }
  51. });
  52. }
  53. }
  54. });
  55. }
  56. /* TODO:selected the target option with text */
  57. $.fn.selectedText = function(text) {
  58. return this.each(function() {
  59. if (this.tagName == 'SELECT') {
  60. var options = this.options;
  61. if (options.length > 0) {
  62. $.each(options, function(index, optionData) {
  63. // once match then exist loop
  64. if (optionData.text == text) {
  65. optionData.selected = true;
  66. return false;
  67. }
  68. });
  69. }
  70. }
  71. });
  72. }
  73. /* returns the selected value */
  74. $.fn.getSelected = function() {
  75. return $(this).val();
  76. }
  77. /* return the text of selected option */
  78. $.fn.getSelectedText = function() {
  79. return $(this).children("[@selected]").text();
  80. }
  81. })(jQuery);
  82. function getDepartment(data) { $("#department").fillSelect(data); }
  83. $("#company").change(function() {
  84. $.getJSON("GetDepartment?company=" + $("#company").val(), null, getDepartment);
  85. });

實現了聯動框

下面的代碼實現了下拉和選擇功能

  1. $("#imgArrow,#imgDefault").toggle(function() {
  2. $("#divList").CSS("left", $("#imgDefault").CSS("left"));
  3. $("#divList").slideToggle("fast");
  4. }, function() {
  5. $("#divList").CSS("left", $("#imgDefault").CSS("left"));
  6. $("#divList").slideToggle("fast");
  7. });
  8. $("#head").val($("#imgDefault").attr("src"));
  9. $("img:.imgList").click(function() {
  10. $("#imgDefault").attr("src", $(this).attr("src"));
  11. $("#divList").slideToggle("fast");
  12. $("#head").val($("#imgDefault").attr("src"));
  13. }).hover(function() {
  14. $(this).removeClass().addClass("alpha");
  15. }, (function() {
  16. $(this).removeClass();
  17. }));
  18. $("body").click(function() {
  19. $("#divList").slideUp("fast");
  20. $("#imgDefault").hover(function() {
  21. $(this).addClass("alpha");
  22. }, function() {
  23. $(this).removeClass();
  24. });
  25. });

還有我們用到的action

  1. public ActionResult CreateService()
  2. {
  3. VIEwData["company"] = GetCompanyList();
  4. List blank=new List();
  5. blank.Add(GetBlankDepartment());
  6. VIEwData["department"] = blank.ToArray();
  7. VIEwData["head"] = GetHeadList();
  8. VIEwData["selectedUrl"] = Url.Content(Head_Folder + "Head/head0.gif");
  9. VIEwData["arrowUrl"] = Url.Content(Head_Folder + "/dropdown.gif");
  10. VIEwData["imageTable"] = GetImageTable();
  11. return VIEw();
  12. }
  13. [AcceptVerbs(HttpVerbs.Get)]
  14. public ActionResult GetDepartment(int? company)
  15. {
  16. SelectListItem[] items=null;
  17. if(company!=null)
  18. items = GetDepartmentList((int)company);
  19. else
  20. {
  21. SelectListItem i=GetBlankDepartment();
  22. items=new SelectListItem[]{i};
  23. }
  24. return JSon(items);
  25. }
  26. private SelectListItem[] GetCompanyList()
  27. {
  28. ServiceDataContext db = new ServiceDataContext();
  29. var items = from t in db.CompanIEs
  30. select new
  31. {
  32. ID = t.ID,
  33. Name = t.Name,
  34. Brief = t.BrIEf
  35. };
  36. List s = new List();
  37. SelectListItem b = new SelectListItem();
  38. b.Text = "--please select company--";
  39. b.Value = "";
  40. s.Add(b);
  41. foreach (var i in items)
  42. {
  43. SelectListItem item = new SelectListItem();
  44. item.Text = i.Name;
  45. item.Value = i.ID.ToString();
  46. s.Add(item);
  47. }
  48. return s.ToArray();
  49. }
  50. private SelectListItem[] GetDepartmentList(int nCompanyId)
  51. {
  52. ServiceDataContext db = new ServiceDataContext();
  53. var items = from t in db.Departments
  54. where t.Company==nCompanyId
  55. select new
  56. {
  57. ID = t.ID,
  58. Name = t.Name,
  59. Brief = t.BrIEf
  60. };
  61. List s = new List();
  62. SelectListItem b = new SelectListItem();
  63. b.Text = "--please select department--";
  64. b.Value = "";
  65. s.Add(b);
  66. foreach (var i in items)
  67. {
  68. SelectListItem item = new SelectListItem();
  69. item.Text = i.Name;
  70. item.Value = i.ID.ToString();
  71. s.Add(item);
  72. }
  73. return s.ToArray();
  74. }
  75. private string[] GetHeadList()
  76. {
  77. List head = new List();
  78. string strServer=Url.Content("~/Content/images/Head/");
  79. string strUrlFolder = Server.MapPath(Url.Content(Head_Folder+"Head/"));
  80. string[] fileEntrIEs = Directory.GetFiles(strUrlFolder);
  81. FileInfo fi=null;
  82. foreach (string file in fileEntrIEs)
  83. {
  84. fi = new FileInfo(file);
  85. if (fi.Name.ToLower().IndexOf("head")==0 && !fi.FullName.ToLower().Contains("_offline"))
  86. {
  87. head.Add(strServer+fi.Name);
  88. }
  89. }
  90. return head.ToArray();
  91. }
  92. private string GetImageTable()
  93. {
  94. System.Text.StringBuilder sbResult = new System.Text.StringBuilder();
  95. string[] arrImgs = GetHeadList();
  96. foreach (string strImage in arrImgs)
  97. {
  98. string strFile=strImage.Substring(strImage.LastIndexOf("/")+1);
  99. sbResult.AppendLine(string.Format("",
  100. "img_"+strFile.Remove(strFile.LastIndexOf(".")),
  101. Url.Content(strImage)
  102. ));
  103. }
  104. string strImg=sbResult.ToString();
  105. strImg=strImg.Remove(strImg.Length-5,5);
  106. return strImg;
  107. }
  108. }

注意紫色處,我們加了CSSimgList來供jquery選擇

  1. $("img:.imgList").click(function() {
  2. $("#imgDefault").attr("src", $(this).attr("src"));
  3. $("#divList").slideToggle("fast");
  4. $("#head").val($("#imgDefault").attr("src"));
  5. }).hover(function() {
  6. $(this).removeClass().addClass("alpha");
  7. }, (function() {
  8. $(this).removeClass();
  9. }));

好了,可以試試效果了。

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