九、BOM(一) -- BOM对象

	DOM:文档对象模型
			通过JS来操作网页
			
	BOM:浏览器对象模型
			通过JS来操作浏览器的
			
			1、Window
					-Window对象代表整个浏览器的窗口
					-同时Window对象也是全局对象。
								在全局作用域中定义的变量会被当做window的属性来保存。
								在全局作用域定义的函数会被当做Window的函数来保存。
								
			2、Navigator
					-代表当前浏览器信息。通过该对象可以识别不同的浏览器
			
			3、Location
					-代表当前浏览器的地址栏信息。通过该对象可以获取地址栏信息或者跳转页面

			3、History
					-代表浏览器的历史记录,可以通过该对象来操作浏览器的历史记录。
					 由于隐私的原因,该对象不能获取到具体的历史记录。
					 只能操作浏览器向前或向后跳转。而且该操作只在当次访问时有效。
					 
			4、Screen
					-代表用户屏幕信息,通过该对象可以获取显示器的相关信息。
					-PC端很少用,移动端常用
			
			Window对象是全局对象,以上这些BOM对象都是作为Window的属性来保存的。
			所以可以直接通过Window对象来访问,也可以直接访问。
					

二、Navigator

	
	Navigator 
				1、代表浏览器信息,通过该对象可以识别不同的浏览器
				
				2、由于历史原因,Navigator对象中的大部分属性已经不能帮助我们识别浏览器了。		
								
								历史原因:IE浏览器为了防止歧视,就将Navigator对象中的属性设置为和其他浏览器相同的值。
								         这样就不能通过这些属性来区分IE浏览器了。
										 (X)
										 
2.1 通过Navigator对象的userAgent属性来识别浏览器

	Navigator.userAgent	
			
			1) 用这个属性来判断浏览器。userAgent属性返回一个字符串,这个字符串中包含了浏览器的相关信息。
			2) 通过userAgent可以识别浏览器,但是不能识别IE11。因为IE11又同化了这个属性。		
	
	
			    - 火狐的userAgent
						Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0
						
				- 谷歌的userAgent
						Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36
				
				- IE8的userAgent
						Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; McAfee; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)	
			
				- IE9的userAgent
						Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; McAfee; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)	
				
				- IE11的userAgent
						Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; McAfee; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; rv:11.0) like Gecko
						在IE11中已经将微软和IE相关的标识都已经去除了,所以不能通过userAgent来识别IE了。	

	

案例


      var ua=navigator.userAgent
      
      if(/Firefox/ig.test(ua)){
            alert("你是火狐浏览器")
      }

      if(/Chrome/ig.test(ua)){
            alert("你是谷歌浏览器")
      }

      //只能识别IE8、IE9、IE10浏览器
      if(/MSIE/ig.test(ua)){
            alert("你是IE浏览器")
      }

>>>>>> 可以识别谷歌浏览器

在这里插入图片描述

>>>>>> 可以识别火狐浏览器

在这里插入图片描述

>>>>>> 可以识别IE8、IE9、IE10浏览器

在这里插入图片描述

>>>>>> 不能识别IE11浏览器

在这里插入图片描述

2.2 通过ActiveXObject对象来识别IE浏览器

由于通过userAgent不能识别IE11浏览器,则需要通过其他方式来识别IE。

	
	如果通过userAgent不能判断IE浏览器,则可以通过浏览器特有对象来判断浏览器。
	 比如ActiveXObject。它是IE中特有对象。	
	 
>>>>>> 这种写法不能识别IE浏览器(不能识别IE11)
		
	这种写法可以识别IE78910浏览器。但不能识别IE11浏览器
	因为IE11修改了这个默认值。 !!window.ActiveXObject =false
				
				IE真是小人啊。为了防止自己不被歧视,
				连原则都不坚持,将基本属性都改了。
					

        if(window.ActiveXObject) {
            console.log("你是IE浏览器")
        }      

在这里插入图片描述
在这里插入图片描述

>>>>>> 这种写法可以识别IE浏览器

        if("ActiveXObject" in window) {
          			 console.log("你是IE浏览器")
       	}       
	

在这里插入图片描述

