2019.6.22 Web全栈开发 学习笔记(十三)

JavaScript Day 6

上课日期:2019年8月15日

课程内容

这次的三个案例都是用js代码实现的对网页上图片的一个处理,我们在浏览网页的时候,如果图片全部摆成一行,或者摆在一个地方,很容易影响页面美观,所以使用三维轮播等效果,就能使美观性大大提高。

一、案例:三维图片轮播

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        body {
            background: black;
        }

        .banner {
            position: relative;
            width: 900px;
            height: 350px;
            margin: auto;
        }

        .imglist {
            width: 400px;
            height: 200px;
            position: absolute;
            box-shadow: 0 0 15px #fff;
            transition: all .1s linear;
        }

        .imglist > img {
            width: 100%;
            height: 100%;
        }

        .items {
            position: absolute;
            z-index: 10;
            left: 0;
            bottom: -40px;
            width: 100%;
            text-align: center;
        }

        .items > div {
            width: 17px;
            height: 17px;
            margin: 0 5px;
            border: 2px solid #fff;
            box-sizing: border-box;
            display: inline-block;
            transform: rotate(45deg);
        }

        .bgcolor {
            background: #17ff1e;
        }
    </style>
</head>
<body>
<div class="banner">
    <div class="imglist" style="left: 250px;top: 150px;z-index: 5">
        <img src="./img/1.jpg" alt=""/>
    </div>
    <div class="imglist" style="left:0px;top: 100px;z-index: 4">
        <img src="./img/2.jpg" alt=""/>
    </div>
    <div class="imglist" style="left:0px;top: 50px;z-index: 3">
        <img src="./img/3.jpg" alt=""/>
    </div>
    <div class="imglist" style="left:250px;top: 0px;z-index: 2">
        <img src="./img/4.jpg" alt=""/>
    </div>
    <div class="imglist" style="left:500px;top: 50px;z-index: 3">
        <img src="./img/5.jpg" alt=""/>
    </div>
    <div class="imglist" style="left:500px;top: 100px;z-index: 4">
        <img src="./img/6.jpg" alt=""/>
    </div>
    <div class="items">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
</div>
<script>
    /*
     * 1.网页加载完成  默认转动
     *
     * */

    var lists = document.getElementsByClassName("imglist");
    var item = document.getElementsByClassName("items")[0].children;
    var speed = 1500;
    var timer = null;
    var index = 0//记录当前正面的那张图的索引
    var count = 0;//用户操作的时候转的次数
    var isanimate = true;//记录动画的状态
    function animate() {
        timer = setInterval(function () {
            ShunAnimate();
            addColor();
        }, speed);
    }
    //顺时针动画
    function ShunAnimate() {
        var ele_0 = [lists[0].style.left, lists[0].style.top, lists[0].style.zIndex];
        for (var i = 0; i < lists.length; i++) {
            if (i < lists.length - 1) {
                lists[i].style.left = lists[i + 1].style.left;
                lists[i].style.top = lists[i + 1].style.top;
                lists[i].style.zIndex = lists[i + 1].style.zIndex;
            }
            else {
                lists[i].style.left = ele_0[0];
                lists[i].style.top = ele_0[1];
                lists[i].style.zIndex = ele_0[2];
            }
            if (lists[i].style.zIndex == 5) {
                index = i;
            }
        }
    }
    function addMouseEnter() {
        for (var i = 0; i < lists.length; i++) {
            lists[i].onmouseenter = function () {
                //停止计时器
                clearInterval(timer);
            }
            lists[i].onmouseleave = function () {
                animate();
            }
        }
    }
    function addColor() {
        //下面的点跟随变化的一个方法
        for (var i = 0; i < item.length; i++) {
            //当前变色
            if (i == index) {
                item[i].className = "bgcolor";
            }
            else {
                item[i].className = "";
            }
        }
    }
    function additemEvent() {
        for (var i = 0; i < item.length; i++) {
            item[i].index = i;
            item[i].onmouseenter = function () {
                clearInterval(timer);
                if (isanimate) {
                    isanimate = false;
                }
                else {
                    return;
                }
                //进入当前的变色
                count = this.index - index;
                //顺时针动画
                var num = 0;
                var c = 0;
                if (this.index > index) {
                    c = item.length - count;
                }
                else if (this.index < index) {
                    c = index - this.index;
                }
                loop();
                function loop() {
                    num++;
                    if (num > c) {
                        console.log("动画完成");
                        isanimate = true;
                        return;
                    }
                    ShunAnimate();//0.3ms
                    setTimeout(loop, 100);
                }

                index = this.index;
                addColor();

            }
            item[i].onmouseleave = function () {
                animate();
            }
        }
    }
    window.onload = function () {
        animate();
        addMouseEnter();
        addColor();
        additemEvent();
    }
