原生js + css3 仿渡一首页(轮播图 + 炫酷导航栏 )

大致是效果图(录屏鼠标位置可能有点错位)在这里插入图片描述

这个首页原版是用jQuery写的,小柒用原生js仿写这个首页(涉及简单的ES6箭头函数语法),例如jQuery里的淡入淡出语法转为原生js的实现过程等。

整个js代码是由简单的函数模块实现

导航栏用到主要涉及到css3的animation、关键帧、transition、伸缩盒子
轮播图就是分为 点击小圆点切换 ,以及自动播放

代码都有详细注释:

html部分:

<body>
    <div class="wrapper">
        <!-- 头部 -->
        <div class="circle-box" id="_circle-box">
            <span class="bar"></span>
            <span class="bar"></span>
            <span class="bar"></span>
            <span class="bar"></span>
        </div>
        <header id="header">
            <nav class="navbar">
                <div class="logo-box" id="_logo-box">
                    
                </div>
                <div class="container">
                    <ul class="nav-right" id="_nav-right">
                        <li class="active"> 
                            <a href="#" >首页</a>
                        </li>
                        <li>
                            <a href="#">HTML5</a>
                        </li>
                        <li>
                            <a href="#">CSS3</a>
                        </li>
                        <li>
                            <a href="#">javaScript</a>
                        </li>
                        <li>
                            <a href="#">关于我们</a>
                        </li>
                    </ul>
                </div>
            </nav>
        </header>
        <div class="swiper-box" id="_swiper">
            <ul class="img-box" id="_img-box">
                <li>
                    <a href="#">
                        <img src="img/5.png" alt="">
                    </a>
                </li>
                <li>
                    <a href="#">
                        <img src="img/2.jpg" alt="">
                    </a>
                </li>
                <li>
                    <a href="#">
                        <img src="img/6.png" alt="">
                    </a>
                </li>
                <li>
                    <a href="#">
                        <img src="img/5.png" alt="">
                    </a>
                </li>
            </ul>
            <dic class="order-box" id="_order-box">

            </dic>
        </div>
    </div>
</body>

css部分:

 *{
    padding:0;
    margin: 0;
    list-style: none;
}
body {
    overflow-x: hidden;
}

/* 头部 */
.wrapper .circle-box {
    width: 42px;
    height: 42px;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 99;
    opacity: 0;
    background-color: #fff;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    /* 元素在主轴方向对齐的方式 */
    justify-content: center;
    /* 元素在侧轴方向对齐的方式 */
    align-items: center;
    /* 调整主轴为侧轴 */
    flex-direction: column;
    margin-top: 33px;
    transition: all 1.5s ease-in-out;

}
.wrapper .circle-box>.bar{
    background-color: #000;
    width: 22px;
    height: 2px;
    border-radius: 1px;
    margin: 2px 0;
}
.wrapper header {
    position: fixed;
    top: 0;
    z-index: 5;
    width: 100%;
    height: 110px;
    background-color: #222;
    /* animation: fade 1s ease-in-out forwards; */
    /* animation-play-state: paused; */
}
@keyframes fadeOut {
    from {
        opacity: 1;
    }
    to{
        opacity: 0;
    }
}
@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to{
        opacity: 1;
    }
}
header .navbar {
    position: relative;
    padding: 0 15px;
    height: 110px;
}
header .logo-box {
    position: absolute;
    top:50%;
    left: 20%;
    margin-top: -21px;
    display: inline-block;
    width: 42px;
    height: 45px;
    background: url('img/wan.jpg');
    background-size: 100% 100%;
    margin-left: -15px;
    transition: all 1s ease-in-out;
}
 
header .container {
    padding: 0 15px;
}
header .container .nav-right {
    float: right;
    margin-right: 110px;
    height: 110px;
}
header .container .nav-right>li {
    float: left;
    height: 110px;
    position: relative;
    /*超出部分隐藏  */
    overflow: hidden;
}

header .container .nav-right li::after {
    content: "";
    position: absolute;
    width: 0%;
    bottom: 4px;
    transition: width 0.8s ease-in-out;
    border-bottom: 2px solid #ccc;
}
/* li hover之后 伪元素的变换*/
header .container .nav-right li:hover::after {
    /* 宽度撑满*/
    width: 100%;
}

header .container .nav-right>li>a {
    position: relative;
    display: inline-block;
    width: 116px;
    line-height: 110px;
    text-align: center;
    text-decoration: none;
    color: #fff;
    font-size: 14px;
    cursor: pointer;
    left:0;
    transition: all 0.5s ease-in-out;
}
header .container .nav-right li.active a {
    color: burlywood;
}

/* banner部分 */
.swiper-box {
    width: 100%;
    height: 600px;
    margin-top: 110px;
    position: relative;
}
.swiper-box .img-box {
    position: absolute;
    top: 0;
    left: 0;
    font-size: 0; 
    height: 100%;
}
.swiper-box .img-box>li {
    display: inline-block;
    height: 100%;;
}

.swiper-box .img-box>li img {
     vertical-align: middle;
     width: 100%;
     height: 100%;
}
.swiper-box .order-box {
    position: absolute;
    bottom: 2%;
    width: 100%;
    text-align: center;
    height: 30px;
}
.swiper-box .order-box  li{
    display: inline-block;
    width: 12px;
    height: 12px;
    margin-right: 15px;
    border-radius: 50%;
    cursor: pointer;
    background-color: lightgray;
}
.swiper-box .order-box  li.active{
    background-color: #be926f;
}


