小兔鲜项目-原生JS实现页面登录模块

命运对每个人都是一样的,不一样的是各自的努力和付出不同,付出的越多,努力的越多,得到的回报也越多,在你累的时候请看一下身边比你成功却还比你更努力的人,这样,你就会更有动力。

导读:本篇文章讲解 小兔鲜项目-原生JS实现页面登录模块,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

1. 项目搭建

1.1 在项目开始之前,先做好准备工作,创建这样的文件目录

在这里插入图片描述

1.1.1 css文件夹

包含三个css文件,分别是base.css、common.css、login.css。
在这里插入图片描述

1.1.2 images文件

里面放一些固定的图片

1.1.3 uploads文件

里面放一些固定的图片

1.2 HTML结构

1.2.1 头部结构

在这里插入图片描述

1.2.2主体结构

在这里插入图片描述

1.2.3 login.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>小兔鲜儿-新鲜、惠民、快捷!</title>
    <meta name="description" content="小兔鲜儿官网,致力于打造全球最大的食品、生鲜电商购物平台。">
    <meta name="keywords" content="小兔鲜儿,食品,生鲜,服装,家电,电商,购物">
    <!-- 引入网站图标 -->
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
    <!-- 引入初始化样式 -->
    <link rel="stylesheet" href="./css/base.css">
    <!-- 引入公共样式 -->
    <link rel="stylesheet" href="./css/common.css">
    <!-- 引入登录样式 -->
    <link rel="stylesheet" href="./css/login.css">
    <!-- 引入字体图标 -->
    <link rel="stylesheet" href="https://at.alicdn.com/t/font_2143783_iq6z4ey5vu.css">
</head>

