jquery 如何在打开/关闭树视图菜单项时使用JavaScript创建效果

kcugc4gi  于 2023-08-04  发布在  jQuery
关注(0)|答案(1)|浏览(134)

我想使用JavaScript将效果添加到此树视图。例如,在打开菜单项时添加延迟效果,这意味着它们将在单击它们一秒钟后打开,并在单击树视图中的(ul / li)项时显示加载图标。另外,我想在关闭treeview项目时添加相同的效果。

  1. $.fn.extend({
  2. treed: function(o) {
  3. var openedClass = 'fa-minus-circle';
  4. var closedClass = 'fa-plus-circle';
  5. if (typeof o != 'undefined') {
  6. if (typeof o.openedClass != 'undefined') {
  7. openedClass = o.openedClass;
  8. }
  9. if (typeof o.closedClass != 'undefined') {
  10. closedClass = o.closedClass;
  11. }
  12. };
  13. //initialize each of the top levels
  14. var tree = $(this);
  15. tree.addClass("tree");
  16. tree.find('li').has("ul").each(function() {
  17. var branch = $(this); //li with children ul
  18. branch.prepend("<i class='indicator fas " + closedClass + "'></i>");
  19. branch.addClass('branch');
  20. branch.on('click', function(e) {
  21. if (this == e.target) {
  22. var icon = $(this).children('i:first');
  23. icon.toggleClass(openedClass + " " + closedClass);
  24. $(this).children().children().toggle();
  25. }
  26. })
  27. branch.children().children().toggle();
  28. });
  29. //fire event from the dynamically added icon
  30. tree.find('.branch .indicator').each(function() {
  31. $(this).on('click', function() {
  32. $(this).closest('li').click();
  33. });
  34. });
  35. //fire event to open branch if the li contains an anchor instead of text
  36. tree.find('.branch>a').each(function() {
  37. $(this).on('click', function(e) {
  38. $(this).closest('li').click();
  39. e.preventDefault();
  40. });
  41. });
  42. //fire event to open branch if the li contains a button instead of text
  43. tree.find('.branch>button').each(function() {
  44. $(this).on('click', function(e) {
  45. $(this).closest('li').click();
  46. e.preventDefault();
  47. });
  48. });
  49. }
  50. });
  51. //Initialization of treeviews
  52. $('#tree1').treed();
  53. $('#tree2').treed({
  54. openedClass: 'fa-folder-open',
  55. closedClass: 'fa-folder'
  56. });
  1. .tree,
  2. .tree ul {
  3. margin: 0;
  4. padding: 0;
  5. list-style: none;
  6. margin-left: 10px;
  7. }
  8. .tree ul {
  9. margin-left: 1em;
  10. position: relative
  11. }
  12. .tree ul ul {
  13. margin-left: .5em
  14. }
  15. .tree ul:before {
  16. content: "";
  17. display: block;
  18. width: 0;
  19. position: absolute;
  20. top: 0;
  21. bottom: 0;
  22. left: 0;
  23. border-left: 1px solid
  24. }
  25. .tree li {
  26. margin: 0;
  27. padding: 0 1em;
  28. line-height: 2em;
  29. color: #369;
  30. font-weight: 700;
  31. position: relative;
  32. }
  33. .tree ul li:before {
  34. content: "";
  35. display: block;
  36. width: 10px;
  37. height: 0;
  38. border-top: 1px solid;
  39. margin-top: -1px;
  40. position: absolute;
  41. top: 1em;
  42. left: 0
  43. }
  44. .tree ul li:last-child:before {
  45. background: #fff;
  46. height: auto;
  47. top: 1em;
  48. bottom: 0
  49. }
  50. .indicator {
  51. margin-right: 5px;
  52. }
  53. .tree li a {
  54. text-decoration: none;
  55. color: #369;
  56. }
  57. .tree li button,
  58. .tree li button:active,
  59. .tree li button:focus {
  60. text-decoration: none;
  61. color: #369;
  62. border: none;
  63. background: transparent;
  64. margin: 0px 0px 0px 0px;
  65. padding: 0px 0px 0px 0px;
  66. outline: 0;
  67. }
  1. <div class="row">
  2. <div class="col-md-4">
  3. <ul id="tree2">
  4. <li>
  5. <a href="#">TECH</a>
  6. <ul>
  7. <li>Company Maintenance</li>
  8. <li>
  9. Employees
  10. <ul>
  11. <li>
  12. Reports
  13. <ul>
  14. <li>Report1</li>
  15. <li>Report2</li>
  16. <li>Report3</li>
  17. </ul>
  18. </li>
  19. <li>Employee Maint.</li>
  20. </ul>
  21. </li>
  22. <li>Human Resources</li>
  23. </ul>
  24. </li>
  25. <li>
  26. XRP
  27. <ul>
  28. <li>Company Maintenance</li>
  29. <li>
  30. Employees
  31. <ul>
  32. <li>
  33. Reports
  34. <ul>
  35. <li>Report1</li>
  36. <li>Report2</li>
  37. <li>Report3</li>
  38. </ul>
  39. </li>
  40. <li>Employee Maint.</li>
  41. </ul>
  42. </li>
  43. <li>Human Resources</li>
  44. </ul>
  45. </li>
  46. </ul>
  47. </div>
  48. </div>
  49. <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet">
  50. <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
  51. <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
  52. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js"></script>
7y4bm7vi

7y4bm7vi1#