</script>
</body>
</html>

二、案例:横向滚动

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .par {
            position: relative;
            width: 600px;
            height: 300px;
            border: 1px solid black;
            margin: auto;
            overflow: hidden;
        }

        .nav {
            width: 4200px;
            height: 300px;
        }

        .tras {
            transition: margin-left 0.5s ease-in;
        }

        .nav > li {
            width: 600px;
            height: 300px;
            float: left;
            list-style: none;
        }

        .nav > li img {
            width: 100%;
            height: 100%;
        }

        .dian {
            position: absolute;
            width: 100%;
            height: auto;
            left: 0;
            bottom: 0;
            text-align: center;
        }

        .dian > div {
            width: 13px;
            height: 13px;
            border-radius: 50%;
            border: 1px solid #fff;
            display: inline-block;
        }

        .bgcolor {
            background: red;
        }

        .btn {
            position: absolute;
            top: 140px;
            z-index: 11;
            width: 100%;
        }

        .btn > span {
            display: block;
            width: 30px;
            height: 30px;
            background: rgba(0, 0, 0, 0.69);
            color: #fff;
            text-align: center;
            line-height: 30px;
        }

        .btn > span:nth-child(1) {
            border-radius: 0 50% 50% 0;
            float: left;
        }

        .btn > span:nth-child(2) {
            border-radius: 50% 0 0 50%;
            float: right;
        }
    </style>
</head>
<body>
<div class="par">
    <ul class="nav tras">
        <li>
            <img src="img/1.jpg">
        </li>
        <li>
            <img src="img/2.jpg">
        </li>
        <li>
            <img src="img/3.jpg">
        </li>
        <li>
            <img src="img/4.jpg">
        </li>
        <li>
            <img src="img/5.jpg">
        </li>
        <li>
            <img src="img/6.jpg">
        </li>
        <li>
            <img src="img/1.jpg">
        </li>
    </ul>
    <div class="dian">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>

    <div class="btn">
        <span><</span>
        <span>></span>
    </div>
</div>
<script>
    /*
     * 1.初始动画
     * */
    var nav = document.getElementsByClassName("nav")[0];
    var par = document.getElementsByClassName("par")[0];
    var dian = document.getElementsByClassName("dian")[0].children;
    var btnspan = document.getElementsByClassName("btn")[0].children;
    var speed = 2000;
    var count = 0;
    var index = 0;
    var timer = null;
    function animate() {
        timer = setInterval(function () {
            transanimte();
        }, speed)
    }
    function transanimte() {
        nav.className = "nav tras";
        count++;
        nav.style.marginLeft = (-600 * count) + "px";
        setTimeout(function () {
            //动画完成之后去做判断
            if (count >= nav.children.length - 1) {
                nav.className = "nav";
                count = 0;
                nav.style.marginLeft = "0px";
            }
            index = count == 6 ? 0 : count;
            addColor();
        }, 500);
    }
    function addColor() {
        for (var i = 0; i < dian.length; i++) {
            if (i == index) {
                dian[i].className = "bgcolor";
            }
            else {
                dian[i].className = "";
            }
        }
    }
    window.onload = function () {
        animate();
        addColor();
        //事件
        par.onmouseenter = function () {
            clearInterval(timer);
        }
        par.onmouseleave = function () {
            animate();
        }
        for (var i = 0; i < dian.length; i++) {
            dian[i].index = i;
            dian[i].onmouseenter = function () {
                count = this.index - 1;
                transanimte();
                index = this.index;
                addColor();
            }
        }
        //两箭头事件
        btnspan[0].onclick = function () {
            transanimte();
        }
        //右边   比较难写
        btnspan[1].onclick = function () {
            count--;
            if (count < 0) {
                nav.className = "nav";
                count = nav.children.length - 2;
                nav.style.marginLeft = "-3600px";
            }
            //稍微上下代码有点延迟
            setTimeout(function () {
                nav.className = "nav tras";
                nav.style.marginLeft = (-600 * count) + "px";
                //动画完成之后
                setTimeout(function () {
                    index = count;
                    addColor();
                }, 500)
            }, 1);
        }
    }
