zoukankan      html  css  js  c++  java
  • javaScript动画1 offsetWidth、offsetLeft

    offsetWidth和offsetHeight

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>offsetWidth和offsetHeight</title>
        <style>
            div{
                 100px;
                height: 100px;
                padding: 10px;
                border: 10px solid #000;
                margin: 10px;
                background-color: pink;
            }
        </style>
    </head>
    <body>
    <div class="box"></div>
    
    <script>
        div=document.getElementsByTagName("div")[0];
    
        //offsetHeight和offsetWidth没有单位(数字类型)
        //可以显示盒子的宽高,包括宽高本身padding,border;不包括margin
        console.log(div.offsetHeight);
        console.log(div.offsetWidth);
    </script>
    </body>
    </html>
    

     offsetLeft和offsetTop

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>02-offsetLeft和offsetTop</title>
        <style>
            .box1 {
                 300px;
                height: 300px;
                padding: 100px;
                background-color: pink;
                position: relative;
    
            }
    
            .box2 {
                 100px;
                height: 100px;
                background-color: red;
                position: absolute;
                /*left: 0;*/
                /*top: 0;*/
            }
        </style>
    </head>
    <body>
    <div class="box1">
        <div class="box3" style="position: absolute;">
            <div class="box2"></div>
        </div>
    </div>
    
    <script>
        var box2 = document.getElementsByClassName("box2")[0];
    
        //offsetTop和offsetLeft
        //检测距离父盒子有定位的左/上面的距离
        //如果父级都没有定位则以body为准
        console.log(box2.offsetTop);
        console.log(box2.offsetLeft);
    </script>
    </body>
    </html>
    

     offsetLeft和styleleft区别

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>offsetLeft和styleleft区别</title>
        <style>
            .box1 {
                 300px;
                height: 300px;
                padding: 100px;
                background-color: pink;
                position: relative;
    
            }
    
            .box2 {
                 100px;
                height: 100px;
                background-color: red;
                position: absolute;
                /*left: 0;*/
                /*top: 0;*/
            }
        </style>
    </head>
    <body>
    <div class="box1">
        <div class="box3" style="position: absolute;">
            <div class="box2"></div>
        </div>
    </div>
    
    <script>
        var box2 = document.getElementsByClassName("box2")[0];
    
        // 一、最大区别在于offsetLeft可以返回没有定位盒子的距离左侧的位置。而 style.left不可以
        //如果父系盒子都没有定位,以body为准
        //style.left只能获取行内式,如果没有返回""
    
        // 二、offsetTop 返回的是数字,而 style.top 返回的是字符串,除了数字外还带有单位:px。
        //100 100px
    
        // 三、offsetTop 只读,而 style.top 可读写。(只读是获取值,可写是赋值)
    
        // 四、如果没有给 HTML 元素指定过 top 样式,则style.top 返回的是空字符串。
    
    </script>
    </body>
    </html>

    案例-动画封装

    1、体验动画

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>动画</title>
    
        <style>
            div {
                height: 100px;
                 100px;
                background: pink;
                position: relative;
            }
        </style>
    </head>
    <body>
    <button>动画</button>
    <div class="box" style="left: 0;"></div>
    
    <script>
        var btn=document.getElementsByTagName("button")[0];
        var div=document.getElementsByTagName("div")[0];
        // btn.onclick = function () {
        //     div.style.left="500px";
        // }
    
        btn.onclick = function () {
            //定时器,每隔一段时间向右走一些
            setInterval(function () {
                //NaN不能用
                // div.style.left = parseInt(div.style.left)+10+"px";
    
                div.style.left = div.offsetLeft+10+"px";
    
                },300);
        }
    
    
    </script>
    </body>
    </html>

    2、动画封装

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>动画</title>
    
        <style>
            .box1 {
                margin: 0;
                padding: 5px;
                height: 200px;
                background-color: #ccc;
                position: relative;
    
             }
            .box2 {
                height: 100px;
                 100px;
                background: pink;
                position: absolute;
                left: 0px;
            }
    
            button{
                margin: 10px;
            }
    
        </style>
    </head>
    <body>
    
    <div class="box1">
        <button>运动到200</button>
        <button>运动到400</button>
        <div class="box2"></div>
    </div>
    
    
    <script>
        var btnArr=document.getElementsByTagName("button");
        var box2=document.getElementsByClassName("box2")[0];
    
        //定时器
        var timer=null;
    
    
        //绑定事件
        btnArr[0].onclick = function () {
            animate(200);
        };
    
        btnArr[1].onclick = function () {
            animate(400);
        };
    
        function animate(target) {
            timer = setInterval(function () {
                //盒子自身的位置加上步长
                box2.style.left=box2.offsetLeft+10+"px";
                if(box2.offsetLeft === target){
                    // clearInterval(timer)
                }
            },30);
        }
    
    
    
    
    </script>
    </body>
    </html>

    3、动画封装去除bug

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>动画</title>
    
        <style>
            .box1 {
                margin: 0;
                padding: 5px;
                height: 200px;
                background-color: #ccc;
                position: relative;
    
             }
            .box2 {
                height: 100px;
                 100px;
                background: pink;
                position: absolute;
                left: 0px;
            }
    
            button{
                margin: 10px;
            }
    
        </style>
    </head>
    <body>
    
    <div class="box1">
        <button>运动到200</button>
        <button>运动到400</button>
        <div class="box2"></div>
    </div>
    
    
    <script>
        var btnArr=document.getElementsByTagName("button");
        var box2=document.getElementsByClassName("box2")[0];
    
        //定时器
        var timer=null;
    
    
        //绑定事件
        btnArr[0].onclick = function () {
            animate(200);
        };
    
        btnArr[1].onclick = function () {
            animate(400);
        };
    
        function animate(target) {
            //BUG1 点击多次越来越快(每次只能开一个定时器,执行定时器前面先清除)
            //要用定时器先清除定时器
            clearInterval(timer);
            //BUG2 无法返回  原因就是步长不能是恒定值。
            // 传递的目标值如果比当前值大,那么步长为+10
            // 传递的目标值如果比当前值小,那么步长为-10
            // var speed = (target - box2.offsetLeft)>0?10:-10;
            var speed = target > box2.offsetLeft ?10:-10;
            timer = setInterval(function () {
                var val=target - box2.offsetLeft;
                //盒子自身的位置加上步长
                box2.style.left=box2.offsetLeft+speed+"px";
                if(Math.abs(val)<=Math.abs(speed)){
                    box2.style.left=target + "px";
                    clearInterval(timer)
                }
            },30);
        }
    
    </script>
    </body>
    </html>

    4、动画封装最终

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>动画</title>
    
        <style>
            .box1 {
                margin: 0;
                padding: 5px;
                height: 200px;
                background-color: #ccc;
                position: relative;
    
             }
            .box2 {
                height: 100px;
                 100px;
                background: pink;
                position: absolute;
                left: 0px;
                top: 30px;
            }
            .box3 {
                height: 100px;
                 100px;
                background: yellow;
                position: absolute;
                left: 0px;
                top:150px;
            }
    
            button{
                margin: 10px;
            }
    
        </style>
    </head>
    <body>
    
    <div class="box1">
        <button>运动到200</button>
        <button>运动到400</button>
        <div class="box2"></div>
        <div class="box3"></div>
    </div>
    
    
    <script>
        var btnArr=document.getElementsByTagName("button");
        var box2=document.getElementsByClassName("box2")[0];
        var box3=document.getElementsByClassName("box3")[0];
    
        //定时器
        // var timer=null;
    
    
        //绑定事件
        btnArr[0].onclick = function () {
            //如果有一天要传递另外一个盒子,方法不能用
            //所以我们要传递第二个参数,被移动盒子本身
            animate(box2,200);
            animate(box3,200);
        };
    
        btnArr[1].onclick = function () {
            animate(box2,400);
        };
    
    
        function animate(ele,target) {
            //BUG1 点击多次越来越快(每次只能开一个定时器,执行定时器前面先清除)
            //要用定时器先清除定时器(直接绑在盒子上)
            clearInterval(ele.timer);
            //BUG2 无法返回  原因就是步长不能是恒定值。
            // 传递的目标值如果比当前值大,那么步长为+10
            // 传递的目标值如果比当前值小,那么步长为-10
            // var speed = (target - box2.offsetLeft)>0?10:-10;
            var speed = target > ele.offsetLeft ?10:-10;
            ele.timer = setInterval(function () {
                var val=target - ele.offsetLeft;
                //盒子自身的位置加上步长
                ele.style.left=ele.offsetLeft+speed+"px";
                if(Math.abs(val)<=Math.abs(speed)){
                    ele.style.left=target + "px";
                    clearInterval(ele.timer)
                }
            },30);
        }
    
    </script>
    </body>
    </html>
    

     案例-滑动焦点图

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>滑动焦点图</title>
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            .box{
                 490px;
                height: 170px;
                padding: 5px;
                border: 1px solid #ccc;
                margin: 100px auto;
            }
            .inner{
                 490px;
                height: 170px;
                position: relative;
                overflow: hidden;
            }
            ul{
                list-style: none;
                 500%;
                position: absolute;
                left: 0;
            }
            li{
                float: left;
            }
            .square{
                position: absolute;
                bottom: 10px;
                right: 10px;
    
            }
            span{
                display: inline-block;
                 16px;
                height: 16px;
                background-color: #fff;
                text-align: center;
                margin: 3px;
                border: 1px solid #ccc;
                line-height: 16px;
                cursor: pointer;
    
            }
            .current{
                background-color: darkorange;
                color: #fff;
            }
        </style>
        <script>
            window.onload = function () {
                //鼠标放到小方块上面,上面的图片向左移动指定个数的图片
                //绑定事件移动ul点亮指定的盒子,移动ul
                //1 获取事件源和相关元素
    
                //2 绑定事件和书写事件驱动程序(循环绑定)
    
                //3 点亮盒子
    
                //4 移动ul(向左移动,给定目标为负值,放到第n个小方块上,向左移动n-1个)
    
    
    
                //1 获取事件源和相关元素
                var inner =  document.getElementById("inner");
                var imgWidth=inner.offsetWidth;
                var ul = inner.children[0];
                var spanArr = inner.children[1].children;
    
                //2 绑定事件和书写事件驱动程序(循环绑定)
                for(var i=0;i<spanArr.length;i++){
                    //属性绑定(自定义属性参数盒子的索引值)
                    //用span的innerHTML属性也可以(用自己的属性)
                    spanArr[i].index = i; //绑定的是索引值,所以移动盒子的时候不用n-1
    
                    spanArr[i].onmouseover = function () {
                        //3 点亮盒子(排他思想)
                        for(var j=0;j<spanArr.length;j++){
                            spanArr[j].className = "";
                        }
                        this.className = "current";
    
                        //4 移动ul(向左移动,给定目标为负值,放到第n个小方块上,向左移动n-1个)
                        //利用框架移动盒子,(两个参数 1元素 2 目标位置)
                        animate(ul,this.index*-imgWidth)
                    }
    
                }
    
                function animate(ele,target) {
                    clearInterval(ele.timer);
                    var speed = target > ele.offsetLeft ?10:-10;
                    ele.timer = setInterval(function () {
                        var val=target - ele.offsetLeft;
                        ele.style.left=ele.offsetLeft+speed+"px";
                        if(Math.abs(val)<=Math.abs(speed)){
                            ele.style.left=target + "px";
                            clearInterval(ele.timer)
                        }
                    },10);
                }
    
    
    
    
    
    
    
    
    
    
    
    
                //1 获取事件源和相关元素
    
                //2 绑定事件和书写事件驱动程序(循环绑定)
    
                //3 点亮盒子
    
                //4 移动ul(向左移动,给定目标为负值,放到第n个小方块上,向左移动n-1个)
    
            }
        </script>
    </head>
    <body>
        <div class="box">
            <div class="inner" id="inner">
                <ul>
                    <li><img src="images/01.jpg" alt=""></li>
                    <li><img src="images/02.jpg" alt=""></li>
                    <li><img src="images/03.jpg" alt=""></li>
                    <li><img src="images/04.jpg" alt=""></li>
                    <li><img src="images/05.jpg" alt=""></li>
                </ul>
    
                <div class="square" >
                    <span class="current">1</span>
                    <span>2</span>
                    <span>3</span>
                    <span>4</span>
                    <span>5</span>
                </div>
            </div>
        </div>
    </body>
    </html>

    案例-左右焦点图

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	<title>无标题文档</title>
    	<style type="text/css">
    		body,ul,ol,li,img{margin:0;padding:0;list-style:none;}
    		#box{490px;height:170px;padding:5px;
    			position: relative;border:1px solid #ccc;margin:100px auto 0;overflow:hidden;}
    		.ad{490px;height:170px;overflow:hidden;position:relative;}
    		#box img{490px;}
    		.ad ol{position:absolute;right:10px;bottom:10px;}
    		.ad ol li{20px;height:20px;line-height:20px;border:1px solid #ccc;text-align:center;background:#fff;float:left;margin-right:10px;cursor:pointer;_display:inline;}
    		.ad ol li.current{background:yellow;}
    		.ad ul li{float:left;}
    		.ad ul{ position:absolute; top:0; 2940px;}
    		.ad ul li.current{display:block;}
    		#arr {display: none;}
    		#arr span{ 40px; height:40px; position:absolute; left:5px; top:50%; margin-top:-20px; background:#000; cursor:pointer; line-height:40px; text-align:center; font-weight:bold; font-family:'黑体'; font-size:30px; color:#fff; opacity:0.3; border:1px solid #fff;}
    		#arr #right{right:5px; left:auto;}
    	</style>
    </head>
    <body>
    <div id="box" class="all">
    	<div class="ad">
    		<ul id="imgs">
    			<li><img src="images/1.jpg" /></li>
    			<li><img src="images/2.jpg" /></li>
    			<li><img src="images/3.jpg" /></li>
    			<li><img src="images/4.jpg" /></li>
    			<li><img src="images/5.jpg" /></li>
    		</ul>
    	</div>
    	<div id="arr">
    		<span id="left"><</span>
    		<span id="right">></span>
    	</div>
    </div>
    
    <script>
        //1 点击左侧按钮,ul向右移动,相反按钮,向左移动
        //2 如何移动盒子,利用计数器模拟index值,点右侧自增,点左自减
        //3 点击左侧盒子,同理
    
        //1 点击左侧按钮,ul向右移动,相反按钮,向左移动
        var box = document.getElementById("box");
        var imgWidth= box.children[0].offsetWidth;
        var ul = box.children[0].children[0];
        var boxLeftRight = box.children[1];
        var btnArr = boxLeftRight.children;
    
    
        //鼠标放上去显示,离开隐藏
        box.onmouseover = function () {
            boxLeftRight.style.display = "block";
        };
        box.onmouseout = function () {
            boxLeftRight.style.display = "none";
        };
    
        //2 如何移动盒子,利用计数器模拟index值,点右侧自增,点左自减
        //定义计数器
        var index = 0;
    
        // btnArr[1].onclick = function () {
        btnArr[1].onclick = function () {
            index++;
            //要对index的值进行约束index在[0-4]
            if(index>ul.children.length-1){
    
                index=ul.children.length-1;
            }
            //点击盒子以后移动图片
            animate(ul,-index*imgWidth);
    
        };
        //3 点击左侧盒子,同理
        btnArr[0].onclick = function () {
    
            index--;
            if(index<0){
                index=0;
            }
            //点击盒子以后移动图片
            animate(ul,-index*imgWidth);
        };
    
    
        function animate(ele,target) {
            clearInterval(ele.timer);
            var speed = target > ele.offsetLeft ? 10 : -10;
            ele.timer = setInterval(function () {
                var val = target - ele.offsetLeft;
                ele.style.left = ele.offsetLeft + speed + "px";
                if (Math.abs(val) <= Math.abs(speed)) {
                    ele.style.left = target + "px";
                    clearInterval(ele.timer)
                }
            }, 10);
        }
    
    </script>
    </body>
    </html>
    

     案例-带有定时器的无限轮播图

    循序渐进

    01 

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <style type="text/css">
    *{ padding:0; margin:0; list-style:none; border:0;}
    .all{
      500px;
      height:200px;
      padding:7px;
      border:1px solid #ccc;
      margin:100px auto;
      position:relative;
    }
    .screen{
    	500px;
    	height:200px;
    	 overflow:hidden; 
    	position:relative;
    }
    .screen li{ 500px; height:200px; overflow:hidden; float:left;}
    .screen ul{ position:absolute; left:0; top:0px; 3000px;}
    .all ol{ position:absolute; right:10px; bottom:10px; line-height:20px; text-align:center;}
    .all ol li{ float:left; 20px; height:20px; background:#fff; border:1px solid #ccc; margin-left:10px; cursor:pointer;}
    .all ol li.current{ background:yellow;}
    
    /*#arr {display: none;}*/
    #arr span{ 40px; height:40px; position:absolute; left:5px; top:50%; margin-top:-20px; background:#000; cursor:pointer; line-height:40px; text-align:center; font-weight:bold; font-family:'黑体'; font-size:30px; color:#fff; opacity:0.3; border:1px solid #fff;}
    #arr #right{right:5px; left:auto;}
    
    </style>
    
    
    </head>
    
    <body>
    <div class="all" id='all'>
    	<div class="screen" id="screen">
            <ul id="ul">
                <li><img src="images/1.jpg" width="500" height="200" /></li>
                <li><img src="images/2.jpg" width="500" height="200" /></li>
                <li><img src="images/3.jpg" width="500" height="200" /></li>
                <li><img src="images/4.jpg" width="500" height="200" /></li>
                <li><img src="images/5.jpg" width="500" height="200" /></li>
            </ul>
            <ol></ol>
            <div id="arr"><span id="left"><</span><span id="right">></span></div>
        </div>
    </div>
    </body>
    </html>
    <script>
        //需求:无缝轮播图
        //步骤:
        //1.老三步。获取相关元素。
        //2.补齐相互盒子
            //1.复制第一张图片所在的li,填入所在的ul中。
            //2.生成相关的ol中的li。
            //3.点亮第一个ol中的li。
        //3.鼠标放到小方块儿上,轮播图片。
        //4.添加定时器。
        //5.左右切换的按钮。
    
    
    
        //1.老三步。获取相关元素。
        var box = document.getElementById("all");
        var ul = box.children[0].children[0];
        var ol = box.children[0].children[1];
        var ulLiArr = ul.children;
        //2.补齐相互盒子
            //1.复制第一张图片所在的li,填入所在的ul中。
        var newLi = ulLiArr[0].cloneNode(true);
        ul.appendChild(newLi);
            //2.生成相关的ol中的li。
        for(var i=0;i<ulLiArr.length-1;i++){
            var newOlLi = document.createElement("li");
            newOlLi.innerHTML = i+1;
            ol.appendChild(newOlLi);
        }
            //3.点亮第一个ol中的li。
        var olLiArr = ol.children;
        ol.children[0].className = "current";
        //3.鼠标放到小方块儿上,轮播图片。
        for(var i=0;i<olLiArr.length;i++){
            olLiArr[i].index = i;
            olLiArr[i].onmouseover = function () {
                for(var j=0;j<olLiArr.length;j++){
                    olLiArr[j].className = "";
                }
                olLiArr[this.index].className = "current";
                animate(ul,-this.index*ul.children[0].offsetWidth);
                key = square = this.index;
            }
        }
        //4.添加定时器。
        var timer = null;
        var key = 0;
        var square = 0;
        timer = setInterval(autoPlay,1000);
    
        function autoPlay(){
            key++;
            square++;
            if(key>olLiArr.length){
                key=1;
                ul.style.left = 0;
            }
            animate(ul,-key*ul.children[0].offsetWidth);
    
            square = square>olLiArr.length-1?0:square;
            for(var j=0;j<olLiArr.length;j++){
                olLiArr[j].className = "";
            }
            olLiArr[square].className = "current";
        }
        //5.鼠标移开开启定时器,鼠标放上去开启定时器。
        box.onmouseover = function () {
            clearInterval(timer);
        }
        box.onmouseout = function () {
            timer = setInterval(autoPlay,1000);
        }
        //6.左右切换的按钮。
        var btnArr = box.children[0].children[2].children;
        btnArr[0].onclick = function () {
            key--;
            square--;
            if(key<0){
                key=4;
                ul.style.left = -5*ul.children[0].offsetWidth + "px";
            }
            animate(ul,-key*ul.children[0].offsetWidth);
    
            square = square<0?olLiArr.length-1:square;
            for(var j=0;j<olLiArr.length;j++){
                olLiArr[j].className = "";
            }
            olLiArr[square].className = "current";
        }
        btnArr[1].onclick = function () {
            autoPlay();
        }
    
    
    
    
        //  基本封装
        function animate(obj,target) {
            clearInterval(obj.timer);
            var speed = obj.offsetLeft < target ? 15 : -15;
            obj.timer = setInterval(function() {
                var result = target - obj.offsetLeft;
                obj.style.left = obj.offsetLeft + speed  + "px";
                if(Math.abs(result) <= 15) {
                    obj.style.left = target + "px";
                    clearInterval(obj.timer);
                }
            },10);
        }
    
    
    </script>
    

     02

    <!doctype html>
    <html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>无标题文档</title>
        <style type="text/css">
            *{ padding:0; margin:0; list-style:none; border:0;}
            .all{
                500px;
                height:200px;
                padding:7px;
                border:1px solid #ccc;
                margin:100px auto;
                position:relative;
            }
            .screen{
                500px;
                height:200px;
                overflow:hidden;
                position:relative;
            }
            .screen li{ 500px; height:200px; overflow:hidden; float:left;}
            .screen ul{ position:absolute; left:0; top:0px; 3000px;}
            .all ol{ position:absolute; right:10px; bottom:10px; line-height:20px; text-align:center;}
            .all ol li{ float:left; 20px; height:20px; background:#fff; border:1px solid #ccc; margin-left:10px; cursor:pointer;}
            .all ol li.current{ background:yellow;}
    
            #arr {display: none;}
            #arr span{ 40px; height:40px; position:absolute; left:5px; top:50%; margin-top:-20px; background:#000; cursor:pointer; line-height:40px; text-align:center; font-weight:bold; font-family:'黑体'; font-size:30px; color:#fff; opacity:0.3; border:1px solid #fff;}
            #arr #right{right:5px; left:auto;}
        </style>
    
        <script>
            window.onload = function () {
    
                //需求:无缝滚动。
                //思路:赋值第一张图片放到ul的最后,然后当图片切换到第五张的时候
                //     直接切换第六章,再次从第一张切换到第二张的时候先瞬间切换到
                //     第一张图片,然后滑动到第二张
                //步骤:
                //1.获取事件源及相关元素。(老三步)
                //2.复制第一张图片所在的li,添加到ul的最后面。
                //3.给ol中添加li,ul中的个数-1个,并点亮第一个按钮。
                //4.鼠标放到ol的li上切换图片
                //5.添加定时器
                //6.左右切换图片(鼠标放上去隐藏,移开显示)
    
    
    
    
    
    
                //1.获取事件源及相关元素。(老三步)
                var all = document.getElementById("all");
                var screen = all.firstElementChild || all.firstChild;
                var imgWidth = screen.offsetWidth;
                var ul = screen.firstElementChild || all.firstChild;
                var ol = screen.children[1];
                var div =screen.lastElementChild || screen.lastChild;
                var spanArr = div.children;
    
                //2.复制第一张图片所在的li,添加到ul的最后面。
                var ulNewLi = ul.children[0].cloneNode(true);
                ul.appendChild(ulNewLi);
    
                // //3.给ol中添加li,ul中的个数-1个,并点亮第一个按钮。
                for(var i = 0 ;i<ul.children.length-1;i++){
                    var olNewLi = document.createElement("li");
                    olNewLi.innerHTML = i+1;
                    ol.appendChild(olNewLi);
                }
                var olLiArr = ol.children;
                olLiArr[0].className = "current";
                //4.鼠标放到ol的li上切换图片
                //5.添加定时器
                //6.左右切换图片(鼠标放上去显示,移开隐藏)
    
    
    
            }
        </script>
    </head>
    
    <body>
    <div class="all" id='all'>
        <div class="screen" id="screen">
            <ul id="ul">
                <li><img src="images/1.jpg" width="500" height="200" /></li>
                <li><img src="images/2.jpg" width="500" height="200" /></li>
                <li><img src="images/3.jpg" width="500" height="200" /></li>
                <li><img src="images/4.jpg" width="500" height="200" /></li>
                <li><img src="images/5.jpg" width="500" height="200" /></li>
            </ul>
            <ol>
    
            </ol>
            <div id="arr">
                <span id="left"><</span>
                <span id="right">></span>
            </div>
        </div>
    </div>
    </body>
    </html>
    

     03

    <!doctype html>
    <html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>无标题文档</title>
        <style type="text/css">
            *{ padding:0; margin:0; list-style:none; border:0;}
            .all{
                500px;
                height:200px;
                padding:7px;
                border:1px solid #ccc;
                margin:100px auto;
                position:relative;
            }
            .screen{
                500px;
                height:200px;
                overflow:hidden;
                position:relative;
            }
            .screen li{ 500px; height:200px; overflow:hidden; float:left;}
            .screen ul{ position:absolute; left:0; top:0px; 3000px;}
            .all ol{ position:absolute; right:10px; bottom:10px; line-height:20px; text-align:center;}
            .all ol li{ float:left; 20px; height:20px; background:#fff; border:1px solid #ccc; margin-left:10px; cursor:pointer;}
            .all ol li.current{ background:yellow;}
    
            #arr {display: none;}
            #arr span{ 40px; height:40px; position:absolute; left:5px; top:50%; margin-top:-20px; background:#000; cursor:pointer; line-height:40px; text-align:center; font-weight:bold; font-family:'黑体'; font-size:30px; color:#fff; opacity:0.3; border:1px solid #fff;}
            #arr #right{right:5px; left:auto;}
        </style>
    
        <script>
            window.onload = function () {
    
                //需求:无缝滚动。
                //思路:赋值第一张图片放到ul的最后,然后当图片切换到第五张的时候
                //     直接切换第六章,再次从第一张切换到第二张的时候先瞬间切换到
                //     第一张图片,然后滑动到第二张
                //步骤:
                //1.获取事件源及相关元素。(老三步)
                //2.复制第一张图片所在的li,添加到ul的最后面。
                //3.给ol中添加li,ul中的个数-1个,并点亮第一个按钮。
                //4.鼠标放到ol的li上切换图片
                //5.添加定时器
                //6.左右切换图片(鼠标放上去隐藏,移开显示)
    
    
    
    
    
    
                //1.获取事件源及相关元素。(老三步)
                var all = document.getElementById("all");
                var screen = all.firstElementChild || all.firstChild;
                var imgWidth = screen.offsetWidth;
                var ul = screen.firstElementChild || all.firstChild;
                var ol = screen.children[1];
                var div =screen.lastElementChild || screen.lastChild;
                var spanArr = div.children;
    
                //2.复制第一张图片所在的li,添加到ul的最后面。
                var ulNewLi = ul.children[0].cloneNode(true);
                ul.appendChild(ulNewLi);
    
                // //3.给ol中添加li,ul中的个数-1个,并点亮第一个按钮。
                for(var i = 0 ;i<ul.children.length-1;i++){
                    var olNewLi = document.createElement("li");
                    olNewLi.innerHTML = i+1;
                    ol.appendChild(olNewLi);
                }
                var olLiArr = ol.children;
                olLiArr[0].className = "current";
                //4.鼠标放到ol的li上切换图片
                //5.添加定时器
                //6.左右切换图片(鼠标放上去显示,移开隐藏)
    
    
    
            }
        </script>
    </head>
    
    <body>
    <div class="all" id='all'>
        <div class="screen" id="screen">
            <ul id="ul">
                <li><img src="images/1.jpg" width="500" height="200" /></li>
                <li><img src="images/2.jpg" width="500" height="200" /></li>
                <li><img src="images/3.jpg" width="500" height="200" /></li>
                <li><img src="images/4.jpg" width="500" height="200" /></li>
                <li><img src="images/5.jpg" width="500" height="200" /></li>
            </ul>
            <ol>
    
            </ol>
            <div id="arr">
                <span id="left"><</span>
                <span id="right">></span>
            </div>
        </div>
    </div>
    </body>
    </html>
    

     04

    <!doctype html>
    <html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>无标题文档</title>
        <style type="text/css">
            *{ padding:0; margin:0; list-style:none; border:0;}
            .all{
                500px;
                height:200px;
                padding:7px;
                border:1px solid #ccc;
                margin:100px auto;
                position:relative;
            }
            .screen{
                500px;
                height:200px;
                overflow:hidden;
                position:relative;
            }
            .screen li{ 500px; height:200px; overflow:hidden; float:left;}
            .screen ul{ position:absolute; left:0; top:0px; 3000px;}
            .all ol{ position:absolute; right:10px; bottom:10px; line-height:20px; text-align:center;}
            .all ol li{ float:left; 20px; height:20px; background:#fff; border:1px solid #ccc; margin-left:10px; cursor:pointer;}
            .all ol li.current{ background:yellow;}
    
            #arr {display: none;}
            #arr span{ 40px; height:40px; position:absolute; left:5px; top:50%; margin-top:-20px; background:#000; cursor:pointer; line-height:40px; text-align:center; font-weight:bold; font-family:'黑体'; font-size:30px; color:#fff; opacity:0.3; border:1px solid #fff;}
            #arr #right{right:5px; left:auto;}
        </style>
    
        <script>
            window.onload = function () {
    
                //需求:无缝滚动。
                //思路:赋值第一张图片放到ul的最后,然后当图片切换到第五张的时候
                //     直接切换第六章,再次从第一张切换到第二张的时候先瞬间切换到
                //     第一张图片,然后滑动到第二张
                //步骤:
                //1.获取事件源及相关元素。(老三步)
                //2.复制第一张图片所在的li,添加到ul的最后面。
                //3.给ol中添加li,ul中的个数-1个,并点亮第一个按钮。
                //4.鼠标放到ol的li上切换图片
                //5.添加定时器
                //6.左右切换图片(鼠标放上去隐藏,移开显示)
    
    
    
    
    
    
                //1.获取事件源及相关元素。(老三步)
                var all = document.getElementById("all");
                var screen = all.firstElementChild || all.firstChild;
                var imgWidth = screen.offsetWidth;
                var ul = screen.firstElementChild || screen.firstChild;
                var ol = screen.children[1];
                var div =screen.lastElementChild || screen.lastChild;
                var spanArr = div.children;
    
                //2.复制第一张图片所在的li,添加到ul的最后面。
                var ulNewLi = ul.children[0].cloneNode(true);
                ul.appendChild(ulNewLi);
    
                // //3.给ol中添加li,ul中的个数-1个,并点亮第一个按钮。
                for(var i = 0 ;i<ul.children.length-1;i++){
                    var olNewLi = document.createElement("li");
                    olNewLi.innerHTML = i+1;
                    ol.appendChild(olNewLi);
                }
                var olLiArr = ol.children;
                olLiArr[0].className = "current";
    
                //4.鼠标放到ol的li上切换图片
                for(var i=0;i<olLiArr.length;i++){
                    //自定义属性,把索引值绑定到元素的index属性上
                    olLiArr[i].index = i;
                    //排他
                    olLiArr[i].onmouseover = function () {
                        for(var j = 0;j<olLiArr.length;j++ ){
                            olLiArr[j].className = "";
                        }
                        this.className = "current";
                        //鼠标放到小方块的时候,索引值和key以及square同步
                        // key = this.index;
                        // square = this.index;
                        key = square =this.index;
                        //移动盒子
                        animate(ul,-this.index*imgWidth);
    
                    }
                }
    
                //5.添加定时器
                //需要两个值
    
                var timer = setInterval(autoPlay,1000);
                //固定向右切换图片
                //两个定时器,一个记录图片key,一个记录小方块square
                var key =0;
                var square =0;
    
                function autoPlay() {
                    //通过控制key的自增模拟图片索引值,然后移动ul
                    key++;
                    if(key>olLiArr.length){  //索引不能大于等于5,如果等于5立刻等于0
                        //图片滑动到最后一张,接下来跳转到第一张,然后滑动到第二张
                        ul.style.left = 0;
                        key = 1;
                    }
                    animate(ul,-key*imgWidth);
    
                    //通过控制square的自增模拟小方块索引值,然后移动ol
                    //排他思想
                    square++;
                    if(square>olLiArr.length-1){  //索引不能大于等于5,如果等于5立刻等于0
                        square=0;
                    }
                    for(var i=0;i<olLiArr.length;i++){
                        olLiArr[i].className = "";
                    }
                    olLiArr[square].className = "current";
    
                }
                //鼠标放上清除定时器,移开后在开启定时器
                all.onmouseover = function () {
                    div.style.display ="block";
                    clearInterval(timer)
                };
                all.onmouseout = function () {
                    div.style.display = 'none';
                    timer = setInterval(autoPlay,1000)
                };
    
    
                //6.左右切换图片(鼠标放上去显示,移开隐藏)
                spanArr[0].onclick = function () {
                    //通过控制key的自增模拟图片索引值,然后移动ul
                    key--;
                    if(key<0){
                        //先移动到最后一张,然后key的值取之前一张的索引值,然后再向前移动
                        ul.style.left = -imgWidth*(olLiArr.length-1)+"px";
                        key = olLiArr.length-1;
                    }
                    animate(ul,-key*imgWidth);
    
                    //通过控制square的自增模拟小方块索引值,然后移动ol
                    //排他思想
                    square--;
                    if(square<0){  //索引不能大于等于5,如果等于5立刻等于0
                        square=olLiArr.length-1;
                    }
                    for(var i=0;i<olLiArr.length;i++){
                        olLiArr[i].className = "";
                    }
                    olLiArr[square].className = "current";
    
                };
    
                spanArr[1].onclick = function () {
                    //右侧的和定时器一模一样
                    autoPlay();
                };
    
    
    
                function animate(ele,target) {
                    clearInterval(ele.timer);
                    var speed = target > ele.offsetLeft ?10:-10;
                    ele.timer = setInterval(function () {
                        var val=target - ele.offsetLeft;
                        ele.style.left=ele.offsetLeft+speed+"px";
                        if(Math.abs(val)<=Math.abs(speed)){
                            ele.style.left=target + "px";
                            clearInterval(ele.timer)
                        }
                    },10);
                }
    
    
    
            }
        </script>
    </head>
    
    <body>
    <div class="all" id='all'>
        <div class="screen" id="screen">
            <ul id="ul">
                <li><img src="images/1.jpg" width="500" height="200" /></li>
                <li><img src="images/2.jpg" width="500" height="200" /></li>
                <li><img src="images/3.jpg" width="500" height="200" /></li>
                <li><img src="images/4.jpg" width="500" height="200" /></li>
                <li><img src="images/5.jpg" width="500" height="200" /></li>
            </ul>
            <ol>
    
            </ol>
            <div id="arr">
                <span id="left"><</span>
                <span id="right">></span>
            </div>
        </div>
    </div>
    </body>
    </html>
    
  • 相关阅读:
    Xcode6中如何添加pch文件
    iOS 在UILabel显示不同的字体和颜色
    IOS 获取手机ip地址
    获取设备基本信息
    [__NSCFConstantString size]: unrecognized selector sent to instance 错误
    控制器之间的跳转,多层的跳转
    Multiple build commands for output file
    Can't find keyplane that supports type 4 for keyboard iPhone-Portrait-NumberPad; using 3876877096_Portrait_iPhone-Simple-Pad_Default
    Activity的生命之路
    Spring自定义标签
  • 原文地址:https://www.cnblogs.com/zhangkui/p/8677634.html
Copyright © 2011-2022 走看看