JavaScript原生代码实现楼层跳跃


    
    
  1. <!doctype html>
  2. <html lang=“en”>
  3. <head>
  4. <meta charset=“UTF-8”>
  5. <meta name=“viewport”
  6. content= “width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0”>
  7. <meta http-equiv=“X-UA-Compatible” content=“ie=edge”>
  8. <title>demo </title>
  9. <style>
  10. *{
  11. padding: 0;
  12. margin: 0;
  13. }
  14. body , html{
  15. height: 100%;
  16. }
  17. ul, ol{
  18. list-style: none;
  19. }
  20. ul{
  21. height: 100%;
  22. text-align: center;
  23. line-height: 50;
  24. }
  25. ul li {
  26. height: 100%;
  27. }
  28. ol{
  29. position: fixed;
  30. bottom: 100px;
  31. right: 120px;
  32. }
  33. ol li{
  34. width: 50px;
  35. height: 50px;
  36. border: 1px solid #000;
  37. line-height: 50px;
  38. text-align: center;
  39. margin-top: - 1px;
  40. cursor: pointer;
  41. }
  42. </style>
  43. <script>
  44. window.onload = function () {
  45. var ul = document.getElementsByTagName( “ul”)[ 0];
  46. var ol = document.getElementsByTagName( “ol”)[ 0];
  47. var olarr = ol.children;
  48. var ularr = ul.children;
  49. var colorArr = [ “#fada8d”, “#03263a”, “#b3a896”, “#de7d2c”, “#14446a”];
  50. var target = 0;
  51. var timer = null;
  52. var leader = 0;
  53. for ( var i = 0;i<olarr.length;i++){
  54. ularr[i].style.backgroundColor = colorArr[i];
  55. olarr[i].style.backgroundColor = colorArr[i];
  56. // 绑定索引值
  57. olarr[i].index = i;
  58. // 循环绑定事件
  59. olarr[i].onclick = function () {
  60. //获取目标位置
  61. target = ularr[ this.index].offsetTop;
  62. // 清除定时器
  63. clearInterval(timer);
  64. // 设置定时器
  65. timer = setInterval( function () {
  66. var speed = (target-leader)/ 10;
  67. // 判断方向
  68. speed = speed> 0? Math.ceil(speed): Math.floor(speed);
  69. //下一步到的位置
  70. leader = leader+speed;
  71. //移动
  72. window.scrollTo( 0,leader);
  73. //判断是否到达,到达清除定时器
  74. if(leader == target){
  75. console.log( 1);
  76. clearInterval(timer);
  77. }
  78. }, 30)
  79. }
  80. }
  81. window.onscroll = function () {
  82. leader = scroll().top
  83. }
  84. //缓动动画封装
  85. function animate(ele, target) {
  86. clearInterval(ele.timer);
  87. ele.timer = setInterval( function () {
  88. var step = (target - ele.offsetTop) / 10;
  89. step = step > 0 ? Math.ceil(step) : Math.floor(step);
  90. ele.style.top = ele.offsetTop + step + “px”;
  91. console.log( 1);
  92. if ( Math.abs(target - ele.offsetTop) < Math.abs(step)) {
  93. ele.style.top = target + “px”;
  94. clearInterval(ele.timer);
  95. }
  96. }, 25);
  97. }
  98. // scroll的封装
  99. function scroll() {
  100. return {
  101. “top”: window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop,
  102. “left”: window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft
  103. }
  104. }
  105. }
  106. </script>
  107. </head>
  108. <body>
  109. <ul>
  110. <li>Test </li>
  111. <li>Test </li>
  112. <li>Test </li>
  113. <li>Test </li>
  114. <li>Test </li>
  115. </ul>
  116. <ol>
  117. <li>test </li>
  118. <li>test </li>
  119. <li>test </li>
  120. <li>test </li>
  121. <li>test </li>
  122. </ol>
  123. </body>
  124. </html>


  
  
  1. <!doctype html>
  2. <html lang=“en”>
  3. <head>
  4. <meta charset=“UTF-8”>
  5. <meta name=“viewport”
  6. content= “width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0”>
  7. <meta http-equiv=“X-UA-Compatible” content=“ie=edge”>
  8. <title>demo </title>
  9. <style>
  10. *{
  11. padding: 0;
  12. margin: 0;
  13. }
  14. body , html{
  15. height: 100%;
  16. }
  17. ul, ol{
  18. list-style: none;
  19. }
  20. ul{
  21. height: 100%;
  22. text-align: center;
  23. line-height: 50;
  24. }
  25. ul li {
  26. height: 100%;
  27. }
  28. ol{
  29. position: fixed;
  30. bottom: 100px;
  31. right: 120px;
  32. }
  33. ol li{
  34. width: 50px;
  35. height: 50px;
  36. border: 1px solid #000;
  37. line-height: 50px;
  38. text-align: center;
  39. margin-top: - 1px;
  40. cursor: pointer;
  41. }
  42. </style>
  43. <script>
  44. window.onload = function () {
  45. var ul = document.getElementsByTagName( “ul”)[ 0];
  46. var ol = document.getElementsByTagName( “ol”)[ 0];
  47. var olarr = ol.children;
  48. var ularr = ul.children;
  49. var colorArr = [ “#fada8d”, “#03263a”, “#b3a896”, “#de7d2c”, “#14446a”];
  50. var target = 0;
  51. var timer = null;
  52. var leader = 0;
  53. for ( var i = 0;i<olarr.length;i++){
  54. ularr[i].style.backgroundColor = colorArr[i];
  55. olarr[i].style.backgroundColor = colorArr[i];
  56. // 绑定索引值
  57. olarr[i].index = i;
  58. // 循环绑定事件
  59. olarr[i].onclick = function () {
  60. //获取目标位置
  61. target = ularr[ this.index].offsetTop;
  62. // 清除定时器
  63. clearInterval(timer);
  64. // 设置定时器
  65. timer = setInterval( function () {
  66. var speed = (target-leader)/ 10;
  67. // 判断方向
  68. speed = speed> 0? Math.ceil(speed): Math.floor(speed);
  69. //下一步到的位置
  70. leader = leader+speed;
  71. //移动
  72. window.scrollTo( 0,leader);
  73. //判断是否到达,到达清除定时器
  74. if(leader == target){
  75. console.log( 1);
  76. clearInterval(timer);
  77. }
  78. }, 30)
  79. }
  80. }
  81. window.onscroll = function () {
  82. leader = scroll().top
  83. }
  84. //缓动动画封装
  85. function animate(ele, target) {
  86. clearInterval(ele.timer);
  87. ele.timer = setInterval( function () {
  88. var step = (target - ele.offsetTop) / 10;
  89. step = step > 0 ? Math.ceil(step) : Math.floor(step);
  90. ele.style.top = ele.offsetTop + step + “px”;
  91. console.log( 1);
  92. if ( Math.abs(target - ele.offsetTop) < Math.abs(step)) {
  93. ele.style.top = target + “px”;
  94. clearInterval(ele.timer);
  95. }
  96. }, 25);
  97. }
  98. // scroll的封装
  99. function scroll() {
  100. return {
  101. “top”: window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop,
  102. “left”: window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft
  103. }
  104. }
  105. }
  106. </script>
  107. </head>
  108. <body>
  109. <ul>
  110. <li>Test </li>
  111. <li>Test </li>
  112. <li>Test </li>
  113. <li>Test </li>
  114. <li>Test </li>
  115. </ul>
  116. <ol>
  117. <li>test </li>
  118. <li>test </li>
  119. <li>test </li>
  120. <li>test </li>
  121. <li>test </li>
  122. </ol>
  123. </body>
  124. </html>

猜你喜欢

转载自blog.csdn.net/chenjuan1993/article/details/81516537