<body>
    <!-- 登录头部 -->
    <div class="xtx-login-header">
        <h1 class="logo"></h1>
        <a class="home" href="./index.html">进入网站首页</a>
    </div>
    <!-- 登录内容 -->
    <div class="xtx-login-main">
        <div class="wrapper">
            <form action="" autocomplete="off">
                <div class="box">
                    <div class="tab-nav">
                        <a href="javascript:;" class="active" data-id="0">账户登录</a>
                        <a href="javascript:;" data-id="1">二维码登录</a>
                    </div>
                    <div class="tab-pane">
                        <div class="link">
                            <a href="javascript:;">手机验证码登录</a>
                        </div>
                        <div class="input">
                            <span class="iconfont icon-zhanghao"></span>
                            <input required type="text" placeholder="请输入用户名称/手机号码" name="username">
                        </div>
                        <div class="input">
                            <span class="iconfont icon-suo"></span>
                            <input required type="password" placeholder="请输入密码" name="password">
                        </div>
                        <div class="agree">
                            <label for="my-checkbox">
                                <input type="checkbox" value="1" id="my-checkbox" class="remember" name="agree">
                                <span class="iconfont icon-xuanze"></span>
                            </label>
                            我已同意 <a href="javascript:;">《服务条款》</a href="javascript:;"> 和 <a>《服务条款》</a>
                        </div>
                        <div class="button clearfix">
                            <button type="submit" class="dl">登 录</button>
                            <!-- <a class="dl" href="./center.html">登 录</a> -->
                            <a class="fl" href="./forget.html">忘记密码?</a>
                            <a class="fr" href="./register.html">免费注册</a>
                        </div>
                    </div>
                    <div class="tab-pane" style="display: none;">
                        <img class="code" src="./images/code.png" alt="">
                    </div>
                </div>
            </form>
        </div>
    </div>
    <!-- 登录底部 -->
    <footer>
        <div class="ft w">
            <dl class="kefu">
                <dt>客服服务</dt>
                <dd class="zaixian">
                    <p>
                        <a href="javascript:;">
                            在线客服
                        </a>
                    </p>
                </dd>
                <dd class="wenti">
                    <p>
                        <a href="javascript:;">
                            问题反馈
                        </a>
                    </p>
                </dd>
            </dl>
            <dl class="guanzhu">
                <dt>关注我们</dt>
                <dd class="gongzhong">
                    <p>
                        <a href="javascript:;">
                            公众号
                        </a>
                    </p>
                </dd>
                <dd class="weibo">
                    <p>
                        <a href="javascript:;">
                            微博
                        </a>
                    </p>
                </dd>
            </dl>
            <dl class="xiazai">
                <dt>下载APP</dt>
                <dd>
                    <img src="./uploads/qrcode.png" alt="">
                </dd>
                <dd>
                    <p><a href="javascript:;">扫描二维码</a></p>
                    <p><a href="javascript:;">立马下载APP</a></p>
                    <button>下载页面</button>
                </dd>
            </dl>
            <dl class="rexian">
                <dt>服务热线</dt>
                <dd>
                    <p>400-0000-000</p>
                </dd>
                <dd>
                    <p>周一至周日 8:00-18:00</p>
                </dd>
            </dl>
        </div>
        <div class="fb">
            <div class="fb_t">
                <ul>
                    <li>
                        价格亲民
                    </li>
                    <li>
                        物流快捷
                    </li>
                    <li>
                        品质新鲜
                    </li>
                </ul>
            </div>
            <div class="fb_b">
                <p>
                    <a href="javascript:;">关于我们</a>
                    <i>|</i>
                    <a href="javascript:;">帮助中心</a>
                    <i>|</i>
                    <a href="javascript:;">售后服务</a>
                    <i>|</i>
                    <a href="javascript:;">配送与验收</a>
                    <i>|</i>
                    <a href="javascript:;">商务合作</a>
                    <i>|</i>
                    <a href="javascript:;">搜索推荐</a>
                    <i>|</i>
                    <a href="javascript:;">友情链接</a>
                </p>
                <p>
                    <a href="javascript:;">CopyRight @ 小兔鲜儿</a>
                </p>
            </div>
        </div>
        </div>
    </footer>
    <script>
        // 1、tab账户登录和二维码登录切换
        // 获取tab-nav元素
        const tab_nav = document.querySelector('.tab-nav')
        const tab_pane = document.querySelectorAll('.tab-pane')
        // 给tab-nav注册事件委托
        tab_nav.addEventListener('click', function (e) {
            // 判断是否点击的A链接
            if (e.target.tagName === 'A') {
                // 移除类
                tab_nav.querySelector('.active').classList.remove('active')
                // 添加类
                e.target.classList.add('active')
                // 切换下面的盒子
                // 移除所有盒子
                for (let i = 0; i < tab_pane.length; i++) {
                    tab_pane[i].style.display = 'none'
                }
                // 添加盒子
                tab_pane[e.target.dataset.id].style.display = 'block'
            }
        })
        // 2点击提交模块
        // 2.1获取整个form表单
        const form = document.querySelector('form')
        const remember = document.querySelector('.remember')
        const userName = document.querySelector('[name="username"]')
        // 2.2给form注册提交事件
        form.addEventListener('submit',function(e) {
            e.preventDefault()
            if(!remember.checked) {
                return alert('请勾选同意用户协议')
            }
            // 将用户名存入本地存储中,以便后续首页的调用
            localStorage.setItem('xtx_name',userName.value)
            // 跳转到首页
            location.href = './index.html'
        })
    </script>
</body>

</html>

1.3 css样式

1.3.1 页面初始化(base)样式

在默认的浏览器中有很多默认的样式,所以我们要对其进行清除并且设置自己的默认样式
代码示例:

  box-sizing: border-box;
}

/* 去除常见标签默认的 margin 和 padding */
body,
h1,
h2,
h3,
h4,
h5,
h6,
p,
ul,
ol,
li,
dl,
dt,
dd,
input {
  margin: 0;
  padding: 0;
}

/* 设置网页统一的字体大小、行高、字体系列相关属性 */
body {
  box-sizing: border-box;
  font: 16px/1.5 "Helvetica Neue", Helvetica, Arial, "Microsoft Yahei",
    "Hiragino Sans GB", "Heiti SC", "WenQuanYi Micro Hei", sans-serif;
  color: #333;
}

/* 去除列表默认样式 */
ul,
ol {
  list-style: none;
}

/* 去除默认的倾斜效果 */
em,
i {
  font-style: normal;
}

/* 去除a标签默认下划线,并设置默认文字颜色 */
a {
  text-decoration: none;
  color: #333;
}