2.3 识别浏览器综合处理

      var ua=navigator.userAgent
      
      if(/Firefox/ig.test(ua)){
            alert("你是火狐浏览器")
      }

      if(/Chrome/ig.test(ua)){
            alert("你是谷歌浏览器")
      }

      //只能识别IE8、IE9、IE10浏览器
      //   if(/MSIE/ig.test(ua)){
      //         alert("你是IE浏览器")
      //   }
       
      if("ActiveXObject" in window){
            alert("你是IE浏览器")
      }

三、History对象

	
	History
			-最初的设计来表示浏览器的历史记录。
			 由于隐私的原因,该对象不能获取到具体的历史记录。
			
			-只能操作浏览器在历史页面中向前或向后跳转。而且该操作只在当次访问时有效。
			
			 						
			切记
				【1、通过history跳转的页面是指那些在同一个页面窗口中已经访问的网页】
				【2、只在当前访问有效,只在当前页面窗口有效】
			 
	
	history.length 
				获取在同一个页面窗口中访问的页面数量。
	
	history.back()
				跳转到上一个历史页面,作用和浏览器回退相同
	
	history.forward()
				跳转到下一个历史页面,作用和浏览器前进相同
	
	history.go(数字)
				跳转到指定的历史页面。
				需要一个数字作为参数,表示跳转到第几个历史页面。 
				数字>0,向前跳转。数字<0,向后跳转。
				
						history.go(-1)  回退,作用和back() 相同
						history.go(1)   向前,作用和forward() 相同
						history.go(-3)  回退到第3个历史页面
						
>>>>>> history.length 获取当前页面窗口浏览的页面的个数,即历史页面的个数
<script>
        console.log(history.length);
</script>

在这里插入图片描述

在这里插入图片描述

>>>>>> history.back()、history.forward() 跳转历史页面
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <button>向前跳转</button>
    <button>向后跳转</button>

</body>

<script>

    var btns=document.getElementsByTagName("button");

    btns[0].onclick=function(){
        history.back(); //向前跳转,相当于浏览器的回退
    
    }

    
    btns[1].onclick=function(){
        history.forward(); //向后跳转,相当于浏览器的前进
    }

</script>

</html>
>>>>>> history.go(数字) 跳转到指定的历史页面
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <button>向前跳转</button>
    <button>向后跳转</button>
    <button>跳转到第3个历史页面</button>

</body>

<script>

    var btns=document.getElementsByTagName("button");

    btns[0].onclick=function(){
        //history.back(); //向前跳转,相当于浏览器的回退
        history.go(1)
    }

    
    btns[1].onclick=function(){
        // history.forward(); //向后跳转,相当于浏览器的前进
        history.go(-1)
    }

    btns[2].onclick=function(){
        history.go(3)
    }

</script>

</html>

四、Location对象

	
	Location
			- 代表当前浏览器的地址栏信息。通过该对象可以获取地址栏信息或者跳转页面
			
			- 直接打印Location,会获取到地址栏的URL路径
			- 
4.1 location 对象的属性

		href		设置或返回 完整URL路径。
							可以设置绝对路径跳转
										location.href="http://www.baidu.com"
							可以设置相对路径跳转
										location.href="./index3.html"

							
		host		设置或返回 域名:端口
		hostname	设置或返回 域名		
		port		设置或返回 端口
		
		protocol	设置或返回协议。http 或 https
		pathname	设置或返回 URL 的路径部分。
		hash		设置或返回 从#开始的字符串。
		search		设置或返回 从?开始的字符串。
		
>>>>>> 设置或返回location对象的基本属性

<script>

    //设置或返回完整URL路径
    console.log("href:"+location.href);

    
    //设置或返回域名:端口
    console.log("host:"+location.host);
    //设置或返回域名
    console.log("hostname:"+location.hostname);
    //设置或返回端口
    console.log("port:"+location.port);


    //设置或返回协议
    console.log("protocol:"+location.protocol);
    //设置或返回URL的路径部分
    console.log("pathname:"+location.pathname);


    //设置或返回 从?开始的内容
    console.log("search:"+location.search);
    //设置或返回 从#开始的内容
    console.log("hash:"+location.hash);

</script>