在调用.toggle()的地方,您可以在那里传递若干毫秒作为过渡效果长度。.toggle(300)简单!
但是,使用.slideToggle(300)会看起来更好。
如果您想添加一个每次单击都有延迟的假加载器,只需添加加载器元素,等待,然后删除它并执行slideToggle

  1. $(this).append("<div class='loading'><span class='fa fa-spinner'></span> Loading...</div>");
  2. setTimeout(()=>{
  3. $(this).find('.loading').remove();
  4. $(this).children().children().slideToggle(300);
  5. }, 300)

字符串
以下是添加了该功能的演示:

  1. $.fn.extend({
  2. treed: function (o) {
  3. var openedClass = 'fa-minus-circle';
  4. var closedClass = 'fa-plus-circle';
  5. if (typeof o != 'undefined'){
  6. if (typeof o.openedClass != 'undefined'){
  7. openedClass = o.openedClass;
  8. }
  9. if (typeof o.closedClass != 'undefined'){
  10. closedClass = o.closedClass;
  11. }
  12. };
  13. //initialize each of the top levels
  14. var tree = $(this);
  15. tree.addClass("tree");
  16. tree.find('li').has("ul").each(function () {
  17. var branch = $(this); //li with children ul
  18. branch.prepend("<i class='indicator fas " + closedClass + "'></i>");
  19. branch.addClass('branch');
  20. branch.on('click', function (e) {
  21. if (this == e.target) {
  22. var icon = $(this).children('i:first');
  23. icon.toggleClass(openedClass + " " + closedClass);
  24. $(this).append("<div class='loading'><span class='fa fa-spinner'></span> Loading...</div>");
  25. setTimeout(()=>{
  26. $(this).find('.loading').remove();
  27. $(this).children().children().slideToggle(300);
  28. }, 300)
  29. }
  30. })
  31. branch.children().children().toggle();
  32. });
  33. //fire event from the dynamically added icon
  34. tree.find('.branch .indicator').each(function(){
  35. $(this).on('click', function () {
  36. $(this).closest('li').click();
  37. });
  38. });
  39. //fire event to open branch if the li contains an anchor instead of text
  40. tree.find('.branch>a').each(function () {
  41. $(this).on('click', function (e) {
  42. $(this).closest('li').click();
  43. e.preventDefault();
  44. });
  45. });
  46. //fire event to open branch if the li contains a button instead of text
  47. tree.find('.branch>button').each(function () {
  48. $(this).on('click', function (e) {
  49. $(this).closest('li').click();
  50. e.preventDefault();
  51. });
  52. });
  53. }
  54. });
  55. //Initialization of treeviews
  56. $('#tree1').treed();
  57. $('#tree2').treed({openedClass:'fa-folder-open', closedClass:'fa-folder'});

x

  1. .tree, .tree ul {
  2. margin:0;
  3. padding:0;
  4. list-style:none;
  5. margin-left:10px;
  6. }
  7. .tree ul {
  8. margin-left:1em;
  9. position:relative
  10. }
  11. .tree ul ul {
  12. margin-left:.5em
  13. }
  14. .tree ul:before {
  15. content:"";
  16. display:block;
  17. width:0;
  18. position:absolute;
  19. top:0;
  20. bottom:0;
  21. left:0;
  22. border-left:1px solid
  23. }
  24. .tree li {
  25. margin:0;
  26. padding:0 1em;
  27. line-height:2em;
  28. color:#369;
  29. font-weight:700;
  30. position:relative;
  31. }
  32. .tree ul li:before {
  33. content:"";
  34. display:block;
  35. width:10px;
  36. height:0;
  37. border-top:1px solid;
  38. margin-top:-1px;
  39. position:absolute;
  40. top:1em;
  41. left:0
  42. }
  43. .tree ul li:last-child:before {
  44. background:#fff;
  45. height:auto;
  46. top:1em;
  47. bottom:0
  48. }
  49. .indicator {
  50. margin-right:5px;
  51. }
  52. .tree li a {
  53. text-decoration: none;
  54. color:#369;
  55. }
  56. .tree li button, .tree li button:active, .tree li button:focus {
  57. text-decoration: none;
  58. color:#369;
  59. border:none;
  60. background:transparent;
  61. margin:0px 0px 0px 0px;
  62. padding:0px 0px 0px 0px;
  63. outline: 0;
  64. }
  65. .loading {
  66. font-style: italic;
  67. }
  1. <div class="row">
  2. <div class="col-md-4">
  3. <ul id="tree2">
  4. <li>
  5. <a href="#">TECH</a>
  6. <ul>
  7. <li>Company Maintenance</li>
  8. <li>
  9. Employees
  10. <ul>
  11. <li>
  12. Reports
  13. <ul>
  14. <li>Report1</li>
  15. <li>Report2</li>
  16. <li>Report3</li>
  17. </ul>
  18. </li>
  19. <li>Employee Maint.</li>
  20. </ul>
  21. </li>
  22. <li>Human Resources</li>
  23. </ul>
  24. </li>
  25. <li>
  26. XRP
  27. <ul>
  28. <li>Company Maintenance</li>
  29. <li>
  30. Employees
  31. <ul>
  32. <li>
  33. Reports
  34. <ul>
  35. <li>Report1</li>
  36. <li>Report2</li>
  37. <li>Report3</li>
  38. </ul>
  39. </li>
  40. <li>Employee Maint.</li>
  41. </ul>
  42. </li>
  43. <li>Human Resources</li>
  44. </ul>
  45. </li>
  46. </ul>
  47. </div>
  48. </div>
  49. <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet">
  50. <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
  51. <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
  52. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js"></script>

的字符串

展开查看全部

相关问题