/* 设置img的垂直对齐方式为居中对齐,去除img默认下间隙 */
img {
  vertical-align: middle;
}

/* 去除input默认样式 */
input {
  border: none;
  outline: none;
  color: #333;
}



/* 双伪元素清除浮动 */
.clearfix::before,
.clearfix::after {
  content: "";
  display: table;
}

.clearfix::after {
  clear: both;
}

1.3.2 公共样式(common)样式

在这里插入图片描述

.w {
    width: 1240px;
    margin: 0 auto;
}

/* 快捷导航栏开始 */
.shortcut {
    height: 52px;
    background-color: #333;
    line-height: 52px;
}
.s {
    position: absolute;
    left: 0;
    top: 0;
    
}
.shortcut ul {
    float: right;
}

.shortcut li {
    float: left;
}

.shortcut li:nth-child(13)::before {
    content: '';
    display: inline-block;
    vertical-align: middle;
    width: 11px;
    height: 16px;
    margin-right: 5px;
    background-color: #27ba9b;
    background: url(../images/sprites.png) no-repeat -160px -70px;
}

.sep {
    color: #666;
    margin: 0 15px;
}

.shortcut li a {
    color: #dcdcdc;
    font-size: 14px;
}

.shortcut li a:hover {
    color: #27ba9b;
}

/* 快捷导航栏结束 */
/* 主导航栏开始 */
.main_nav {
    height: 130px;
    padding: 30px 0;

}

.logo {
    float: left;
    width: 207px;
    height: 70px;

}

.logo h1 a {
    display: block;
    width: 207px;
    height: 70px;
    font-size: 0;
    background: url(.././images/logo.png) no-repeat;
    background-size: 100%;
}

.nav {
    float: left;
    margin-top: 5px;
    margin-left: 35px;
}

.nav ul li {
    float: left;
    margin: 24px;
}

.nav ul li a {
    padding-bottom: 5px;
}

.nav ul li:hover a {
    border-bottom: 1px solid #27ba9b;
    color: #27ba9b;
}

.search {
    float: left;
    margin-left: 35px;
    border-bottom: 2px solid #e7e7e7;
}

.search::before {
    content: '';
    display: inline-block;
    vertical-align: middle;
    margin-top: -2px;
    width: 18px;
    height: 22px;
    background: url(.././images/sprites.png) no-repeat -79px -69px;
}

.search input {
    width: 172px;
    height: 30px;
    margin-top: 26px;
    padding-left: 30px;

}

.search input::placeholder {
    color: #ccc;
}

.car {
    position: relative;
    float: left;
    width: 23px;
    height: 23px;
    background-color: #27ba9b;
    margin-top: 30px;
    margin-left: 15px;
    background: url(.././images/sprites.png) no-repeat -119px -70px;
}

.car span {
    position: absolute;
    top: -8px;
    right: -10px;
    font-size: 13px;
    line-height: 14px;
    padding: 0 5px;
    background-color: #e26237;
    border-radius: 7px;
    color: #fff;
}

/* 主导航栏结束 */
/* 底部模块开始 */
footer {
    height: 645px;
}

.ft {
    height: 50%;
}

.ft dl {
    width: 25%;
    float: left;
    text-align: center;
    margin-top: 79px;
    color: #999;
}

.ft dl dt {
    margin-bottom: 30px;
}

.kefu dd,
.guanzhu dd {
    display: inline-block;
    vertical-align: middle;
    width: 92px;
    height: 92px;
    border: 1px solid #eee;
    margin-right: 5px;

}

.kefu dd a,
.guanzhu dd a {
    display: block;
    margin-top: 22px;
    width: 100%;
    height: 100%;
    font-size: 14px;
    color: #999;
}

.kefu dd a::before,
.guanzhu dd a::before {
    display: block;
    width: 35px;
    height: 31px;
    content: '';
    margin: 0 auto;
    padding-bottom: 5px;
    background: url(../images/sprites.png) no-repeat;
    transition: all .5s;
}

.kefu .zaixian a::before {
    background-position: -250px -70px;
}

.kefu .zaixian a:hover::before {
    background-position: -204px -70px;
}