在这里插入图片描述

>>>>>> 通过location.href跳转页面
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button> 跳转百度</button>
    <button> 跳转index9.html</button>

</body>

<script>

    var btns=document.getElementsByTagName("button");

    //通过绝对路径跳转
    btns[0].onclick=function(){
        location.href="http://www.baidu.com"
    }

    //通过相对路径跳转
    btns[1].onclick=function(){
        location.href="./index9.html"
    }

</script>

</html>

在这里插入图片描述

4.2 location 对象的方法

		location.assign("http://www.baidu.com");
					跳转页面,相当于 location.href="http://www.baidu.com"
		
		location.reload();
					刷新页面,相当于F5刷新
	
		location.reload(true);
					强制清空缓存刷新页面,相当于 ctrl+F5 强制清空缓存刷新网页

>>>>>> 通过location.assign跳转页面
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button> 跳转百度</button>
    <button> 跳转index9.html</button>

</body>

<script>

    var btns=document.getElementsByTagName("button");

    //通过绝对路径跳转
    btns[0].onclick=function(){
        location.assign("http://www.baidu.com")
    }

    //通过相对路径跳转
    btns[1].onclick=function(){
        location.assign("./index9.html")
    }

</script>

</html>

在这里插入图片描述

>>>>>> 通过location.reload()刷新页面
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="text"><br/><br/>

    <button> 刷新</button>
    <button> 强制刷新</button>

</body>

<script>

    var btns=document.getElementsByTagName("button");

    //刷新
    btns[0].onclick=function(){
        location.reload();
    }

    //强制刷新
    btns[1].onclick=function(){
        location.reload(true);
    }

</script>

</html>

五、window对象


	
	+++ 对话框
	
			alert("123")	     显示带有一段消息和一个确认按钮的警告框。
			prompt("请输入姓名")	 显示可提示用户输入的对话框。
			confirm("请确认")	 显示带有一段消息以及确认按钮和取消按钮的对话框。



	+++ 定时函数	
			setInterval(show,1000)	
			setInterval("show()",1000)
			setInterval(function(){console.log("111")},1000)
								开启定时调用,每隔多少毫秒定时调用函数。
								返回一个number类型ID。用于定时器的唯一标识
								
			clearInterval(id)	关闭setInterval()开启的定时任务



	+++ 延时函数
			setTimeout(show,1000)	
			setTimeout("show()",1000)
			setTimeout(function(){console.log("111")},1000)
								开启延时调用,隔多少秒开始调用函数。只会调用一次
								返回一个number类型ID。用于定时器的唯一标识
				
			clearTimeout()	取消由 setTimeout() 方法设置的 timeout。
		



	+++ 其他
			print()	打印当前窗口的内容。
			close()	关闭浏览器窗口。
			open()	打开一个新的浏览器窗口或查找一个已命名的窗口。


5.1 对话框
<body>

    <button> 警告框</button>
    <button> 确认框</button>
    <button> 对话框</button>

</body>

<script>


    var btns=document.getElementsByTagName("button");

    //警告窗
    btns[0].onclick=function(){
       window.alert("qwe");
    }

    //确认框
    btns[1].onclick=function(){
        var flag=window.confirm("你确定?");
        console.log(flag)
    }

    //对话框
    btns[2].onclick=function(){
        var str=window.prompt("请输入你的姓名")
        console.log(str)
    }



</script>

警告窗
在这里插入图片描述
确认框
在这里插入图片描述
对话框
在这里插入图片描述

5.2 定时函数
1)开启定时函数

        //方式一
        function show(){
            console.log("123")
        }
        window.setInterval("show()",1000);


        //方式二
        function show(){
            console.log("123")
        }
        window.setInterval(show,1000);



        //方式三
        window.setInterval(function(){
            console.log("123")
        },1000);
        
2)关闭定时函数
<body>

    <div></div>
    <button >关闭定时任务</button>
    
</body>

<script>

  	
  	//开启定时任务
    var id=window.setInterval(function(){
        var div=document.getElementsByTagName("div")[0];
        div.innerHTML=new Date();
    },1000)

    //关闭定时任务
    document.getElementsByTagName("button")[0].onclick=function(){
        window.clearInterval(id);
    }

