1、break与continue

1-1.break语句

当满足某个条件时,break语句中断(跳出)循环

  1. <script>
  2. /* break满足某个条件中断(跳出)循环 */
  3. for(var i=0;i<=4;i++)
  4. {
  5. if(i==2){
  6. break;
  7. }
  8. console.log(i)
  9. }
  10. </script>

image.png

1-2.continue语句

当满足条件时,跳过这次循环,继续往下

  1. <script>
  2. /* 跳过某一个条件,继续循环 */
  3. for(var i=0;i<=4;i++){
  4. if(i==2){
  5. continue;
  6. }
  7. console.log(i)
  8. }
  9. </script>

image.png

2、input输入框相关事件

2-1.焦点事件

  • 获取焦点 .onfocus
  • 遗失焦点 .onblur
    1. <input type="text" id="test">
    2. <script>
    3. /*
    4. 获取焦点 onfocus
    5. 遗失焦点 onblur
    6. */
    7. var test = document.getElementById("test");
    8. test.onfocus = function(){
    9. this.style.backgroundColor = "red";
    10. }
    11. test.onblur = function(){
    12. this.style.backgroundColor = "#fff";
    13. }
    14. </script>
    image.pngimage.png

    2-2.改变事件

  1. <input type="text" id="input">
  2. <script>
  3. var input = document.getElementById("input");
  4. input.onchange = function(){
  5. console.log("hello ")
  6. }
  7. </script>

image.png

3、mouse鼠标相关事件

  • .onmouseover 设置鼠标悬停
  • .onmouseout 设置鼠标离开后
    1. <p id="p">hello world</p>
    2. <script>
    3. var p = document.getElementById("p");
    4. p.onmouseover = function(){
    5. this.style.backgroundColor = "red";
    6. }
    7. p.onmouseout = function(){
    8. this.style.backgroundColor = "yellow"
    9. }
    10. </script>
    触碰后:
    image.png
    离开后:
    image.png

    4、window窗口事件

    4-1.窗口onload

    js引入在头文件时,需设置.onload使页面html内容加载完毕再触发js ```html

    hello world

  1. ```css
  2. /* onload是页面加载完毕之后触发 */
  3. window.onload = function(){
  4. var p = document.getElementById("p");
  5. console.log(p)
  6. }

image.png

4-2.窗口onresize

当窗口大小发生改变时,onresize事件触发

  1. <script>
  2. /* onresize 窗口大小发生改变的时候触发 */
  3. window.onresize = function(){
  4. console.log("窗口改变了")
  5. }
  6. </script>

image.png

4-3.窗口onscroll

当窗口滚动时,onscroll事件触发

  1. <style>
  2. div{
  3. height: 2000px;
  4. width: 100px;
  5. background-color: red;
  6. }
  7. </style>
  8. </head>
  9. <body>
  10. <div></div>
  11. <script>
  12. window.onscroll = function(){
  13. console.log(1)
  14. }
  15. </script>