.kefu .wenti a::before {
    background-position: -350px -70px;
}

.kefu .wenti a:hover::before {
    background-position: -300px -70px;
}

.guanzhu .gongzhong a::before {
    background-position: -250px -15px;
}

.guanzhu .gongzhong a:hover::before {
    background-position: -205px -15px;
}

.guanzhu .weibo a::before {
    background-position: -350px -15px;
}

.guanzhu .weibo a:hover::before {
    background-position: -300px -15px;
}



.xiazai dd {
    width: 105px;
    height: 103px;
    display: inline-block;

}

.xiazai dd a {
    color: #999;
    font-size: 14px;
}

.xiazai dd button {
    width: 107px;
    height: 32px;
    background-color: #27ba9b;
    color: #FFF;
    margin-top: 10PX;
    border: none;
    cursor: pointer;
}

.xiazai dd img {
    width: 100%;
    height: 100%;
    border: 1px solid #eee;
    padding: 5px;
    margin-top: -60px;
}

.ft .rexian dt {
    margin-bottom: 40px;
}

.rexian dd:nth-of-type(1) {
    font-size: 22px;
}

.rexian dd:nth-of-type(2) {
    font-size: 15px;
    margin-top: 5px;
}

.fb {
    height: 50%;
    background-color: #333;
}

.fb .fb_t {
    height: 170px;
    line-height: 175px;
    width: 1393px;
    margin: 0 auto 40px;
    border-bottom: 1px solid #434343;
    text-align: center;
}

.fb .fb_t ul li {
    float: left;
    width: 33.33%;
    color: #fff;
    font-size: 28px;
}

.fb .fb_t ul li::before {
    content: '';
    display: inline-block;
    vertical-align: middle;
    width: 58px;
    height: 58px;
    background: url(../images/sprites.png) no-repeat;
}

.fb .fb_t ul li:nth-child(2):before {
    background-position: -65px 0;
}

.fb .fb_t ul li:nth-child(2):before {
    background-position: -130px 0;
}

.fb .fb_b {
    text-align: center;
    color: #999;
}

.fb .fb_b i {
    padding: 0 4px;
}

.fb .fb_b p a {
    color: #999;
    font-size: 14px;

}

.fb .fb_b p a:hover {
    color: #27ba9b;
}

.fb .fb_b p:nth-child(2) {
    margin-top: 5px;
}

/* 底部模块结束 */

1.3.3 首页样式(login)样式

在这里插入图片描述

2、JavaScript源码

由于我们需要将登录时输入的用户名传到首页,所以我们需要用到本地存储技术,将用户名存储到浏览器中以便首页对其进行调用。

   <script>
        // 1、tab账户登录和二维码登录切换
        // 获取tab-nav元素
        const tab_nav = document.querySelector('.tab-nav')
        const tab_pane = document.querySelectorAll('.tab-pane')
        // 给tab-nav注册事件委托
        tab_nav.addEventListener('click', function (e) {
            // 判断是否点击的A链接
            if (e.target.tagName === 'A') {
                // 移除类
                tab_nav.querySelector('.active').classList.remove('active')
                // 添加类
                e.target.classList.add('active')
                // 切换下面的盒子
                // 移除所有盒子
                for (let i = 0; i < tab_pane.length; i++) {
                    tab_pane[i].style.display = 'none'
                }
                // 添加盒子
                tab_pane[e.target.dataset.id].style.display = 'block'
            }
        })
        // 2点击提交模块
        // 2.1获取整个form表单
        const form = document.querySelector('form')
        const remember = document.querySelector('.remember')
        const userName = document.querySelector('[name="username"]')
        // 2.2给form注册提交事件
        form.addEventListener('submit',function(e) {
            e.preventDefault()
            if(!remember.checked) {
                return alert('请勾选同意用户协议')
            }
            // 将用户名存入本地存储中,以便后续首页的调用
            localStorage.setItem('xtx_name',userName.value)
            // 跳转到首页
            location.href = './index.html'
        })
    </script>

2.1实现效果:

在这里插入图片描述

在这里插入图片描述
需要源文件请私信我免费领取

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/144079.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!