</script>

在这里插入图片描述

3)案例
>>>>>> 案例1:动态切换图片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        div{
            width:400px;
            height:400px;
            overflow: hidden;
            margin-bottom: 20px;
        }

        img{
            transition: 1s;
            width:400px;
            height:400px;
        }
      
    </style>
</head>
<body>
    
    <div>
        <img src="./img/1 (1).png">
    </div>

    <button>开始</button>
    <button>停止</button>
</body>

<script>

    var imgs=["1 (1).png","1 (2).png","1 (3).png","1 (4).png"];


    var img=document.getElementsByTagName("img")[0]
    var btn1=document.getElementsByTagName("button")[0]
    var btn2=document.getElementsByTagName("button")[1]
    var tFalg;

    btn1.onclick=function(){

        //阻止重复点击,开启多个定时事件
        if(tFalg){
            return;
        }

        var i=0;
        tFalg=window.setInterval(function(){
            i++;
            if(i==imgs.length){
                i=0;
            }
            img.src="./img/"+imgs[i];
        },1000);
    }

    btn2.onclick=function(){
        window.clearInterval(tFalg)
    }

   

</script>

</html>

在这里插入图片描述

>>>>>> 案例2:按键控制DIV移动

案例缺点:按键持续按下时,第一次有明显的卡顿

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body >

    <div style="width:100px;height: 100px;background-color: red; position: absolute;"></div>
</body>


<script>
    
    var div=document.getElementsByTagName("div")[0];
    document.onkeydown=function(event){

        event=event||window.event;

        //通过keyCode可以获取上下左右的码值
        //左:37   上:38  右:39   下:40
        console.log(event.keyCode)

        //按下左键
        if(event.keyCode==37){
            div.style.left=(div.offsetLeft-10)+"px"
        }
        //按下上键
        if(event.keyCode==38){
            div.style.top=(div.offsetTop-10)+"px"
        }
        //按下右键
        if(event.keyCode==39){
            div.style.left=(div.offsetLeft+10)+"px"
        }
        //按下下键
        if(event.keyCode==40){
            div.style.top=(div.offsetTop+10)+"px"
        }
        
        //取消浏览器默认行为
        return false;
    }

</script>

</html>

在这里插入图片描述

使用定时器修改上述案例,解决按键持续按下时,第一次的卡顿感

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    
</head>
<body>
        <div style="width:100px;height:100px;background-color: red;position: absolute;"></div>
</body>

<script>



    var key=null;
    var speed=10;
    //将速度的控制交给定时任务
    window.setInterval(function(){
            switch(key){
                    case 37:
                            div.style.left=div.offsetLeft-speed +"px";
                            break;
                    case 38:
                            div.style.top=div.offsetTop - speed +"px";
                            break;
                    
                    case 39:
                            div.style.left=div.offsetLeft+speed +"px";
                            break;

                    case 40:
                            div.style.top=div.offsetTop+speed +"px";
                            break;

            }                   
    },30); 


    //按键只控制方向
    var div=document.getElementsByTagName("div")[0];
    document.onkeydown=function(event){

        event=event||window.event;

        //如果按住ctrl键,代表加速
        if(event.ctrlKey){
            speed=30;
        }else{
            speed=10;
        }
        
        //按键只控制方向
         key=event.keyCode;

    }

    document.onkeyup=function(){
        key=null;
    }

</script>

</html>

在这里插入图片描述

5.3 延时函数
1)开启延时函数
<script>

        //方式一
        function show(){
            console.log("123")
        }
        window.setTimeout("show()",1000);


        //方式二
        function show(){
            console.log("123")
        }
        window.setTimeout(show,1000);



        //方式三
        window.setTimeout(function(){
            console.log("123")
        },1000);
        
</script>
2)关闭延时函数
<script>

     
        var id=window.setInterval(function(){
            console.log("123")
        },3000);

        window.clearTimeout(id);

</script>

六、定时函数完成JS动画特效