image.png
设置当滚动条滚动到距离顶部200px时,导航条显示
获取滚动距离顶部的高度 window.scrollY(document.documentElement.scrollTop)

  1. <style>
  2. *{
  3. margin: 0; padding: 0;
  4. }
  5. #nav{
  6. height: 50px;
  7. background: aqua;
  8. position: fixed;
  9. width: 100%;
  10. opacity: 0;
  11. }
  12. body{
  13. height: 2000px;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="nav">
  19. </div>
  20. <script>
  21. /*
  22. 获取滚动距离顶部的高度 window.scrollY(document.documentElement.scrollTop)
  23. */
  24. var nav = document.getElementById("nav")
  25. window.onscroll = function(){
  26. /* 当滚动条滚动到距离顶部200px的时候,导航条显示 */
  27. var scrollTop = window.scrollY;
  28. if(scrollTop>200){
  29. nav.style.opacity=1;
  30. }
  31. else{
  32. nav.style.opacity=0;
  33. }
  34. console.log(scrollTop)
  35. }
  36. </script>

设置导航条显示过程渐变
1.对窗口执行监听事件
2.获取滚动条距离顶部的高度
3.获得透明度
4.将透明度赋予导航栏元素

  1. <style>
  2. *{
  3. margin: 0; padding: 0;
  4. }
  5. #nav{
  6. height: 50px;
  7. background: aqua;
  8. position: fixed;
  9. width: 100%;
  10. opacity: 0;
  11. }
  12. body{
  13. height: 2000px;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="nav">
  19. </div>
  20. <script>
  21. /*
  22. 获取滚动距离顶部的高度 window.scrollY(或document.documentElement.scrollTop)
  23. */
  24. var nav = document.getElementById("nav")
  25. // 1.对窗口执行监听事件
  26. window.onscroll = function(){
  27. // 2.获取滚动条距离顶部的高度
  28. /* 当滚动条滚动到距离顶部200px的时候,导航条显示 */
  29. var scrollTop = document.documentElement.scrollTop;
  30. // 3.获得透明度
  31. var opacity = scrollTop/200;
  32. // tips:opacity透明度不能大于1
  33. if(opacity>1){
  34. opacity = 1;
  35. }
  36. // 4.将透明度赋予导航栏元素
  37. nav.style.opacity = opacity;
  38. console.log(opacity)
  39. }
  40. </script>

5、rem及视配问题

5-1.rem

rem 相对于根元素(HTML)的字体大小而言的单位

  1. <style>
  2. html {
  3. font-size: 10px;
  4. }
  5. div{
  6. width: 5rem;
  7. height: 5rem;
  8. background: red;
  9. }
  10. </style>

image.png
自适应div块大小固定为窗口的一半

  1. <style>
  2. div{
  3. width: 5rem;
  4. height: 1rem;
  5. background: red;
  6. }
  7. </style>
  8. </head>
  9. <body>
  10. <div></div>
  11. <script>
  12. var html = document.getElementsByTagName("html")[0];
  13. var ww = window.innerWidth;
  14. var rem = ww/10;
  15. html.style.fontSize = rem+"px";
  16. window.onresize = function(){
  17. var ww = window.innerWidth;
  18. html.style.fontSize = ww/10+"px"
  19. }
  20. </script>

image.png
wechat视配

  1. <!--
  2. rpx;
  3. 750rpx; 微信小程序默认窗口750rpx
  4. -->

5-2.em与单位总结

em:相对于父元素字体大小而言的单位

  1. <style>
  2. .parent{
  3. font-size: 10px;
  4. }
  5. .child{
  6. width: 5em;
  7. height: 5em;
  8. background-color: red;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <div class="parent">
  14. <div class="child"></div>
  15. </div>

image.png

  1. <!--
  2. px --绝对单位 像素点
  3. rem --相对单位 相对html字体大小而言
  4. em --相对于父元素的font-size而言的
  5. vm --相对于视窗大小
  6. %
  7. -->

5-3.vw单位

vw:视窗大小单位 最大值为100

  1. <style>
  2. *{margin: 0;padding: 0;}
  3. div{
  4. width: 100vw;
  5. height: 1vw;
  6. background: red;
  7. }
  8. </style>
  9. </head>
  10. <body>
  11. <div>
  12. </div>

image.png

6、键盘相关事件

onkeydown 按下键盘时触发事件
onkeypress 按着键盘时触发事件
onkeyup 按下键盘后松开触发事件

  1. <input type="text" id="app">
  2. <script>
  3. /*
  4. onkeydown --
  5. onkeypress
  6. onkeyup
  7. */
  8. var app = document.getElementById("app")
  9. app.onkeydown =function(event){
  10. console.log(event);
  11. console.log(event.key);
  12. console.log(event.keyCode);
  13. }
  14. </script>

image.png

  1. <input type="text" id="app">
  2. <script>
  3. var app = document.getElementById("app");
  4. app.onkeypress = function(event){
  5. console.log(event)
  6. }
  7. </script>

image.png

  1. <input type="text" id="app">
  2. <script>
  3. var app = document.getElementById("app");
  4. app.onkeyup = function(event){
  5. console.log(event)
  6. }
  7. </script>

image.png
Enter回车事件
每个键盘按键都有独特的keyCode,当keyCode值==13时为Enter,触发事件

  1. <input type="text" id="app">
  2. <script>
  3. var app = document.getElementById("app")
  4. app.onkeydown =function(event){
  5. console.log(event.keyCode);
  6. /* keyCode==13时按的是Enter */
  7. if(event.keyCode==13){
  8. console.log(this.value)
  9. }
  10. }
  11. </script>

image.png

7、内联事件

将调用函数写进html样式内部

  1. <!-- 内联事件 -->
  2. <button onclick="go()">btn</button>
  3. <script>
  4. function go(){
  5. console.log("hello world")
  6. }
  7. </script>

image.png

8、自定义属性

设置data-xx自定义属性名
自定义aid属性:data-aid
获取自定义属性的值用event.target.dataset

  1. <!--
  2. aid自定义属性
  3. data-aid
  4. -->
  5. <div id="test" data-aid="123">div</div>
  6. <script>
  7. /* 自定义属性 */
  8. var test = document.getElementById("test")
  9. test.onclick = function(event){
  10. /* 获取自定义属性的值用event.target.dataset */
  11. console.log(event.target.dataset)
  12. /* JQuery console.log(event.currentTarget.dataset) */
  13. }
  14. </script>

image.png

9、定时器

9-1.定时器方法

window方法:

  • setTimeout间隔一定的时间触发,并且只触发一次
  • setInterval间隔一定的时间触发,无限次数

    1. <script>
    2. /* window的方法 */
    3. /*
    4. setTimeout间隔一定的时间触发,并且只触发一次
    5. */
    6. setTimeout(function(){
    7. console.log("hello")
    8. },10000
    9. )
    10. /* setInterval间隔一定的时间触发,无限次数 */
    11. setInterval(function(){
    12. console.log("world")
    13. }, 2000);
    14. </script>

    image.png

    9-2.清除定时器

  • clearInterval clearTimeout

  • setInterval setTimeout
    1. <button id="clear">清除定时器</button>
    2. <script>
    3. var timer = setInterval(function(){
    4. console.log("world")
    5. },1000)
    6. console.log(timer)
    7. var clear = document.getElementById("clear");
    8. clear.onclick = function(){
    9. clearInterval(timer)
    10. }
    11. /*
    12. clearInterval clearTimeout
    13. setInterval setTimeout
    14. */
    15. </script>
    image.png

    9-3.3秒倒计时

    通过设置number自减,再if判断num的值不小于零时每秒执行一次赋值给button,防止计时器无限循环下去
    1. <button id="btn">3</button>
    2. <script>
    3. var btn = document.getElementById("btn");
    4. var num = 3;
    5. var timer = setInterval(function(){
    6. num--;
    7. console.log(num);
    8. if(num>=0){
    9. btn.innerHTML = num;
    10. }
    11. else{
    12. clearInterval(timer)//防止计时器无限循环下去
    13. }
    14. },1000)
    15. </script>
    image.pngimage.png
    递归思路3秒倒计时
    通过先写函数function把num自减赋值btn,在num大于0的情况下再次调用函数自身递减赋值
    1. <button id="btn">3</button>
    2. <script>
    3. /* 递归:函数调用函数自身 */
    4. var btn = document.getElementById("btn");
    5. var num = 3;
    6. function go(){
    7. setTimeout(function(){
    8. num--;
    9. if(num>=0){
    10. btn.innerHTML = num;
    11. go();
    12. }
    13. },1000)
    14. }
    15. go();
    16. </script>
    image.pngimage.png

    10、按钮无法点击状态

    disabled设置按钮点击: —true 不能点击
    —false 能够点击的状态
  1. <button id="btn">发送验证码</button>
  2. <script>
  3. var btn = document.getElementById("btn");
  4. btn.disabled = true;
  5. /*
  6. disabled --true 不能点击
  7. --false 能够点击的状态
  8. */
  9. </script>

image.png