资讯专栏INFORMATION COLUMN

原生js造轮子之模仿JQ的slideDown()与slideUp()

王笑朝 / 627人阅读

摘要:代码如下原生调用该文件中加入这一行代码参数参数时间调用该文件加入这一行代码中引入绑定到实例原型上组件中调用鄙人创建了一个群,供大家学习交流,希望和大家合作愉快,互相帮助,交流学习,以下为群二维码

代码如下:

</>复制代码

  1. const slider = (function() {
  2. var Slider = {};
  3. // the constructed function,timeManager,as such that"s a manager about managing the setInterval
  4. function TimerManager() {
  5. this.timers = [];
  6. this.args = [];
  7. this.isTimerRun = false;
  8. }
  9. // if the element can"t has the property of TimerManage what represented the constructor function,repeated creating a constructed function
  10. TimerManager.makeTimerManage = function(element) {
  11. if (
  12. !element.TimerManage ||
  13. element.TimerManage.constructor !== TimerManager
  14. ) {
  15. element.TimerManage = new TimerManager();
  16. }
  17. };
  18. // That"s order to create the method what add the timer
  19. TimerManager.prototype.add = function(timer, args) {
  20. this.timers.push(timer);
  21. this.args.push(args);
  22. this.timerRun();
  23. };
  24. // called the method is order to run the timer by ordering
  25. TimerManager.prototype.timerRun = function() {
  26. if (!this.isTimerRun) {
  27. var timer = this.timers.shift(),
  28. args = this.args.shift();
  29. if (timer && args) {
  30. this.isTimerRun = true;
  31. timer(args[0], args[1]);
  32. }
  33. }
  34. };
  35. // let it run the next timer
  36. TimerManager.prototype.next = function() {
  37. this.isTimerRun = false;
  38. this.timerRun();
  39. };
  40. function slideUp(element, time) {
  41. if (element.offsetHeight > 0) {
  42. var totalHeight = element.offsetHeight;
  43. var currentHeight = totalHeight;
  44. var reduceValue = totalHeight / (time / 10);
  45. element.style.transition = "height " + time + " ms";
  46. element.style.overflow = "hidden";
  47. var timer = setInterval(function() {
  48. currentHeight -= reduceValue;
  49. element.style.height = currentHeight + "px";
  50. if (currentHeight <= 0) {
  51. clearInterval(timer);
  52. element.style.display = "none";
  53. element.style.height = totalHeight + "px";
  54. if (
  55. element.TimerManage &&
  56. element.TimerManage.constructor === TimerManager
  57. ) {
  58. element.TimerManage.next();
  59. }
  60. }
  61. }, 10);
  62. } else {
  63. if (
  64. element.TimerManage &&
  65. element.TimerManage.constructor === TimerManager
  66. ) {
  67. element.TimerManage.next();
  68. }
  69. }
  70. }
  71. function slideDown(element, time) {
  72. if (element.offsetHeight <= 0) {
  73. element.style.display = "block";
  74. element.style.transition = "height" + time + " ms";
  75. element.style.overflow = "hidden";
  76. var totalHeight = element.offsetHeight;
  77. var currentHeight = 0;
  78. element.style.height = "0px";
  79. var addValue = totalHeight / (time / 10);
  80. var timer = setInterval(function() {
  81. currentHeight += addValue;
  82. element.style.height = currentHeight + "px";
  83. if (currentHeight >= totalHeight) {
  84. clearInterval(timer);
  85. element.style.height = totalHeight + "px";
  86. if (
  87. element.TimerManage &&
  88. element.TimerManage.constructor === TimerManager
  89. ) {
  90. element.TimerManage.next();
  91. }
  92. }
  93. }, 10);
  94. } else {
  95. if (
  96. element.TimerManage &&
  97. element.TimerManage.constructor === TimerManager
  98. ) {
  99. element.TimerManage.next();
  100. }
  101. }
  102. }
  103. // the interface about slideUp method
  104. Slider.slideUp = function(element) {
  105. TimerManager.makeTimerManage(element);
  106. element.TimerManage.add(slideUp, arguments);
  107. return this;
  108. };
  109. // the interface about slideDown method
  110. Slider.slideDown = function(element) {
  111. TimerManager.makeTimerManage(element);
  112. element.TimerManage.add(slideDown, arguments);
  113. return this;
  114. };
  115. return Slider;
  116. })();

原生调用:

</>复制代码

  1. //该js文件中加入这一行代码
  2. window.slider = slider;
  3. //参数1,dom,参数2:时间
  4. slider.slideDown(document.queryselector(),time);
  5. slider.slideUp(document.queryselector(),time);

vue.js调用:

</>复制代码

  1. //该js文件加入这一行代码
  2. export default slider;
  3. main.js中引入:
  4. import slider from "slider.js";
  5. //绑定到Vue实例原型上
  6. Vue.prototype.slider = slider;
  7. //组件中调用
  8. this.slider(this.$refs,time);

鄙人创建了一个QQ群,供大家学习交流,希望和大家合作愉快,互相帮助,交流学习,以下为群二维码:

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

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

相关文章

  • 原生js轮子模仿JQslideDown()slideUp()

    摘要:代码如下原生调用该文件中加入这一行代码参数参数时间调用该文件加入这一行代码中引入绑定到实例原型上组件中调用鄙人创建了一个群,供大家学习交流,希望和大家合作愉快,互相帮助,交流学习,以下为群二维码 代码如下: const slider = (function() { var Slider = {}; // the constructed function,timeManager,...

    RancherLabs 评论0 收藏0
  • JavaWEB开发04——JQuery

    摘要:设计的宗旨是,,即倡导写更少的代码,做更多的事情。它封装常用的功能代码,提供一种简便的设计模式,优化文档操作事件处理动画设计和交互。 今日任务 使用JQuery完成页面定时弹出广告 定时器: ​ setInterval clearInterval ​ setTimeout clearTimeout 显示: img.style.display = bloc...

    chunquedong 评论0 收藏0
  • JavaWEB开发04——JQuery

    摘要:设计的宗旨是,,即倡导写更少的代码,做更多的事情。它封装常用的功能代码,提供一种简便的设计模式,优化文档操作事件处理动画设计和交互。 今日任务 使用JQuery完成页面定时弹出广告 定时器: ​ setInterval clearInterval ​ setTimeout clearTimeout 显示: img.style.display = bloc...

    nicercode 评论0 收藏0
  • JavaWEB开发04——JQuery

    摘要:设计的宗旨是,,即倡导写更少的代码,做更多的事情。它封装常用的功能代码,提供一种简便的设计模式,优化文档操作事件处理动画设计和交互。 今日任务 使用JQuery完成页面定时弹出广告 定时器: ​ setInterval clearInterval ​ setTimeout clearTimeout 显示: img.style.display = bloc...

    pakolagij 评论0 收藏0
  • jQuery 入门详解(二)

    摘要:相对论极大地改变了人类对宇宙和自然的常识性观念,提出了同时的相对性四维时空弯曲时空等全新的概念。狭义相对性原理是相对论的两个基本假定,在目前实验的观测下,物体的运动与相对论是吻合很好的,所以目前普遍认为相对论是正确的理论。 7. jQuery 里的事件机制 javascript和HTML之间的交互是通过用户和浏览器操作页面时引发的事件来处理的。jQuery不仅提供了更加优雅的事件处理...

    DevYK 评论0 收藏0

发表评论

0条评论

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