6.1 使用定时函数完成动画特效
1) 案例:回到顶部动画
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        .top{
            width:80px;
            height:80px;
            background-color: red;
            color:rgba(0,0,0,0.5);
            border-radius: 10%;
            font-size: 30px;
            font-weight: bolder;
            text-align: center;
            line-height: 40px;
            padding:10px;

            position: fixed;
            top:800px;
            left:1900px;
        }

       
    </style>
</head>
<body>
    ----------------
    <div style="margin-bottom:2000px;"></div>
    ---------------

    <div class="top">回到顶部</div>
</body>

<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
<script>

    var now=0;
    $(".top").click(function(){
        //顺时滚动到顶部
        // $("body,html").scrollTop(0);

        //使用定时器来完成动画滚动
        var id=setInterval(function(){
            now=$("body,html").scrollTop()-100;
            $("body,html").scrollTop(now);
            if(now<0){
                clearInterval(id);
            }
        },10);

    })  


</script>
</html>

在这里插入图片描述

2) 案例:移动div动画
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    
</head>
<body>
        <button> 向右移动div</button>
        <button> 向左移动div</button>
        <div style="width:100px;height:100px;background-color: red;position: absolute;margin-top:20px;"></div>

        <div style="width:2px;background-color: blue; height:800px;position: absolute; left:1200px;"></div>
</body>

<script>

        var timer;
        var div=document.getElementsByTagName("div")[0];
      
        var btn1=document.getElementsByTagName("button")[0];
        var btn2=document.getElementsByTagName("button")[1];

        //向右移动
        btn1.onclick=function(event){

            window.clearInterval(timer);
 
            event=event||window.event;
            var speed=11;

            timer=window.setInterval(function(){

                    var move=div.offsetLeft+speed;
                    if(move>=1200){
                        move=1200;
                        window.clearInterval(timer)
                    }
                    div.style.left=move +"px";

            },30)
        }

        //向左移动
        btn2.onclick=function(event){
            event=event||window.event;

            window.clearInterval(timer);
            var speed=11;

            timer=window.setInterval(function(){

                var move=div.offsetLeft-speed;
                if(move<=0){
                    move=0;
                    window.clearInterval(timer)
                }
                div.style.left=move +"px";
            },30)
        }

</script>

</html>

在这里插入图片描述

6.2 自定义动画函数
   
        //封装动画函数
        //obj   操作的元素对象
        //attr  操作的css属性
        //spped 速度
        //callFun 回调函数
        function move(obj,attr,target,speed,callFun){
           
            if(obj.timer){
                window.clearInterval(obj.timer);
            }
 
            event=event||window.event;
            var nowPosition=parseInt(obj.style[attr]);

            //设置speed正负号
            if(target <  nowPosition){
                speed=-speed;
            }

            obj.timer=window.setInterval(function(){
                console.log("1")
                var newPosition=parseInt(obj.style[attr])+speed;
                
                if((speed >0 && newPosition>=target) || (speed<0 && newPosition<=target)){
                    newPosition= target;
                }
                
                obj.style[attr]=newPosition+"px";

                if(newPosition == target){
                    window.clearInterval(obj.timer)
                    callFun.call();    
                }
            },30)

        }

1)自定义动画函数的演变过程
>>>>>> 案例1:移动盒子
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    
</head>
<body>
        <button> 向右移动div</button>
        <button> 向左移动div</button>
        <div style="width:100px;height:100px;background-color: red;position: absolute;margin-top:20px;"></div>

        <div style="width:2px;background-color: blue; height:800px;position: absolute; left:1200px;"></div>
</body>

<script>

        var timer;
        var div=document.getElementsByTagName("div")[0];
      
        var btn1=document.getElementsByTagName("button")[0];
        var btn2=document.getElementsByTagName("button")[1];

        //向右移动
        btn1.onclick=function(event){

            window.clearInterval(timer);
 
            event=event||window.event;
            var speed=11;

            timer=window.setInterval(function(){

                    var move=div.offsetLeft+speed;
                    if(move>=1200){
                        move=1200;
                        window.clearInterval(timer)
                    }
                    div.style.left=move +"px";

            },30)
        }

        //向左移动
        btn2.onclick=function(event){
            event=event||window.event;

            window.clearInterval(timer);
            var speed=11;

            timer=window.setInterval(function(){

                var move=div.offsetLeft-speed;
                if(move<=0){
                    move=0;
                    window.clearInterval(timer)
                }
                div.style.left=move +"px";
            },30)
        }