</script>
</body>
</html>

三、案例:淡入淡出

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        .par {
            width: 600px;
            height: 400px;
            border: 1px solid black;
            margin: auto;
            position: relative;
        }

        .par > div.items {
            position: absolute;
            width: 100%;
            height: 100%;
            opacity: 0;
            transition: opacity 0.6s ease-in-out;
        }

        .par > div > a img {
            width: 100%;
            height: 100%;
            border: 0;
        }

        .nav {
            position: absolute;
            z-index: 10;
            width: 100%;
            left: 0;
            bottom: 0;
            text-align: center;
        }

        .nav > div {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            border: 2px solid #fff;
            box-shadow: 0 0 10px #fff;
            display: inline-block;
        }

        .bgcolor {
            background: red;
        }

        .btn {
            position: absolute;
            top: 180px;
            z-index: 11;
            width: 100%;
        }

        .btn > span {
            display: block;
            width: 30px;
            height: 30px;
            background: rgba(0, 0, 0, 0.69);
            color: #fff;
            text-align: center;
            line-height: 30px;
        }

        .btn > span:nth-child(1) {
            border-radius: 0 50% 50% 0;
            float: left;
        }

        .btn > span:nth-child(2) {
            border-radius: 50% 0 0 50%;
            float: right;
        }
    </style>
</head>
<body>
<div class="par">
    <div class="items"><a href="#"><img src="./img/1.jpg" alt=""/></a></div>
    <div class="items"><a href="#"><img src="./img/2.jpg" alt=""/></a></div>
    <div class="items"><a href="#"><img src="./img/3.jpg" alt=""/></a></div>
    <div class="items"><a href="#"><img src="./img/4.jpg" alt=""/></a></div>
    <div class="items"><a href="#"><img src="./img/5.jpg" alt=""/></a></div>
    <div class="items"><a href="#"><img src="./img/6.jpg" alt=""/></a></div>
    <div class="nav">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
    <div class="btn">
        <span><</span>
        <span>></span>
    </div>
</div>
<script>
    /*
     * 淡入淡出动画
     * 1.设置层的情况
     * 2.写层的动画
     * 3.添加淡入淡出动画
     * 4.写圈圈动画  默认第一个有颜色  ---对应的图片对应的圈 变化
     *5.添加事件  鼠标进入和离开
     * 6.给点添加事件
     * 7.当前点变色   显示当前对应的图片
     * 8.给两个箭头添加点击事件操作动画
     * */
    var items = document.getElementsByClassName("items");
    var dian = document.getElementsByClassName("nav")[0].children;
    var btnspan = document.getElementsByClassName("btn")[0].children;
    var parentEle = document.getElementsByClassName("par")[0];
    var speed = 2000;
    items[0].style.opacity = 1;
    var index = 0;
    var timer = null;
    //设置层
    function attrZindex() {
        for (var i = 0; i < items.length; i++) {
            items[i].style.zIndex = items.length - i - 1;
        }
    }
    //动画方法
    function animate() {
        timer = setInterval(function () {
            setindexopacity("right", 1);
            addColor();
        }, speed);
    }
    function setindexopacity(res, count) {
        for (var k = 0; k < count; k++) {
            for (var i = 0; i < items.length; i++) {
                var zindex = parseInt(items[i].style.zIndex);
                if (res == "right") {
                    zindex += 1;
                    if (zindex >= items.length) {
                        items[i].style.opacity = 0;
                        zindex = 0;
                    }
                    if (zindex == items.length - 1) {
                        items[i].style.opacity = 1;
                        //动态修改index 的值
                        index = i;
                    }
                }
                else {
                    zindex -= 1;
                    if (zindex == -1) {
                        items[i].style.opacity = 1;
                        zindex = items.length - 1;
                        //动态修改index 的值
                        index = i;
                    }
                    else if (zindex == items.length - 2) {
                        items[i].style.opacity = 0;
                    }
                }
                items[i].style.zIndex = zindex;
            }
        }
    }
    //点颜色的相关操作
    function addColor() {
        for (var i = 0; i < dian.length; i++) {
            if (i == index) {
                dian[i].className = "bgcolor";
            }
            else {
                dian[i].className = "";
            }
        }
    }
    window.onload = function () {
        attrZindex();
        animate();
        addColor();
        //操作动画  停止开启
        parentEle.onmouseenter = function () {
            clearInterval(timer);
        }
        parentEle.onmouseleave = function () {
            animate();
        }
        //给点添加事件
        for (var i = 0; i < dian.length; i++) {
            dian[i].index = i;
            dian[i].onmouseenter = function () {
                //鼠标进入当前点变色
                //使用当前的索引 -之前的索引
                var count = this.index - index > 0 ? this.index - index : dian.length + (this.index - index);
                setindexopacity("right", count);
                index = this.index;
                addColor();
            }
        }
        //给left  right btn 事件
        //left
        btnspan[0].onclick = function () {
            setindexopacity("left", 1);
            addColor();
        }
        //right
        btnspan[1].onclick = function () {
            setindexopacity("right", 1);
            addColor();
        }
    }
