资讯专栏INFORMATION COLUMN

模拟select弹框

tomorrowwu / 3590人阅读

摘要:模拟弹框功能点点击显示下拉框,再次点击下拉框下拉框消失点击下拉框将值赋值给点击下拉框之外区域,下拉框消失模拟弹框模拟下拉请选择请选择男女

模拟select弹框

功能点:

点击text显示下拉框,再次点击下拉框下拉框消失

点击下拉框将值赋值给text

点击下拉框之外区域,下拉框消失

html

</>复制代码

  1. 模拟<span class="hljs-keyword">select</span>弹框
  2. --请选择--
    • --请选择--

css:

</>复制代码

  1. /*
  2. * @Author: baby
  3. * @Date: 2017-07-06 12:37:56
  4. * @Last Modified by: baby
  5. * @Last Modified time: 2017-07-06 14:38:51
  6. */
  7. .model-select-box {
  8. position: relative;
  9. width: 200px;
  10. height: 30px;
  11. line-height: 30px;
  12. background-color: #fff;
  13. border: 1px solid #e4e4e4;
  14. border-radius: 3px;
  15. text-indent: 5px;
  16. }
  17. .model-select-box .model-select-text {
  18. position: relative;
  19. width: 100%;
  20. height: 28px;
  21. // height: 30px;
  22. // line-height: 30px;
  23. color: #666;
  24. text-indent: 10px;
  25. font-size: 14px;
  26. cursor: pointer;
  27. user-select: none;
  28. }
  29. .model-select-box .model-select-text:after {
  30. position: absolute;
  31. top: 10px;
  32. right: 10px;
  33. content: "";
  34. width: 0;
  35. height: 0;
  36. border-width: 10px 8px 0;
  37. border-style: solid;
  38. border-color: #666 transparent transparent;
  39. }
  40. .model-select-box .model-select-option {
  41. position: absolute;
  42. top: 30px;
  43. left: -1px;
  44. display: none;
  45. list-style: none;
  46. border: 1px solid #e4e4e4;
  47. border-top: none;
  48. padding: 0;
  49. margin: 0;
  50. width: 100%;
  51. z-index: 99;
  52. background-color: #fff;
  53. }
  54. .model-select-box .model-select-option li {
  55. height: 28px;
  56. line-height: 28px;
  57. color: #333;
  58. font-size: 14px;
  59. margin: 0;
  60. padding: 0;
  61. // text-indent: 10px;
  62. cursor: pointer;
  63. }
  64. .model-select-box .model-select-option li:hover {
  65. background-color: #f3f3f3;
  66. }
  67. .model-select-box .model-select-option li.seleced {
  68. background-color: #f3f3f3;
  69. }

js:

</>复制代码

  1. /*
  2. * @Author: baby
  3. * @Date: 2017-07-06 12:38:11
  4. * @Last Modified by: baby
  5. * @Last Modified time: 2017-07-06 14:24:53
  6. * 模拟select弹框
  7. * 功能点:
  8. * 1. 点击text显示下拉框,再次点击下拉框下拉框消失
  9. * 2. 点击下拉框将值赋值给text
  10. * 3. 点击下拉框之外区域,下拉框消失
  11. *
  12. */
  13. "use strict";
  14. $(function() {
  15. selectModel();
  16. });
  17. /**
  18. * 模拟网页中所有下拉列表select
  19. * @return {[type]} [description]
  20. */
  21. function selectModel() {
  22. var $box = $("div.model-select-box");
  23. var $option = $("ul.model-select-option", $box);
  24. var $txt = $("div.model-select-text", $box);
  25. var speed = 10;
  26. /**
  27. * 单击某个下拉列表时,显示当前下拉列表的下拉列表框
  28. * 并隐藏页面中其他下拉列表
  29. */
  30. $txt.on("click", function() {
  31. var $self = $(this);
  32. $option.not($self).siblings("ul.model-select-option").slideUp(speed, function() {
  33. init($self);
  34. });
  35. $self.siblings("ul.model-select-option").slideToggle(speed, function() {
  36. init($self);
  37. });
  38. return false;
  39. });
  40. // 点击选择,关闭其他下拉
  41. /**
  42. * 为每个下拉列表框中的选项设置默认选中标识 data-selected
  43. * 点击下拉列表框中的选项时,将选项的 data-option 属性的属性值赋给下拉列表的 data-value 属性,并改变默认选中标识 data-selected
  44. * 为选项添加 mouseover 事件
  45. */
  46. $option.find("li").each(function(index, element) {
  47. var $self = $(this);
  48. if ($self.hasClass("selected")) {
  49. $self.addClass("data-selected");
  50. }
  51. }).mousedown(function() {
  52. $(this).parent().siblings("div.model-select-text").text($(this).text()).attr("data-value", $(this).attr("data-option"));
  53. $option.slideUp(speed, function() {
  54. init($(this));
  55. });
  56. $(this).addClass("selected data-selected").siblings("li").removeClass("selected data-selected");
  57. return false;
  58. }).mouseover(function() {
  59. $(this).addClass("selected").siblings("li").removeClass("selected");
  60. });
  61. // 点击文档隐藏所有下拉
  62. $(document).on("click", function() {
  63. var $self = $(this);
  64. $option.slideUp(speed, function() {
  65. init($self);
  66. })
  67. });
  68. /**
  69. * 初始化默认选择
  70. */
  71. function init(obj) {
  72. obj.find("li.data-selected").addClass("selected").siblings("li").removeClass("selected");
  73. }
  74. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/112222.html

相关文章

  • 模拟select弹框

    摘要:模拟弹框功能点点击显示下拉框,再次点击下拉框下拉框消失点击下拉框将值赋值给点击下拉框之外区域,下拉框消失模拟弹框模拟下拉请选择请选择男女 模拟select弹框 功能点: 点击text显示下拉框,再次点击下拉框下拉框消失 点击下拉框将值赋值给text 点击下拉框之外区域,下拉框消失 html 模拟select弹框 ...

    ASCH 评论0 收藏0
  • 模拟select弹框

    摘要:模拟弹框功能点点击显示下拉框,再次点击下拉框下拉框消失点击下拉框将值赋值给点击下拉框之外区域,下拉框消失模拟弹框模拟下拉请选择请选择男女 模拟select弹框 功能点: 点击text显示下拉框,再次点击下拉框下拉框消失 点击下拉框将值赋值给text 点击下拉框之外区域,下拉框消失 html 模拟select弹框 ...

    xialong 评论0 收藏0
  • 在Vue项目中使用ElementUI(一)

    摘要:这是一段内容标题名称确定点击确定后的回调函数确认消息弹框提示用户确认其已经触发的动作,并询问是否进行此操作时会用到此对话框。。 初始化一个Vue项目 F:Test>vue init webpack Test1 ? Project name test1 ? Project description A Vue.js project ? Author Selience ? Vue buil...

    linkin 评论0 收藏0
  • 在Vue项目中使用ElementUI(一)

    摘要:这是一段内容标题名称确定点击确定后的回调函数确认消息弹框提示用户确认其已经触发的动作,并询问是否进行此操作时会用到此对话框。。 初始化一个Vue项目 F:Test>vue init webpack Test1 ? Project name test1 ? Project description A Vue.js project ? Author Selience ? Vue buil...

    shadowbook 评论0 收藏0
  • 在Vue项目中使用ElementUI(一)

    摘要:这是一段内容标题名称确定点击确定后的回调函数确认消息弹框提示用户确认其已经触发的动作,并询问是否进行此操作时会用到此对话框。。 初始化一个Vue项目 F:Test>vue init webpack Test1 ? Project name test1 ? Project description A Vue.js project ? Author Selience ? Vue buil...

    NicolasHe 评论0 收藏0

发表评论

0条评论

tomorrowwu

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<