</script>

</html>

在这里插入图片描述

>>>>>> 案例2:由移动盒子案例为基础,封装移动函数
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    
</head>
<body>
        <button> 向右移动div</button>
        <button> 向左移动div</button>
        <div style="width:100px;height:100px;background-color: red;position: absolute;margin-top:20px;left:0px;"></div>

        <div style="width:2px;background-color: blue; height:800px;position: absolute; left:1200px;"></div>
</body>

<script>


        
        //封装move移动函数
        function move(obj,target,speed){
           
            if(obj.timer){
                window.clearInterval(obj.timer);
            }
 
            event=event||window.event;
            var nowPosition=parseInt(obj.style.left);

            //设置speed正负号
            if(target <  nowPosition){
                speed=-speed;
            }

            obj.timer=window.setInterval(function(){
                console.log("1")
                var newPosition=parseInt(obj.style.left)+speed;
                
                if((speed >0 && newPosition>=target) || (speed<0 && newPosition<=target)){
                    newPosition= target;
                    window.clearInterval(obj.timer)
                }
                
                obj.style.left=newPosition+"px"
            },30)

        }


        var div=document.getElementsByTagName("div")[0];
      
        var btn1=document.getElementsByTagName("button")[0];
        var btn2=document.getElementsByTagName("button")[1];


        //向右移动
        btn1.onclick=function(event){
            move(div,1200,10);
        }

        //向左移动
        btn2.onclick=function(event){
            move(div,0,10);
        }

</script>

</html>

在这里插入图片描述

>>>>>> 案例3:由移动函数为基础,封装为动画函数
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    
</head>
<body>
        <button> 向右移动1200px</button>
        <button> 高度增长到1000px</button>
        <button> 高度减少到200px</button>

        <div style="width:100px;height:100px;background-color: red;position: absolute;margin-top:20px;left:0px;"></div>

</body>

<script>


        
        //封装move移动函数
        function move(obj,attr,target,speed,callFun){
           
            if(obj.timer){
                window.clearInterval(obj.timer);
            }
 
            event=event||window.event;
            var nowPosition=parseInt(obj.style[attr]);

            //设置speed正负号
            if(target <  nowPosition){
                speed=-speed;
            }

            obj.timer=window.setInterval(function(){
                console.log("1")
                var newPosition=parseInt(obj.style[attr])+speed;
                
                if((speed >0 && newPosition>=target) || (speed<0 && newPosition<=target)){
                    newPosition= target;
                }
                
                obj.style[attr]=newPosition+"px";

                if(newPosition == target){
                    window.clearInterval(obj.timer)
                    callFun.call();    
                }
            },30)

        }


        var div=document.getElementsByTagName("div")[0];
      
        var btn0=document.getElementsByTagName("button")[0];
        var btn1=document.getElementsByTagName("button")[1];
        var btn2=document.getElementsByTagName("button")[2];


        btn0.onclick=function(event){
            move(div,"left",1200,10,function(){alert("执行完毕")});
        }


        btn1.onclick=function(event){
            move(div,"height",1200,10,function(){alert("执行完毕")});
        }

        btn2.onclick=function(event){
            move(div,"height",200,10,function(){alert("执行完毕")});
        }


</script>

</html>

在这里插入图片描述

>>>>>> 案例4:自定义动画函数的高级应用
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    
</head>
<body>
        <button>开始动画</button>

        <div style="width:100px;height:100px;background-color: red;position: absolute;margin-top:20px;left:0px;"></div>

</body>