</script>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
前端技术的发展是互联网自身发展变化的一个缩影。前端技术指通过浏览器到用户端计算机的统称,存贮于服务器端的统称为后端技术。前端开发主要职能就是把网站的界面更好地呈现给用户。以前会Photoshop和Dreamweaver就可以制作网页,随着网站开发难度加大、开发方式多样,网页制作更接近传统的网站后台开发,网页制作更多被称为Web前端开发前端技术包括4个部分:前端美工、浏览器兼容、CSS、HTML“传统”技术与Adobe AIR、Google Gears,以及概念性较强的交互式设计,艺术性较强的视觉设计等。在Web1.0时代,由于网速和终端能力的限制,大部分网站只能呈现简单的图文信息,并不能满足用户在界面上的需求,对界面技术的要求也不高。随着硬件的完善、高性能浏览器的出现和宽带的普及,技术可以在用户体验方面实现更多种可能,前端技术领域迸发出旺盛的生命力。2005年以后,互联网进入Web2.0时代,各种类似桌面软件的Web应用大量涌现,前端由此发生了翻天覆地的变化。网页不再只是承载单一的文字和图片,各种富媒体让网页的内容更加生动,网页上软件化的交互形式为用户提供了更好的使用体验,这些都是基于前端技术实现的。随着手机成为人们生活中不可或缺的一部分,成为人们身体的延伸,人们迎来了体验为王的时代。移动端的前端技术开发前景宽阔。此外,前端技术还能应用于智能电视、智能手表甚至人工智能领HTML掌握HTML是网页的核心,是一种制作万维网页面的标准语言,是万维网浏览器使用的一种语言,它消除了不同计算机之间信息交流的障碍。因此,它是网络上应用最为广泛的语言,也是构成网页文档的主要语言,学好HTML是成为Web开发人员的基本条件。HTML是一种标记语言,能够实现Web页面并在浏览器中显示。HTML5作为HTML的最新版本,引入了多项新技术,大大增强了对于应用的支持能力,使得Web技术不再局限于呈现网页内容。随着CSS、JavaScript、Flash等技术的发展,Web对于应用的处理能力逐渐增强,用户浏览网页的体验已经有了较大的改善。不过HTML5中的几项新技术实现了质的突破,使得Web技术首次被认为能够接近于本地原生应用技术,开发Web应用真正成为开发者的一个选择。HTML5可以使开发者的工作大大简化,理论上单次开发就可以在不同平台借助浏览器运行,降低开发的成本,这也是产业界普遍认为HTML5技术的主要优点之一。AppMobi、摩托罗拉、Sencha、Appcelerator等公司均已推出了较为成熟的开发工具,支持HTML5应用的发展。 [3] CSS学好CSS是网页外观的重要一点,CSS可以帮助把网页外观做得更加美观。JavaScript学习JavaScript的基本语法,以及如何使用JavaScript编程将会提高开发人员的个人技能。操作系统了解Unix和Linux的基本知识,对于开发人员有益无害。网络服务器了解Web服务器,包括对Apache的基本配置,htaccess配置技巧的掌握等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值