js部分:

 window.onload = function() {
    var imgBox = document.getElementById('_img-box');
    var swiper = document.getElementById('_swiper');
    var ul  = document.createElement('ul');
    var header = document.getElementById('header');
    var circle = document.getElementById('_circle-box');
    var nav_right = document.getElementById('_nav-right');
    var logo_box = document.getElementById('_logo-box');
    var as = nav_right.getElementsByTagName('a');
    var imglis = imgBox.children;
    var leader = 0;
    var target = 0;
    var current = 0; // 当前序列
    var autoTimer = null;
    init();
    function init() {
        // 头部
        animate();
        navClick();
        // banner部分
        setBanner();
        // 创建小圆点
        createSquare();
        // 自动播放
        autoPlay();
        swiper.addEventListener('mouseleave',function() {
            clearInterval(autoTimer);
            autoPlay();

        });
        swiper.addEventListener('mouseenter',function(){
            clearInterval(autoTimer);
            autoTimer = null;
        })      
    }
    function animate() {
       // 页面滚动大于0时触发
        window.onscroll =  () => {
            var scrollTop = window.pageXOffset || document.documentElement.scrollTop || document.body.scrollTop;
            if(scrollTop > 0){
                fadeIn();
            }            
            if(scrollTop <= 0){
                fadeOut();
            }
        }
        // 头像被点击时显示
        circle.addEventListener("click",() => {
                fadeOut();
         });
    }
    // 淡入
    function fadeIn() {
        header.style.animation = " fadeOut 1s ease-in-out forwards";
        circle.style.left =  (window.innerWidth - 60)+ 'px';
        circle.style.opacity = 1;
        logo_box.style.left =  (window.innerWidth - 60)+ "px";
        logo_box.style.opacity = 0;
        for(let i = 0;i < as.length;i++){
            as[i].style.left = 90 +'px';
        }
    }
    // 淡出
    function fadeOut() {
        header.style.animation = " fadeIn 1s ease-in-out forwards";
        circle.style.left =  0 + 'px';
        circle.style.opacity = 0;
        logo_box.style.left =  20 + '%';
        logo_box.style.opacity = 1;
        for(let i = 0;i < as.length;i++){
            as[i].style.left = 0 +'px';
        }
    }
    // 点击导航条li文字颜色改变
    function navClick() {
        var nav_right = document.getElementById('_nav-right');
        var lis = nav_right.children;
        for(let i = 0;i < lis.length;i++)
        {
            lis[i].onclick = function() {
                for( let j = 0;j < lis.length;j++)
                {
                    lis[j].className = '';
                }
                this.classList.add('active');
        }
        }
    }
    // 设置banner的ul宽度以及每个li的宽度
    function setBanner() {
        var clientWidth = document.documentElement.clientWidth;// 检测屏幕宽度
        imgBox.style.width = clientWidth*4 + 'px';
        for(let i = 0;i < imglis.length;i++)
        {
            imglis[i].style.width = clientWidth + 'px';
        }
        
    }
    // 生成小圆点
    function createSquare() {
        var orderBox = document.getElementById('_order-box'); 
        orderBox.appendChild(ul);
        for( let i = 0; i < imglis.length -1;i++)
        {
            var temp = document.createElement('li');
            ul.appendChild(temp);
        }
        ul.children[0].classList.add('active');
        let lis  = ul.children;
        for(let i = 0;i < lis.length;i++)
        {
            lis[i].addEventListener('mouseover',() => {
                for(let j = 0;j < lis.length;j++){
                    lis[j].classList.remove('active');
                }
            this.className = 'active';
            // 轮播图
            slider(i);
            });
        }
    }
          
    // 轮播图
    function slider(index) {
         current = index;
        if(current === imglis.length)// 当current = 4时,让imgBox的left立即等于 - imgBox.offsetWidth / 4
        {
            imgBox.style.left = - imgBox.offsetWidth / 4 + 'px' ;
            target = leader  = 0;// 一定要重新记录,否则会发生回溯的现象
            current = 1;// 设置为1,播放的下一张为第二张
        }
        for(let i = 0; i < ul.children.length;i++) // 根据序列值调整 小圆点的背景色
        {
            const element = ul.children[i];
            if (i === current )
            {
                element.style.backgroundColor = '#be926f';
            }
            else {
                element.style.backgroundColor = 'lightgray';
            }
        }
        //  这个if判断一定要放在for循环的后面 
        if(current === imglis.length -1)// 当current = 3时,第一个小圆点的背景亮起来
        {
            ul.children[0].style.backgroundColor = '#be926f';
        }
        target = imgBox.offsetWidth / 4 * current;
        clearInterval(imgBox.timer);
        imgBox.timer = setInterval(() => {
            leader = leader + (target - leader) / 10;
            imgBox.style.left = -leader + 'px';
            if(Math.abs(imgBox.offsetLeft - target) < 1)
            {
                clearInterval(imgBox.timer);
                imgBox.style.left = target+ 'px';
            }
          
        },30);
    }
    
    function autoPlay() {
        autoTimer = setInterval(function() {
                slider(current + 1);
        },3000);
    }
     
}

  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值