<script>


        
        //封装move移动函数
        function move(obj,attr,target,speed,callFun){
           
            if(obj.timer){
                window.clearInterval(obj.timer);
            }
 
            event=event||window.event;
            var nowPosition=parseInt(obj.style[attr]);

            //设置speed正负号
            if(target <  nowPosition){
                speed=-speed;
            }

            obj.timer=window.setInterval(function(){
                console.log("1")
                var newPosition=parseInt(obj.style[attr])+speed;
                
                if((speed >0 && newPosition>=target) || (speed<0 && newPosition<=target)){
                    newPosition= target;
                }
                
                obj.style[attr]=newPosition+"px";

                if(newPosition == target){
                    window.clearInterval(obj.timer)
                    callFun.call();    
                }
            },30)

        }


        var div=document.getElementsByTagName("div")[0];
      
        var btn0=document.getElementsByTagName("button")[0];


        btn0.onclick=function(event){
            //向右移动500px
            move(div,"left",500,10,function(){

                //高度增高500px
                move(div,"height",500,10,function(){
                    
                    //向左移动500px
                    move(div,"left",0,10,function(){
                        alert("动画完毕")
                    });
                });
            });
        }


      

</script>

</html>

在这里插入图片描述

2)自定义动画的使用
>>>>>> 轮播图

utils.js

 
        //封装动画函数
        //obj   操作的元素对象
        //attr  操作的css属性
        //spped 速度
        //callFun 回调函数
        function move(obj,attr,target,speed,callFun){
           
            if(obj.timer){
                window.clearInterval(obj.timer);
            }
 
            event=event||window.event;
            var nowPosition=parseInt(obj.style[attr]);
 
            //设置speed正负号
            if(target <  nowPosition){
                speed=-speed;
            }
 
            obj.timer=window.setInterval(function(){
                var newPosition=parseInt(obj.style[attr])+speed;
 
                if((speed >0 && newPosition>=target) || (speed<0 && newPosition<=target)){
                    newPosition= target;
                }
                
                obj.style[attr]=newPosition+"px";
 
                if(newPosition == target){
                    window.clearInterval(obj.timer)
                    callFun.call();    
                }
            },30)
 
        }

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<style>

    *{
        margin:0px;
        padding:0px;
        list-style: none;
    }

    .box{
        width:600px;
        height:470px;
        margin:20 auto;

        background-color: pink;
        padding:20px;
        padding-left:0px;

        margin:20px auto;

        position: relative;

        overflow:hidden;

    }

    .box ul:first-of-type{
        width:3600px;
        height:100%;

        position: absolute;
        left:0px;

    }

    .box img{
        width:580px;
        height:450px;
        
        float:left;
        margin-left:20px;
    }

    .box ul:last-of-type{
        position: absolute;
        width:500px;

        bottom:10px;
        left:25%;
        right:0px;
        margin:0px auto;


    }

    .box .dot{
        
        border:10px solid rgba(0,0,0,0.3);
        margin-left:20px;
        float:left;
    }

    .box .dot.w{
        border-color:white;
    }
    

</style>
<body>

    <div  class="box">
        <ul style="left:0px">
            <li><img src="1 (1).png"></li>
            <li><img src="1 (2).png"></li>
            <li><img src="1 (3).png"></li>
            <li><img src="1 (4).png"></li>
            <li><img src="1 (5).png"></li>
            <li><img src="1 (6).png"></li>
        </ul>
        
        <ul >
            <li class="dot w"></li>
            <li class="dot"></li>
            <li class="dot"></li>
            <li class="dot"></li>
            <li class="dot"></li>
            <li class="dot"></li>
        </ul>
</div>

</body>

<script type="text/javascript" src="./utils.js"></script>

<script>

    


       var imgUl=document.querySelector(".box ul:first-of-type");
       var dots=document.querySelectorAll(".box .dot");


       //刷新原点样式
       function refreshDot(target){
            for(var i=0;i<dots.length;i++){

                //清空样式
                dots[i].className="dot";
                
                if(i==target){
                    dots[i].className="dot  w";
                }

            }
       }


       // 轮播图
       var i=0;
       function callFun(){
            window.setTimeout(function(){
                
                if(i==6){
                    imgUl.style.left="0px"
                    i=0;
                }

                refreshDot(i);
                move(imgUl,"left",-600*i++,100,callFun);            
            },1000)
       }

      callFun();
      
      //点击原点切换图片
      for(var i=0;i<dots.length;i++){

        dots[i].index=i;

        dots[i].onclick=function(){
            i=this.index;
            refreshDot(this.index);

            imgUl.style.left=-600*this.index+"px";
        }
      }



</script>
</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值