Tags组件(jsbin)

HTML
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
  5. <meta charset="utf-8">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9. <div class="tabs">
  10. <ul class="tab-head">
  11. <li>1</li>
  12. <li>2</li>
  13. <li>3</li>
  14. </ul>
  15. <ul class="tab-body">
  16. <li>content1</li>
  17. <li>content2</li>
  18. <li>content3</li>
  19. </ul>
  20. </div>
  21. <div class="tabs">
  22. <ul class="tab-head">
  23. <li>1</li>
  24. <li>2</li>
  25. <li>3</li>
  26. </ul>
  27. <ul class="tab-body">
  28. <li>content1</li>
  29. <li>content2</li>
  30. <li>content3</li>
  31. </ul>
  32. </div>
  33. </body>
  34. </html>

CSS

  1. *{margin:0}
  2. ul, li {list-style:none}
  3. .tab-head {display: flex;}
  4. .tab-head li{border: 1px solid transparent; padding: 2px 8px; cursor: pointer}
  5. .tab-head li:hover {border-color: #c03}
  6. .tab-head li.cur {
  7. border-color: #c03;
  8. }
  9. .tab-body li {display: none;}
  10. .tab-body li.cur {display: block;}

JS

  1. function Tags(selector) {
  2. this.parentEle = $(selector)
  3. this.init()
  4. this.bindEvents()
  5. }
  6. Tags.prototype.init = function () {
  7. this.parentEle.each( (i,v) => {
  8. $(v).find('ul').eq(0).find('li').eq(0).addClass('cur')
  9. $(v).find('ul').eq(1).find('li').eq(0).addClass('cur')
  10. })
  11. }
  12. Tags.prototype.bindEvents = function () {
  13. this.parentEle.on('click', '.tab-head li', function(e){
  14. $(this).addClass('cur').siblings().removeClass('cur')
  15. $(this).parent().next().find('li').eq($(this).index()).addClass('cur').siblings().removeClass('cur')
  16. })
  17. }
  18. // // ES6 class写法
  19. // class Tags {
  20. // constructor(selector) {
  21. // this.parentEle = $(selector)
  22. // this.init()
  23. // this.bindEvents()
  24. // }
  25. // init() {
  26. // this.parentEle.each( (i,v) => {
  27. // $(v).find('ul').eq(0).find('li').eq(0).addClass('cur')
  28. // $(v).find('ul').eq(1).find('li').eq(0).addClass('cur')
  29. // })
  30. // }
  31. // bindEvents() {
  32. // this.parentEle.on('click', '.tab-head li', function(e){
  33. // $(this).addClass('cur').siblings().removeClass('cur')
  34. // $(this).parent().next().find('li').eq($(this).index()).addClass('cur').siblings().removeClass('cur')
  35. // })
  36. // }
  37. // }
  38. var tabs = new Tags('.tabs')

Dialog组件(jsbin)

HTML
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
  5. <meta charset="utf-8">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9. <button class="a1">111</button>
  10. <button class="a2">222</button>
  11. </body>
  12. </html>

CSS

  1. * {margin:0;padding:0;}
  2. .zc-dialog-mask {
  3. width: 100%;
  4. height: 100%;
  5. background: rgba(0,0,0,0.3);
  6. position: fixed;
  7. left: 0;
  8. top: 0;
  9. display: flex;
  10. align-items: center;
  11. justify-content: center;
  12. }
  13. .zc-dialog-wrap {
  14. background: #fff;
  15. width: 300px;
  16. padding: 15px;
  17. position: relative;
  18. box-shadow: 1px 1px 1px 1px rgab(0,0,0,.3);
  19. }
  20. .zc-dialog-head {
  21. padding-bottom: 10px;
  22. border-bottom: 1px solid #333;
  23. }
  24. .zc-dialog-body {
  25. padding: 15px 0;
  26. min-height: 100px;
  27. display: flex;
  28. align-items: center;
  29. justify-content: center;
  30. word-break: break-all;
  31. }
  32. .zc-dialog-wrap>span {
  33. position: absolute;
  34. top: 15px;
  35. right: 15px;
  36. cursor: pointer;
  37. }
  38. .zc-dialog-button-alert {
  39. text-align: center;
  40. }
  41. .zc-dialog-button-confirm {
  42. text-align: right;
  43. }
  44. .zc-dialog-button>button {
  45. padding: 5px 15px;
  46. border-radius: 4px;
  47. color: #ffffff;
  48. outline: none;
  49. cursor: pointer;
  50. }
  51. .zc-dialog-button>.zc-dialog-confirm {
  52. background-color: #6cb5f4;
  53. border:1px solid #54a9f2;
  54. }
  55. .zc-dialog-button>.zc-dialog-confirm:hover {
  56. background-color: #3d9df0;
  57. border-color: #1b8cee;
  58. }
  59. .zc-dialog-button>.zc-dialog-cancel {
  60. background-color: #d9534f;
  61. border: 1px solid #d43f3a;
  62. }
  63. .zc-dialog-button>.zc-dialog-cancel:hover {
  64. background-color: #c9302c;
  65. border-color: #ac2925;
  66. }

JS

  1. function Dialog() {
  2. // this.option = option
  3. this.bindEvent()
  4. }
  5. Dialog.prototype.template = function (option) {
  6. let temp = `<div class="zc-dialog-mask">
  7. <div class="zc-dialog-wrap">
  8. <div class="zc-dialog-head">
  9. ${option.title || '提示'}
  10. </div>
  11. <div class="zc-dialog-body">
  12. ${option.content || option || '内容'}
  13. </div>
  14. <div class="zc-dialog-button"></div>
  15. <span>X</span>
  16. </div>
  17. </div>`
  18. this.$temp = $(temp)
  19. }
  20. Dialog.prototype.hide = function () {
  21. this.$temp.detach()
  22. }
  23. Dialog.prototype.bindEvent = function () {
  24. $('body').on('click', '.zc-dialog-wrap>span, .zc-dialog-button>.zc-dialog-confirm, .zc-dialog-button>.zc-dialog-cancel', function(){
  25. dialog.hide()
  26. if (this.classList[0] === 'zc-dialog-confirm')
  27. return true
  28. else if (this.classList[0] === 'zc-dialog-cancel')
  29. return false
  30. })
  31. }
  32. Dialog.prototype.setSize = function (option) {
  33. if(option.width && option.width > 330){
  34. this.$temp.find('.zc-dialog-wrap').css({width: `${option.width}px`})
  35. }
  36. if(option.height && option.height > 150){
  37. this.$temp.find('.zc-dialog-body').css({height: `${option.height}px`})
  38. }
  39. }
  40. Dialog.prototype.alert = function (option) {
  41. this.template(option)
  42. this.$temp.find('.zc-dialog-button').addClass('zc-dialog-button-alert').removeClass('zc-dialog-button-confirm').html(`<button class="zc-dialog-confirm" type="button">确认</button>`)
  43. this.setSize(option)
  44. $('body').append(this.$temp)
  45. }
  46. Dialog.prototype.confirm = function (option) {
  47. this.template(option)
  48. this.$temp.find('.zc-dialog-button').addClass('zc-dialog-button-confirm').removeClass('zc-dialog-button-alert').html(`<button class="zc-dialog-confirm" type="button">确认</button> <button class="zc-dialog-cancel" type="button">取消</button>`)
  49. this.setSize(option)
  50. $('body').append(this.$temp)
  51. }
  52. var dialog = new Dialog();
  53. $('.a1').click(function(){
  54. dialog.alert({
  55. title: '非模态窗口',
  56. content: '<p>这是一个非模态窗口,并且不带按钮</p>'
  57. })
  58. })
  59. $('.a2').click(function(){
  60. dialog.confirm({
  61. title: '非模态窗口',
  62. content: '<p>确定要删除吗?</p>'
  63. })
  64. })

持续更新~~~