5秒倒计时

  1. <script>
  2. var num =5;
  3. var btn = document.getElementById("btn");
  4. var timer
  5. /* 1、让按钮进入倒计时的状态(不能点击的状态)*/
  6. btn.onclick = function(){
  7. this.disabled = true;
  8. this.innerHTML = num;
  9. /* 2、每过1s num-- */
  10. timer = setInterval(function(){
  11. num--
  12. if(num>=0){
  13. btn.innerHTML = num;
  14. }else{
  15. btn.disabled = false;
  16. btn.innerHTML = "发送验证码";
  17. num =5;
  18. clearInterval(timer);
  19. }
  20. },1000)
  21. }
  22. /* 使用setTimeout去实现 */
  23. </script>

渐变滚动条

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. *{margin:0;padding:0}
  10. #nav{
  11. height: 50px;
  12. background-color: #ff2d51;
  13. position: fixed;
  14. width:100%;
  15. opacity: 0;
  16. }
  17. body{
  18. height: 2000px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="nav">
  24. </div>
  25. <script>
  26. /* 当滚动条滚动到距离顶部200px的时候 导航条完全显示 */
  27. var nav = document.getElementById("nav")
  28. /* 1、对窗口执行监听事件 */
  29. window.onscroll = function(){
  30. /* 2、获取滚动条的高度 */
  31. var scrollTop = document.documentElement.scrollTop;
  32. /* 3、得到透明度 */
  33. var opacity = scrollTop/200;
  34. if(opacity>1){
  35. opacity=1
  36. }
  37. nav.style.opacity = opacity;
  38. console.log(opacity)
  39. }
  40. </script>
  41. </body>
  42. </html>

tab切换

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. .current{
  10. color:orange
  11. }
  12. .content{
  13. width:100px;
  14. height: 100px;
  15. border:1px solid #333;
  16. position: relative;
  17. }
  18. .content>div{
  19. width:100%;
  20. height: 100%;
  21. position: absolute;
  22. }
  23. .content>div:first-child{
  24. background-color: red;
  25. }
  26. .content>div:last-child{
  27. background-color: yellow;
  28. display: none;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <ul>
  34. <li class="current">登录</li>
  35. <li>注册</li>
  36. </ul>
  37. <div class="content">
  38. <div>登录的内容</div>
  39. <div>注册的内容</div>
  40. </div>
  41. <script>
  42. var lis = document.querySelectorAll('ul li');
  43. var divs = document.querySelectorAll('.content div')
  44. for(var i = 0;i<lis.length;i++){
  45. lis[i].index = i;
  46. lis[i].onclick = function() {
  47. for(var i = 0;i<lis.length;i++) {
  48. lis[i].className = '';
  49. }
  50. this.className = "current";
  51. for(var i =0;i<divs.length;i++) {
  52. divs[i].style.display = 'none';
  53. }
  54. divs[this.index].style.display = 'block'
  55. }
  56. }
  57. </script>
  58. </body>
  59. </html>

tab切换jquery方法

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="libs/jquery-3.6.0.js"></script>
  9. <style>
  10. .current{
  11. color:orange
  12. }
  13. .content{
  14. width:100px;
  15. height: 100px;
  16. border:1px solid #333;
  17. position: relative;
  18. }
  19. .content>div{
  20. width:100%;
  21. height: 100%;
  22. position: absolute;
  23. }
  24. .content>div:first-child{
  25. background-color: red;
  26. }
  27. .content>div:last-child{
  28. background-color: yellow;
  29. display: none;
  30. }
  31. </style>
  32. <script>
  33. $(function(){
  34. $(".dl li").click(function (){
  35. $(this).css("color","red").siblings().css("color","");
  36. var index = $(this).index();
  37. console.log(index);
  38. $(".content div").eq(index).show();
  39. $(".content div").eq(index).siblings().hide();
  40. })
  41. })
  42. </script>
  43. </head>
  44. <body>
  45. <ul class="dl">
  46. <li >登录</li>
  47. <li>注册</li>
  48. </ul>
  49. <div class="content">
  50. <div>登录的内容</div>
  51. <div>注册的内容</div>
  52. </div>
  53. </body>
  54. </html>